【问题标题】:CakePHP same array from the direct and associated modelCakePHP 相同的数组来自直接和关联模型
【发布时间】:2013-02-24 16:07:13
【问题描述】:

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

CategoryhasMany 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
                )

        )
    )
)

【问题讨论】:

    标签: cakephp model cakephp-1.3


    【解决方案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 - 但是最好不要在控制器中进行“查找” - 在模型中创建一个方法,然后从控制器调用它,而不是 $this->Product->find(),而是 @987654330 @(或任何你想称呼它的名字)。 (阅读更多关于“胖模型,瘦控制器”的原因/示例......等)。

    【讨论】:

    • 从关联模型中获取数据总是合并数组。用find('all')findById 完成都没有关系。
    • @Constantin.FF - 如果您尝试获取相同的数据,为什么要从 2 个不同的模型运行查询?在您的 Product 模型中创建一个方法,并在您需要数据的任何时候/每次都调用它。 (另外,如果这实际上是一个元素,请查看 requestAction)。
    • 我看到了你的例子。 $this->Category->Product->find('all'); 肯定会工作,但我已经做了一个查询 ($this->Category->findById('12')),其中包含所需的数据,因为 hasMany 关系。
    • @Constantin.FF - 如果你在问什么,我不明白,但你应该看看Containable
    【解决方案2】:

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

    如有错误请指正,谢谢!

    【讨论】:

    • 显然,如果您遵循约定,提供 'Category.id' 条件将消除对限制 => 1 的需要,因为 id 将始终是唯一的并且只返回 1 个结果。跨度>
    【解决方案3】:

    或者,如果您希望“产品”看起来像:

    <?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']
                )
            )
        );
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多