我有关键字在CakePHP的1.3简单的模型关系 - >产品CakePHP的同一阵列
Category
的hasMany Products
有数据数组与我在不同得到之间略有差异。控制器。在Categories
控制器中获取作为关联模型的Product
数据位于主要产品阵列中,并且在Products
中获取该数据时将其分开。
例如,要获得 '产品1'
在Categories
- $类别[ '产品'] [0] [ '标题']
和Products
- $产品[0] [ '产品' ] ['title']
我想用相同的元素来显示产品。无论使用哪种阵列方案都是一样的。哪里是正确的地方进行修改?我可以在获取它们后修改这些数组,但不要认为这是最好的选择。
当我在Categories
控制器,并得到一个类别,我得到这样的:
// $this->Category->findById('12');
Array
(
[ProductCategory] => Array
(
[id] => 12
[title] => Category 1
[updated] => 2013-02-24 10:06:15
[created] => 2013-02-24 10:06:15
)
[Product] => Array
(
[0] => Array
(
[id] => 4
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:17:01
[created] => 2013-02-24 09:12:59
)
[1] => Array
(
[id] => 6
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:54
[created] => 2013-02-24 09:13:53
)
)
和获取Products
控制器内的所有产品时:
// $this->Product->find('all');
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 10
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:16:42
[created] => 2013-02-24 09:16:35
)
)
[1] => Array
(
[Product] => Array
(
[id] => 8
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:47
[created] => 2013-02-24 09:15:39
)
)
)
)
获取来自关联模型的数据总是合并数组。如果使用'find('all')'或'findById'完成则无关紧要。 – 2013-02-24 16:30:52
@ Constantin.FF - 如果你想获得相同的数据,为什么你要从两个不同的模型运行查询?在您的产品模型中制作单一方法,并在您每次需要数据时调用该方法。 (另外,如果这实际上是一个元素,请查看'requestAction')。 – Dave 2013-02-24 17:19:31
我看到你的Eg。和'$ this-> Category-> Product-> find('all');'会工作,但我已经做了一个查询($ this-> Category-> findById('12'))数据,因为hasMany关系。 – 2013-02-24 17:56:16