如何将这个MySql查询转换为CakePhp的find。
请告诉我如何在cakephp中练习编写Find查询

select distinct trips.fk_userid from spots, trips
where spots.fk_tripid = trips.id
    and trips.isapproved = 1
    and spots.id in (".$row[$first_index]['spot_list'].")

最佳答案

模型可以是Trip,您可以这样查询

$this->Trip->query("select distinct trips.fk_userid from spots, trips where spots.fk_tripid = trips.id and trips.isapproved = 1 and spots.id in (".$row[$first_index]['spot_list'].")");


您应该创建Trip and Spot模型,而在Trip模型中,您必须具有以下in Spot模型:
public $belongsTo = array(
    'Trip' => array(
        'className'     => 'Trip',
        'foreignKey'    => 'fk_tripid'
    )
);

像这样查询:
$this->Spot->find('all', array(
   'fields' => array("distinct Trip.fk_userid"),
   'conditions' => array(
       'Trip.isapproved' => 1,
       'Spot.id' => $row[$first_index]['spot_list']
)
));

关于mysql - 将MySql查询转换为CakePhp 2.0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28087636/

10-10 23:23