问题描述
||
我试图仅提取PlanDetails,其中PlanDetail.company_id = Company.id和PlanDetail.id \'=> $ id ..(您可以在下面的控制器中查看条件)。
控制器:
function pd_list_by_company($id = null) {
$this->recursive = 2; // I am going to use containable to trim this.
return $this->PlanDetail->find(\'all\',array(\'conditions\' =>
array(\'AND\' =>
array(\'PlanDetail.company_id\' => \'Company.id\',array(\'PlanDetail.id\' => $id)))));
}
测试视图:
$planDetailsByCompany = $this->requestAction(\'/planDetails/pd_list_by_company\');
debug($planDetailsByCompany );
我的调试输出结果?
Array()
如果删除条件并仅找到全部,则将按预期获得所有PlanDetails,因此我知道数据正在传递。SQL调试转储甚至显示查询:
WHERE ((`PlanDetail`.`company_id` = \'Company.id\') AND (`PlanDetail`.`id` IS NULL))
是的,我确实注意到$ id为NULL,并且我知道该值必须存在。.所以也许我的问题是,即使我可以看到PlanDetail.id值,为什么$ id值也没有传递给控制器在查找(\'all \')上w /没有条件?
感谢您的提示。
解决方法
由于
$id
似乎为空,因此我假设您在不带参数的情况下调用该函数。而且您不会收到错误消息,因为就PHP而言,该参数是可选的。在这种情况下,它显然是必需的,因此应在函数声明中将其设为必需的参数:
function pd_list_by_company($id) {
另外,您可以简化return语句,不需要AND:
return $this->PlanDetail->find(\'all\',array(\'conditions\' =>
array(\'PlanDetail.company_id\' => \'Company.id\',\'PlanDetail.id\' => $id)
)
);
,回答为什么不传递$ id的问题是因为您没有传递$ id
要传递2的$ id,您需要在requestAction中执行以下操作
$ this-> requestAction(\'/ planDetails / pd_list_by_company / 2 \');
,在我看来,您的代码应该只是
return $this->PlanDetail->find(\'array(\'PlanDetail.id\' => $id));
假设您将$ this-> PlanDetail-> recursive标志设置为> 0,则您的模型应该已经知道并返回任何\'Company \'表的关联数据。
我已经习惯了CakePHP的旧版本(1.3),但是find()函数是非常基本的,旨在仅返回一行。
是的,您绝对需要使用在URL后面附加id的函数,例如。
$planDetailsByCompany = $this->requestAction(\'/planDetails/pd_list_by_company/999\');