模型 X 与模型 Y 没有关联 cakephp
我有属于用户的图片,还有属于用户的优惠券。在我的数据库中,每个图像和优惠券表都有一个外键“user_id”。当我调试这个控制器逻辑的输出时:
$f=$this->Coupon->find('all', array(
'conditions'=>array(
'OR'=> array (
'Coupon.expires' =>0,
'Coupon.end_date >'=>date('y-m-d')
)
),
'contain'=>array(
'Image',
'Location'=>array('id','address','city','state','zip','area_code','exchange','sln','website'),
'User'=>array('id')
)
)
);
$this->set('printcoupons', $f);
}
我得到了除图像数组之外的所有数据,蛋糕告诉我与优惠券无关。我尝试将 Coupon 的递归性设置为 2,但检索到的内容没有任何变化。我尝试加载用户模型并以这种方式进行查找,但结果却是灾难性的。考虑到我不希望它们直接相互关联,我只是不明白如何更好地让图像和优惠券一起使用。
I have Image which belongsTo User, and I have Coupon which belongsTo User. Each of the images and coupons table have a foreignKey of 'user_id' in my db. When I debug the output of this controller logic:
$f=$this->Coupon->find('all', array(
'conditions'=>array(
'OR'=> array (
'Coupon.expires' =>0,
'Coupon.end_date >'=>date('y-m-d')
)
),
'contain'=>array(
'Image',
'Location'=>array('id','address','city','state','zip','area_code','exchange','sln','website'),
'User'=>array('id')
)
)
);
$this->set('printcoupons', $f);
}
I get all of my data except an Image array, which cake tells me is not associated with Coupon. I have tried setting the recursiveness of Coupon to 2, and I get no change in what gets retrieved. I tried loading the User model and doing a find that way, with disastrous results. I just don't understand how better to get Image and Coupon to play together, considering I don't want them directly associated with each other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们的“包含”数组意味着图像与优惠券直接关联(它们之间没有模型),但事实并非如此。优惠券属于用户并且用户有*图像。因此,图像需要放入用户数组中。您包含的所有模型都可以使用全部或大部分可用的 find() 选项。当使用包含时,递归是布尔值。
http://book.cakephp.org/2.0 /en/core-libraries/behaviors/containable.html#containablebehavior-options
我认为你真的应该将其添加到你的 AppModel 中,因为你几乎总是使用包含和 Containable 必须附加到包含中使用的所有模型
public $actsAs = array('Containable');
这样您就不必继续附加每个模型的行为。
They way you have your 'contain' array would imply that Image is directly associated with Coupon (no model between them) which isn't the case. The coupon belongsTo User and User has* Image. So the Image would need to go in the User array. All the models that you contain can use all or most of the find() options available. when using contain, recursive is boolean.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containablebehavior-options
I think you should really just add this to your AppModel since you pretty much always use contain and Containable must to be attached to all models used in containment
public $actsAs = array('Containable');
That way you don't have to keep attaching the behavior to every model.