cakephp中的连接查询
您好,我是Cakephp的新手,我很难用Cakephp转换mysql查询。我有一个查询,我想转换成Cakephp语法cakephp中的连接查询,php,mysql,cakephp,Php,Mysql,Cakephp,您好,我是Cakephp的新手,我很难用Cakephp转换mysql查询。我有一个查询,我想转换成Cakephp语法 SELECT * FROM trip JOIN countries ON trip.departure_country_id = countries.id WHERE countries.country_name LIKE "eng%" 这就是我迄今为止所尝试的 class Trip extends AppModel { public $useTable = 'tr
SELECT *
FROM trip
JOIN countries ON trip.departure_country_id = countries.id
WHERE countries.country_name LIKE "eng%"
这就是我迄今为止所尝试的
class Trip extends AppModel
{
public $useTable = 'trip';
public $primaryKey = 'trip_id';
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'fields' => array('User.user_id','User.first_name','User.last_name','User.profile_image','User.country','User.phone_no')
),
'departure_country' => array(
'className' => 'Country',
'foreignKey' => 'departure_country_id',
),
'arrival_country' => array(
'className' => 'Country',
'foreignKey' => 'arrival_country_id',
)
);
public function getLocationBasedTrips($country){
return $this->find('all', array(
'conditions' => array(
'OR' => array('Country.country_name Like' => '%'.$country.'%')
),
));
}
}
出行模式:
$this->find('all',
array(
'joins' => array(
array(
'table' => 'countries',
'alias' => 'Country',
'type' => 'INNER',
'conditions' => array(
'Trip.departure_country_id = Country.id'
)
),
'conditions' => array(
'Country.country_name Like' => "%$country%"
)
)
);
从上一个答案中移动了一些括号。试试这个:
$this->find('all', array(
'joins' => array(
array(
'table' => 'countries',
'alias' => 'Country',
'conditions' => array(
'Trip.departure_country_id = Country.id'
)
)
),
'conditions' => array(
'Country.country_name LIKE' => "%$country%"
)
)
);
看一看这个。谢谢你的回答。。这是我得到的语法错误或访问冲突的错误:1064您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解在第1行使用“Array WHERE 1=1”的正确语法。我认为这是报价问题,我正在更新我的答案。您好,谢谢您的回答。它没有给我任何错误,但也没有打印任何结果。。就像结果是空的一样…使用以下
$db=ConnectionManager::getDataSource('default')调试sql查询;调试($db->getLog())
并查看sql查询是否与您期望的匹配好的,我在这里感到困惑。我调试了它,然后在phpmyadmin中运行查询,它正确地显示了结果,但是为什么我在变量中得到null…哦,对不起,我的错误