Exemplo n.º 1
0
 public function testJoin()
 {
     $config = $this->getConfig();
     $user_model = new Model('user', $config);
     $join = [[['order o', 'tp_'], 'u.id=o.user_id'], [['user_address' => 'a'], 'u.id=a.user_id']];
     $result = $user_model->alias('u')->join($join)->field('u.username,a.consignee,o.amount')->select();
     $data = ['username' => 'test', 'consignee' => '张三', 'amount' => '200'];
     $this->assertEquals($data, $result[0]);
     $result = $user_model->alias('u')->join('__USER_ADDRESS__ a', 'u.id=a.user_id', 'left')->field('u.username,a.consignee')->select();
     $data = ['username' => 'test', 'consignee' => '张三'];
     $this->assertEquals($data, $result[0]);
     $result = $user_model->alias('u')->join('role_user ru', 'u.id=ru.user_id', 'left')->field('u.username,ru.role_id')->select();
     $data = ['username' => 'test', 'role_id' => '1'];
     $this->assertEquals($data, $result[0]);
     $order_model = new Model('order', $config);
     $subsql = $order_model->limit(1)->buildSql();
     $result = $user_model->alias('u')->join($subsql . ' o', 'u.id=o.user_id', 'left')->field('u.username,o.amount')->select();
     $data = ['username' => 'test', 'amount' => '200'];
     $this->assertEquals($data, $result[0]);
     // 兼容_join方法
     $result = $user_model->alias('u')->join('__USER_ADDRESS__ a on u.id=a.user_id', 'left')->field('u.username,a.consignee')->select();
     $data = ['username' => 'test', 'consignee' => '张三'];
     $this->assertEquals($data, $result[0]);
 }