将 MySql 查询转换为 CakePhp 2.0

Convert the MySql query to CakePhp 2.0

如何将此 MySql 查询转换为 CakePhp 的查找。 请告诉我如何练习在 cakephp

中编写查找查询
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 和 Spot 模型,并且在 Trip 模型中,您必须在 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']
)
));