CakePHP来自直接和关联模型的相同数组

时间:2013-02-24 16:07:14

标签: cakephp model cakephp-1.3

我在CakePHP 1.3中有类别的简单模型关系 - >产品

Category hasMany Products

我在不同控制器中获得的数据阵列之间存在细微差别。在Product控制器中获取相关模型时,Categories数据位于主产品数组中,并且在Products中获取时会分开。

例如获取'Product1'

Categories - $ category ['Product'] [0] ['title']

并在Products - $ product [0] ['Product'] ['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
                )

        )
    )
)

3 个答案:

答案 0 :(得分:1)

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

这两种格式都以不同的格式返回数据,因为find('first')知道您只需要一个项目,而find('all')是一组未知的项目。 < / p>

只需使用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 - 但是如果你没有在你的控制器中找到你的“发现”,那就更好了 - 在你的模型中创建一个方法,并从你的控制器调用它,而不是$this->Product->find(),它将是$this->Product->getProducts()(或任何你想称之为的东西)。 (阅读更多关于“脂肪模型,瘦的控制器”的原因/例子......等等)。

答案 1 :(得分:0)

Dave是对的,区别在于你正在使用的方法......即使你声称关联的数据总是被合并,你在'Product'模型上的find也不是关联的数据,所以格式总是不同的

我已经在这里待了一会儿,我已经注意到戴夫知道他的东西了。 :) 我同意胖模型/瘦子控制器范例,以获得干净,高效的代码。

如果你改变了:

<?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
                    (
                    ...
                    )
            )
    )
)

?>

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

答案 2 :(得分: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']
            )
        )
    );
}

?>