atk4\data\Model::join PHP Method

join() public method

When object is loaded, then instead of pulling all the data from a single table, join will also query $foreign_table in order to find additional fields. When inserting the record will be also added inside $foreign_table and relationship will be maintained.
public join ( string $foreign_table, array $defaults = [] ) : Join
$foreign_table string
$defaults array
return Join
    public function join($foreign_table, $defaults = [])
    {
        if (!is_array($defaults)) {
            $defaults = ['master_field' => $defaults];
        } elseif (isset($defaults[0])) {
            $defaults['master_field'] = $defaults[0];
            unset($defaults[0]);
        }
        $defaults[0] = $foreign_table;
        $c = $this->_default_class_join;
        return $this->add(new $c($defaults));
    }

Usage Example

Example #1
0
 public function testBasicJoin()
 {
     $a = ['user' => [], 'contact' => []];
     $db = new Persistence_Array($a);
     $m = new Model($db, 'user');
     $m->addField('name');
     $j = $m->join('contact');
     $j->addField('contact_phone');
     $m['name'] = 'John';
     $m['contact_phone'] = '+123';
     $m->save();
     /*
             $this->assertEquals([
                 'user'=>[
                     1=>['name'=>'John','contact_id'=>1]
                 ],'contact'=>[
                     1=>['contact_phone'=>'+123'
                 ]]], $a);
             )*/
 }
All Usage Examples Of atk4\data\Model::join