gpt4 book ai didi

mysql - 如何编写 CakePHP 模型查询

转载 作者:行者123 更新时间:2023-11-30 01:25:39 25 4
gpt4 key购买 nike

我的模型中有以下情况:

-a "users" table, with "id" as primary key
-a "stores" table with "id" as primary key, and "users_id" as foreign key
-a "promos" table with "id" as primary key, and "stores_id" as foreign key

换句话说,每个促销只属于 1 个商店,每个商店只属于 1 个用户,并且关系是在模型文件中设置的。

我想创建一个操作/ View ,允许登录用户只能看到他自己的促销,但我不知道如何实现。

以下代码向我显示一条错误消息,因为“User.id”不是促销表的一部分,但我不知道如何创建参数来将促销列表限制为仅此用户拥有的商店。

$allPromos = $this->Promo->find('all', array('conditions' => array('Store.id' => $storeId, 'Store.enabled' => 1, 'User.id' => $this->Session->read('id'))));

有什么建议吗?

谢谢!

最佳答案

嗯,你的模型结构有点奇怪。现在看起来每个商店只属于一个用户,每个促销也只属于一个商店。如果这是正确的,请确保您的模型关联是正确的。您可以从此处的用户处找到它们,因此 User.php 需要 var $hasMany = array('Store') 而 Store.php 需要 var $hasMany = array('Promo')

$allPromos = $this->User->find('all', 
array('fields' => array('Promo.*'),
'contain' => array('Store', 'Promo'),
'recursive' => 2 //Two levels of recursion to get from User to Store to Promo
'conditions' =>
array('Store.id' => $storeId,
'Store.enabled' => 1,
'User.id' => $this->Session->read('id'))));

关于mysql - 如何编写 CakePHP 模型查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18027269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com