【问题标题】:deep association between models模型之间的深度关联
【发布时间】:2013-12-26 21:21:12
【问题描述】:

大家好,我是 cakephp 新手 现在我面临一个小问题 情况是这样的

我有一家商店有许多目录,它与许多产品相关,每个产品都有一个类别 我想通过商店把它们全部拿走 我不知道该怎么做 尝试使用 hasMANy 只会给我 ids

相反,有什么方法可以让商店在其中包含目录数组每个目录都有产品数组,其中包含一个类别数组 谢谢

【问题讨论】:

  • 我在移动设备上,现在无法写出准确的答案,但您应该尝试在每个模型中声明 1 级关联并了解可包含的行为。

标签: php cakephp cakephp-2.0


【解决方案1】:

好的,我现在在我的电脑上:)。

在 ShopModel 中:

class ShopModel extends AppModel {

    public $hasMany = array(
        'Catalog' => array(
            // binding params here...
        ),
    );

}

在目录模型中:

class CatalogModel extends AppModel {

    public $hasMany = array(
        'Product' => array(
            // binding params...
        ),
    ),

}

...然后继续...

如果你不想在所有动作中获取过多的数据,你应该在AppModel中设置:

class AppModel extends Model {

    public $recursive = -1;

}

在您调用具有关联的查找函数的控制器操作中:

$this->Shop->Behaviors->load('Containable');
$big_array = $this->Shop->find('all', array(
    'conditions' => array(
        //...
    ),
    'contain' => array(
        'Catalog' => array(
            'Product' => array(
                // etc, you get the point :)
            ),
        ),
    ),
));

还可以声明 $belongsTo 关联,这样您就可以从任何地方访问任何内容,如下所示:

$this->Catalog->Behaviors->load('Containable');
$big_array = $this->Catalog->find('all', array(
    'conditions' => array(
        //...
    ),
    'contain' => array(
        'Product' => array(
            // ...
        ),
        'Shop' => array(
            // ...
        ),
    ),
));

编辑

我看到你有一个 Product->Category 关系,我猜它是用 $belongsTo 定义的。如果您进行上述查询,您将获得大量重复查询(许多产品中的相同类别)。您可以使用$this->Category->find('list'),但我经常发现这不合适,因为它只返回一个字段(如果有人知道如何使用列表类型获取更多字段,我将不胜感激)。为此,我的解决方法是在 Category 模型中创建一个自定义函数,如下所示:

class Category extends AppModel {

    public function getSorted ($options = array()) {
        $temp= $this->find('all', $options);
        $output = array();
        foreach ($temp[$this->alias] as $row) {
            $output[$this->alias][$row['id']] = $row;
        }
        unset($temp);
        return $output;
    }

}

然后在控制器中我会声明两个数组,一个没有类别关联的大数组和一个类别列表:

$this->loadModel('Category');
$this->set('categories', $this->Category->getSorted());

这样,我可以在视图中的任何需要的位置按类别 ID 获取所需的类别行。

【讨论】:

    【解决方案2】:

    不要使用 CakePHP 关联,它们不能很好地处理复杂的关系,你以后会遇到问题......而是动态创建所有连接查询......我在下面给你一个例子:

    在 Shop 模型中创建一个函数,并加入目录和产品,如下所示:

    $options = array(
            'conditions' => array('Product.id'=>9),
            'joins' => array(
                array(
                    'alias' => 'Catalog',  
                    'table' => 'catalogs',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Catalog.product_id = Product.id',
                    ),
                ),
                array(
                    'alias' => 'Product',  
                    'table' => 'products',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'Shop.id = Product.shop_id',
                    ),
                )
            ),
            'fields' => array('Product.*'),
            'group' => array('Product.id')
        );
    
     $returnData = $this->find('all',$options);
    

    这将使编码更容易一些,并且您可以摆脱关联!

    【讨论】:

    • 我不知道您是如何得出关联不利于处理复杂关系的说法。确实,在某个点之外,它无法自动为您提取数据,而不会在数据库上过于繁重,或者某些类型的关联无法超出复杂点,但除了您提供随时手动加入的建议之外似乎最不切实际。
    • 在飞行中听起来像是一个糟糕而混乱的想法。为什么你认为 cakePHP 关联不适合复杂的关系?
    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多