Search code examples
phpzend-framework2zend-db

ZF2: How to check if table is already joined in Select


I have a problem with Select object in ZF2.

Class One {

  function search(){
    $select = new Select();
    $this->conditions($select);

    $select->join(['t'=>'table'],'t.id = other_table.table_id',[]);
  }

  function conditions(select $select){
    $select->join(['t'=>'table'],'t.id = other_table.table_id',[]);
  }
}

First join is fine (in condition() method). How can I check if table is already joined when I want to join it second time?

I couldn't find the answer...


Solution

  • If you take a look at Zend\Db\Sql\Select class in ZF2 you will see that there is no method that can give you that information.

    I would suggest to create a separate method for join operations in your class and have a flag (boolean) in your class that will indicate whether join method has been called:

    private $isJoinCalled = false;
    
    private function join($select){
        $select->join(['t'=>'table'],'t.id = other_table.table_id',[]);
        $this->isJoinCalled = true;
    } 
    

    This way you can call this method after you check whether join was called or not:

    if(!$this->isJoinCalled){
        $this->join($select);
    }