【发布时间】:2013-07-22 01:25:52
【问题描述】:
我正在编写一个 CakePHP 插件。在我的插件AppModel 中,我有:
public $actsAs = array(
'Containable'
);
然后我有两个模型:CartItem 和 Product。我的CartItem 模型如下所示:
<?php
class CartItem extends ShoppingCartAppModel {
public $belongsTo = array(
'Cart',
'Product'
);
}
但是,在我的控制器中调用 Cart 模型时,我收到以下错误:
警告 (512):模型“CartItem”与模型“产品”不关联 [CORE/Cake/Model/Behavior/ContainableBehavior.php,第 344 行]
为什么会这样,当我定义我的 CartItem 模型通过 belongsTo 关联与 Product 模型关联时?
编辑:我已将我的问题缩小到我试图在我的 Cart 模型中获取我的购物车及其内容的地方。这是电话:
public function findBySessionId($sessionId) {
$cart = $this->find('first', array(
'conditions' => array(
'Cart.session_id' => $sessionId
),
'contain' => array(
'CartItem' => array(
'Product'
)
)
));
return $cart;
}
【问题讨论】:
-
如果你只使用 $this->find 这样的方式,它是否真的在使用你的模型文件?你不需要 $this->CartItem->find 否则它只会使用通用的 appModel 类?!
-
在我的
CartController中,我希望能够获取用户的购物车,以及该购物车中的CartItems 和相关的Products,以便我可以将其显示给用户。
标签: cakephp cakephp-model