LEFT JOIN in ZF2 using TableGateway

前端 未结 7 1751
无人共我
无人共我 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条回答
  • 2021-01-03 23:12

    This is the exact need for both Join and Where clauses with tableGateway.

    public function getEmployeefunctionDetails($empFunctionId) {
        $empFunctionId = ( int ) $empFunctionId;
        //echo '<pre>'; print_r($this->tableGateway->getTable()); exit;
        $where = new Where();
        $where->equalTo('FUNCTION_ID', $empFunctionId);
    
        $sqlSelect = $this->tableGateway->getSql()->select()->where($where);    
    
        $sqlSelect->columns(array('FUNCTION_ID'));
        $sqlSelect->join('DEPARTMENTS', 'DEPARTMENTS.DEPARTMENT_ID = EMPLOYEE_FUNCTIONS.DEPARTMENT_ID', array('DEPARTMENT_ID','DEPARTMENT_NAME'), 'inner');
        $sqlSelect->join('ROLES', 'ROLES.ROLE_ID = EMPLOYEE_FUNCTIONS.ROLE_ID', array('ROLE_ID','ROLE_NAME'), 'inner');
    
        //echo $sqlSelect->getSqlString(); exit;
        $resultSet = $this->tableGateway->selectWith($sqlSelect);
    
        if (! $resultSet) {
            throw new \Exception ( "Could not find row $empFunctionId" );
        }
        return $resultSet->toArray();
    }
    
    0 讨论(0)
  • 2021-01-03 23:14

    if you're using TableGateway, you can select join like this

    $sqlSelect = $this->tableGateway->getSql()->select();
    $sqlSelect->columns(array('column_name'));
    $sqlSelect->join('othertable', 'othertable.id = yourtable.id', array(), 'left');
    
    $resultSet = $this->tableGateway->selectWith($sqlSelect);
    return $resultSet;
    
    0 讨论(0)
  • 2021-01-03 23:14

    You have to include username field in the BlogsSetting Model that is used as model from BlogsSettingTable (The TableGateway)

    class BlogsSetting {
        public $blog_id;
        public $interest_id;
        public $owner_id;
        public $title;
        public $meta_description;
        public $meta_keywords;
        public $theme;
        public $is_active;
        public $date_created;
        public $username;
    
        public function exchangeArray($data)
        {
            // Create exchangeArray
        }
    }
    

    Hope this helps

    0 讨论(0)
  • 2021-01-03 23:15

    In your class inherited from AbstractTableGateway u can use Select with Closure like this:

    use Zend\Db\Sql\Select;
    ...
    public function getAllBlockSettings()
    {
        $resultSet = $this->select(function(Select $select) {
            $select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
        });
    
        return $resultSet;
    }
    
    0 讨论(0)
  • 2021-01-03 23:18

    Adding to @samsonasik's answer and addressing the issues in its comments. You won't be able to get the joined values out of what is returned from that statement. That statement returns the model object which won't have the joined rows. You'll need to execute it as SQL at a level which will prepare it as raw SQL and return you each resulting row as an array rather than an object:

    $sqlSelect = $this->tableGateway->getSql()->select();
    $sqlSelect->columns(array('column_name_yourtable'));
    $sqlSelect->join('othertable', 'othertable.id = yourtable.id', array('column_name_othertable'), 'left');
    
    $statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
    $resultSet = $statement->execute();
    return $resultSet;
    
    //then in your controller or view:
    
    foreach($resultSet as $row){
        print_r($row['column_name_yourtable']);
        print_r($row['column_name_othertable']);
    }
    
    0 讨论(0)
  • 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;
    },
    
    0 讨论(0)
提交回复
热议问题