我们有可以种植在地块中的地块和豆子。
我下定决心用下面的方法找到主人拥有的所有地块,里面有一颗豆子。
$plots=$this->Plot->findAllByOwnerAndBean_id(uid,'>0');
但是,它给了我SQL WHERE
Plot .
所有者= '15' AND
Plot .
bean_id = '> 0'
这表明这可能是不可能的,但我觉得这不是决定性的。(可能,甚至与2.2相关?)可能是这样,所以问题有两个:
我该如何从findBy中得到我想要的东西,如果我真的做不到,我该如何避免比下面更少的代码,我可以确认这些代码有效?
$plots = $this->Plot->find('all', array(
'conditions' => array(
'owner' => uid,
'bean_id >' => 0
)
));
我不知道用魔术方法是怎么可能的(可以用DboSource::expression(),但如果它的用户输入,你必须自己清理它)。但是,您可以在模型中创建一个辅助方法。
class Plot extends AppModel {
public function findAllByOwnerAndBeanId($owner, $beanId) {
return $this->find('all', array(
'conditions' => array(
'owner' => $owner,
'bean_id >' => $beanId,
),
));
}
}
编辑:您可以尝试以下操作,但请注意,它没有经过测试。
$ds = $this->Plot->getDataSource();
$plots = $this->Plot->findAllByOwnerAndBean_id($uid, $ds->expression('> ' . intval($userInputtedBeanId)));
Sanitize::escape()
可能比intval更好。