CakePHP array() 为空,但查询似乎得到了正确的结果

发布于 2024-11-09 08:50:01 字数 1022 浏览 2 评论 0原文

我试图仅提取 PlanDetails,其中 PlanDetail.company_id = Company.id AND 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,并且我知道该值需要在那里..所以也许我的问题是为什么 $id 值没有传递到控制器,即使我可以在 find('all') 上看到 PlanDetail.id 值,但没有条件??

感谢您的任何提示。

I am trying to extract ONLY the PlanDetails where PlanDetail.company_id = Company.id AND PlanDetail.id' => $id.. ( you can see the conditions in my controller below)..

Controller:

    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)))));
    }

Test View:

$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company');

debug($planDetailsByCompany );

Output result of my debug??

Array()

If I remove the conditions and just have the find all, I get all PlanDetails as expected, so I know the data is being passed.. SQL debug dump even shows the query:

WHERE ((`PlanDetail`.`company_id` = 'Company.id') AND (`PlanDetail`.`id` IS NULL))

And yes, I did notice the $id is NULL, and I know the value needs to be there.. So maybe my question is why is the $id value not being passed to the controller even though I can see the PlanDetail.id value on a find('all') w/ out the conditions??

Thanks for any tips.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福不弃 2024-11-16 08:50:01

由于 $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)
    )
);

Since $id seems to be null, I would assume that you call the function without the parameter. And you don't get an error message, because as far as PHP is concerned the parameter is optional. In this case it's clearly required, so you should make it a required parameter in your function declaration:

function pd_list_by_company($id) {

Also you could simplify the return statement, you do not need the AND:

return $this->PlanDetail->find('all',
    array('conditions' => 
        array('PlanDetail.company_id' => 'Company.id','PlanDetail.id' => $id)
    )
);
芸娘子的小脾气 2024-11-16 08:50:01

要回答为什么 $id 没有被传递的问题是因为你没有传递它

要传递 $id 为 2,你需要在 requestAction

$this->requestAction('/planDetails/pd_list_by_company/2) 中执行以下操作');

To answer the question why is the $id not being passed is because you're not passing it

To pass say $id of 2 you need to do the following in your requestAction

$this->requestAction('/planDetails/pd_list_by_company/2');

可爱咩 2024-11-16 08:50:01

在我看来,您的代码应该只是

return $this->PlanDetail->find('array('PlanDetail.id' => $id));

假设您将 $this->PlanDetail->recursive 标志设置为 > 0,你的模型应该已经知道并返回任何“公司”表的关联数据......

我已经习惯了旧的(1.3)版本的 CakePHP,但 find() 函数非常基本,旨在只返回一行。

是的,您肯定需要调用该函数并将 id 附加到 url,例如。

$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company/999');

Seems to me that your code should just be

return $this->PlanDetail->find('array('PlanDetail.id' => $id));

Assuming you have the $this->PlanDetail->recursive flag set to > 0, your Model should already know about and return the associated data for any 'Company' table.....

I'm used to an old (1.3) version of CakePHP but the find() function is pretty basic and is designed to only return one row.

and yes, you definitely need to call the function with the id appended to the url, eg.

$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company/999');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文