您可以使用 findBy() 或 findAllBy() 来检索基于 ID 以外的其他内容的记录。如果您需要为查询提供条件,请使用常规find():
$this->Car->find(
'all',
array(
'conditions' => array(
'CarMake.Slug' => $slug,
'Car.Name LIKE' => $name
),
)
);
另外,对于您尝试设置的 URL,您需要为 /cars 创建一个 route:
Router::connect(
'/cars/:make',
array('controller' => 'cars', 'action' => 'bymake'),
array(
'pass' => array('make'),
'make' => '[A-Za-z]+'
)
);
编辑:
如果您的条件基于与您的模型的直接关联,则上述方法有效。如果您的条件是递归关联(即 Car->CarModel->CarMake),则需要使用显式连接:
$result = $this->Car->find('all', array(
'joins' => array(
array(
'table' => 'car_models',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array('car_models.id = Car.car_model_id')
),
array(
'table' => 'car_makes',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array(
'car_makes.id = car_models.car_make_id',
'car_makes.slug' => $slug
)
)
),
'conditions' => array(
'Car.name LIKE' => $name
)
));