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
Esempio n. 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);
             )*/
 }
Esempio n. 2
0
 public function testDoubleReverseJoin()
 {
     $a = ['user' => [10 => ['id' => 10, 'name' => 'John 2', 'contact_id' => 100], 20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [100 => ['id' => 100, 'contact_phone' => '+555', 'country_id' => 1], 200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [1 => ['id' => 1, 'name' => 'UK'], 2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $c = $j->join('country');
     $c->addField('country_name', ['actual' => 'name']);
     $m_u->load(10);
     $m_u->delete();
     $m_u->loadBy('country_name', 'US');
     $this->assertEquals(30, $m_u->id);
     $this->assertEquals(['user' => [20 => ['id' => 20, 'name' => 'Peter', 'contact_id' => 100], 30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200], 40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300]], 'contact' => [200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2], 300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5]], 'country' => [2 => ['id' => 2, 'name' => 'US'], 3 => ['id' => 3, 'name' => 'India']]], $this->getDB());
 }
Esempio n. 3
0
 /**
  * @expectedException Exception
  */
 public function testLoadMissing()
 {
     $a = ['user' => [2 => ['id' => 2, 'name' => 'Peter', 'contact_id' => 1], 3 => ['id' => 3, 'name' => 'XX', 'contact_id' => 2], 4 => ['id' => 4, 'name' => 'YYY', 'contact_id' => 3]], 'contact' => [2 => ['id' => 2, 'contact_phone' => '+999'], 3 => ['id' => 3, 'contact_phone' => '+777']]];
     $db = new Persistence_Array($a);
     $m_u = new Model($db, 'user');
     $m_u->addField('contact_id');
     $m_u->addField('name');
     $j = $m_u->join('contact');
     $j->addField('contact_phone');
     $m_u->load(2);
 }
Esempio n. 4
0
 public function testExpressionJoin()
 {
     $a = ['user' => [1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1]], 'contact' => [1 => ['id' => 1, 'contact_phone' => '+123 smiths'], 2 => ['id' => 2, 'contact_phone' => '+321 sues']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     $m = new Model($db, 'user');
     $m->addFields(['name', 'gender', 'surname']);
     $m->join('contact')->addField('contact_phone');
     $m->tryLoad(1);
     $this->assertEquals('John', $m['name']);
     $this->assertEquals('+123 smiths', $m['contact_phone']);
     $m->tryLoad(2);
     $this->assertEquals('Sue', $m['name']);
     $this->assertEquals('+321 sues', $m['contact_phone']);
     $m->tryLoad(3);
     $this->assertEquals('Peter', $m['name']);
     $this->assertEquals('+123 smiths', $m['contact_phone']);
     $mm = clone $m;
     $mm->addCondition($mm->expr('[name] = [surname]'));
     $mm->tryLoad(1);
     $this->assertEquals(false, $mm->loaded());
     $mm->tryLoad(2);
     $this->assertEquals('Sue', $mm['name']);
     $this->assertEquals('+321 sues', $mm['contact_phone']);
     $mm->tryLoad(3);
     $this->assertEquals(false, $mm->loaded());
     $mm = clone $m;
     $mm->addCondition($mm->expr('"+123 smiths" = [contact_phone]'));
     $mm->tryLoad(1);
     $this->assertEquals('John', $mm['name']);
     $this->assertEquals('+123 smiths', $mm['contact_phone']);
     $mm->tryLoad(2);
     $this->assertEquals(null, $mm['name']);
     $this->assertEquals(null, $mm['contact_phone']);
     $mm->tryLoad(3);
     $this->assertEquals('Peter', $mm['name']);
     $this->assertEquals('+123 smiths', $mm['contact_phone']);
 }