如何从下面的mysql查询中准备ZF2查询
select (select category_name from categories where category_id = c.parent_id) as category_name
from
ebook as e
join ebook_categories as ec ON ec.ebook_id = e.ebook_id
join categories as c ON c.category_id = ec.category_id
where
e.is_active = 1
group by category_name
答案 0 :(得分:0)
您可以在zf2中使用tablegateway执行此操作。
例如:
public function fetchAllWithLikes( $user_id )
{
$adapter = $this->getAdapter();
$resultset = $this->select( function( Select $select ) use ( $user_id, $adapter ) {
$sub1 = new Select( 'likes' );
$sub1->columns( array( 'total' => new Expression('COUNT(*)') ) )
->where( array( 'id' => new Expression( 'song_id' ) ) );
$sub2 = new Select( 'likes' );
$sub2->columns( array( 'total' => new Expression('COUNT(*)') ) )
->where( array( 'id' => new Expression( 'song_id' ) ) )
->where( array( 'user_id' => $user_id ) );
$select->columns(
array(
'id',
'title',
'ytlink',
'total_likes' => new \Zend\Db\Sql\Expression( '?', array( $sub1 ) ),
'your_likes' => new \Zend\Db\Sql\Expression( '?', array( $sub2 ) ),
)
);
var_dump( $select->getSqlString( $adapter->getPlatform() ) );
});
return $resultset->toArray();
}