0

我在 CakePHP 1.3 中与 Categories -> Products 有简单的模型关系

Category有很多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
                )

        )
    )
)
4

3 回答 3

1

您的一个发现是 a find('all'),另一个是 a 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 - 但是如果你不在你的控制器中做你的“发现”会更好 - 在你的模型中创建一个方法,然后从你的控制器中调用它,而不是$this->Product->find(),它会是$this->Product->getProducts()(或者你想调用它的任何东西)。(阅读更多关于“胖模型,瘦控制器”的原因/示例......等)。

于 2013-02-24T16:09:37.523 回答
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']
            )
        )
    );
}

?>
于 2013-03-27T04:07:13.220 回答
0

戴夫是对的,不同之处在于您使用的方法...即使您声称始终合并关联数据,但您在“产品”模型上的发现不是关联数据,因此格式将始终不同。

我来这里有一段时间了,我已经注意到戴夫知道他的东西。:) 我同意胖模型/瘦控制器范式,以实现干净、高效的代码。

如果你改变了:

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

?>

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

于 2013-03-27T03:56:44.973 回答