【发布时间】:2014-08-12 08:18:41
【问题描述】:
根据 Cookbook 的 Containable Behavior 部分的第二句,contain() 支持 limit 子句。
CakePHP 1.2 核心的新增功能是 ContainableBehavior。此模型行为允许您过滤和限制模型查找操作。
我正在使用 CakePHP 2.5。
我有两个模型:产品和类别(HABTM 关系)。
产品关系:
public $hasAndBelongsToMany = array(
'Category' => array(
'className' => 'Category',
'joinTable' => 'categories_products',
'foreignKey' => 'product_id',
'associationForeignKey' => 'category_id',
),
);
Category 的关系:
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'categories_products',
'foreignKey' => 'category_id',
'associationForeignKey' => 'product_id',
),
);
我正在尝试使用limit,如下所示:
$cats = $this->Category->find('all', array(
'fields' => array(
'id',
'parent_id'
),
'contain' => array(
'Product' => array(
'fields' => array(
'id',
'name',
),
'limit' => 3,
'order' => array('created DESC'),
),
),
));
结果是总共只选择了 3 个产品。每个类别不是 3 个产品。可包含行为已正确加载。如何获得每个类别 3 件产品?
【问题讨论】:
标签: cakephp orm cakephp-2.5 containable cakephp-2.x