Example #1
0
 public function testInnerJoin()
 {
     $j1 = new Join(Join::INNER);
     $j1->from('profiles', 'Profile')->on('User.id', 'Profile.user_id')->fields('id', 'user_id', 'lastLogin', 'currentLogin');
     $this->object->innerJoin(['profiles', 'Profile'], ['id', 'user_id', 'lastLogin', 'currentLogin'], ['User.id' => 'Profile.user_id']);
     $this->assertEquals($j1, $this->object->getJoins()[0]);
 }
Example #2
0
File: Query.php Project: titon/db
 /**
  * Add a new join type.
  *
  * @param string $type
  * @param string|array $table
  * @param array $fields
  * @param array $on
  * @return $this
  */
 protected function _addJoin($type, $table, $fields, $on = [])
 {
     $repo = $this->getRepository();
     $join = new Join($type);
     $conditions = [];
     if (is_array($table)) {
         $alias = $table[1];
         $table = $table[0];
     } else {
         $alias = $table;
     }
     foreach ($on as $pfk => $rfk) {
         if (strpos($pfk, '.') === false) {
             $pfk = $repo->getAlias() . '.' . $pfk;
         }
         if (strpos($rfk, '.') === false) {
             $rfk = $alias . '.' . $rfk;
         }
         $conditions[$pfk] = $rfk;
     }
     $this->_joins[] = $join->from($table, $alias)->on($conditions)->fields($fields);
     return $this;
 }
Example #3
0
 public function testFormatJoins()
 {
     $join = new Join(Join::LEFT);
     $join->from('users')->on(['id' => 'id']);
     $this->assertRegExp('/LEFT JOIN (`|\\")?users(`|\\")? ON (`|\\")?id(`|\\")? = (`|\\")?id(`|\\")?/', $this->object->formatJoins([$join]));
     $join->asAlias('User');
     $this->assertRegExp('/LEFT JOIN (`|\\")?users(`|\\")? AS (`|\\")?User(`|\\")? ON (`|\\")?id(`|\\")? = (`|\\")?id(`|\\")?/', $this->object->formatJoins([$join]));
     $join = new Join(Join::RIGHT);
     $join->from('profiles', 'profiles')->on('User.profile_id', 'profiles.id');
     $this->assertRegExp('/RIGHT JOIN (`|\\")?profiles(`|\\")? ON (`|\\")?User(`|\\")?.(`|\\")?profile_id(`|\\")? = (`|\\")?profiles(`|\\")?.(`|\\")?id(`|\\")?/', $this->object->formatJoins([$join]));
     $join = new Join(Join::INNER);
     $join->from('profiles', 'Profile')->on('User.id', 'Profile.user_id');
     $this->assertRegExp('/INNER JOIN (`|\\")?profiles(`|\\")? AS (`|\\")?Profile(`|\\")? ON (`|\\")?User(`|\\")?.(`|\\")?id(`|\\")? = (`|\\")?Profile(`|\\")?.(`|\\")?user_id(`|\\")?/', $this->object->formatJoins([$join]));
     $join2 = new Join(Join::OUTER);
     $join2->from('settings', 'Setting')->on('User.setting_id', 'Setting.id');
     $this->assertRegExp('/INNER JOIN (`|\\")?profiles(`|\\")? AS (`|\\")?Profile(`|\\")? ON (`|\\")?User(`|\\")?.(`|\\")?id(`|\\")? = (`|\\")?Profile(`|\\")?.(`|\\")?user_id(`|\\")? FULL OUTER JOIN (`|\\")?settings(`|\\")? AS (`|\\")?Setting(`|\\")? ON (`|\\")?User(`|\\")?.(`|\\")?setting_id(`|\\")? = (`|\\")?Setting(`|\\")?.(`|\\")?id(`|\\")?/', $this->object->formatJoins([$join, $join2]));
 }