LEFT JOIN in ZF2 using TableGateway

前端 未结 7 1754
无人共我
无人共我 2021-01-03 23:01

I have a table:

*CREATE TABLE IF NOT EXISTS `blogs_settings` (
  `blog_id` int(11) NOT NULL AUTO_INCREMENT,
  `owner_id` int(11) NOT NULL,
  `title` varchar(         


        
7条回答
  •  -上瘾入骨i
    2021-01-03 23:25

    Give it a try:

    namespace Object\Model;
    
    use Zend\Db\TableGateway\AbstractTableGateway;
    use Zend\Db\Sql\Select;
    
    class BlogsSettingsTbl extends AbstractTableGateway {
        public function __construct($adapter) {
            $this->table = 'blogs_settings';
            $this->adapter = $adapter;
            $this->initialize();
        }
    
        public function fetchAll() {
            $where = array(); // If have any criteria
            $result = $this->select(function (Select $select) use ($where) {
                        $select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
                        //echo $select->getSqlString(); // see the sql query
                    });
            return $result;
        }
    }
    

    Add to 'getServiceConfig()' in Module.php:

    'Object\Model\BlogsSettingsTbl' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $table = new BlogsSettingsTbl($dbAdapter); // <-- also add this to 'USE' at top
        return $table;
    },
    

提交回复
热议问题