2013-02-24 37 views
0

我有关键字在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 
       ) 

     ) 
    ) 
) 

回答

1

一个你的发现是find('all'),另一个是findById()(它使用find('first'))。

这两个都以不同的格式返回数据,因为find('first')知道你只想要一个项目,而find('all')是一个未知的项目集。

对两者都使用find('all'),但根据您是否只需要一个或多个来设置限制。然后,您的数据将返回完全相同。

您从哪个控制器检索数据对返回的数据没有影响。然而,哪种模型 - 只要确保你在使用同一模型进行查找。

例如,

//in your ProductsController 
$this->Product->find('all'); 

//in your CategoriesController 
$this->Category->Product->find('all'); 

// in some other controller 
$this->loadModel('Product); 
$this->Product->find('all'); 

PS - 但它的更好,如果你不这样做你的“认定”在你的控制器 - 使一个方法在你的模型,并从控制器(S)称之为所以不是$this->Product->find(),这将是$this->Product->getProducts()(或任何你想称之为的)。 (阅读更多关于“肥胖模型,瘦身控制器”的原因/例子等)。

+0

获取来自关联模型的数据总是合并数组。如果使用'find('all')'或'findById'完成则无关紧要。 – 2013-02-24 16:30:52

+0

@ Constantin.FF - 如果你想获得相同的数据,为什么你要从两个不同的模型运行查询?在您的产品模型中制作单一方法,并在您每次需要数据时调用该方法。 (另外,如果这实际上是一个元素,请查看'requestAction')。 – Dave 2013-02-24 17:19:31

+0

我看到你的Eg。和'$ this-> Category-> Product-> find('all');'会工作,但我已经做了一个查询($ this-> Category-> findById('12'))数据,因为hasMany关系。 – 2013-02-24 17:56:16

0

Dave是对的,不同之处在于您使用的方法......即使您声称关联数据总是合并,您在“产品”模型上的查找也不是关联数据,因此格式将始终为不同。

我在这里呆了一会儿,我已经注意到戴夫知道他的东西。 :) 我同意脂肪模型/瘦身控制器范例清洁,高效的代码。

如果更改:

<?php 

$this->Category->contain(array(
    'Product' 
)); 
$this->Category->find('all', 
    array(
     'conditions' => array(
      'Category.id' => $id // 12 for your OP. 
     ), 
     'limit' => 1 
    ) 
); 

?> 

应该给你:

<?php 

array 
    (
    [0] => array 
     (
     'Category' => array 
      (
      [id] => 12 
      [title] => Category 1 
      [updated] => 2013-02-24 10:06:15 
      [created] => 2013-02-24 10:06:15 
      ), 
     'Product' => array 
      (
       [0] => array 
        (
        ... 
        ), 
       [1] => array 
        (
        ... 
        ) 
      ) 
    ) 
) 

?> 

请纠正我,如果我错了,谢谢!

+0

显然,如果遵循约定,提供'Category.id'条件将消除limit => 1的需要,因为id始终是唯一的并且只返回1个结果。 – 2013-03-27 04:00:14

0

或者,如果你想“产品”的样子:

<?php 

'Product' => array 
    (
     [0] => array 
      (
       'Product' => array 
        (
         ... 
        ) 
      ) 
    ) 

?> 

从类型模型获取数据时,您需要手动获取相关数据,例如:

<?php 

$this->Category->contain(); 
$cats = $this->Category->find('all'); 
foreach ($cats as &$cat) { 
    $this->Category->Product->contain(); // You have to contain for each find. 
    $cat['Product'] = $this->Category->Product->find('all', 
     array(
      'conditions' => array(
       'Product.category_id' => $cat['Category']['id'] 
      ) 
     ) 
    ); 
} 

?>