【问题标题】:CakePHP Model Extended AssociationCakePHP 模型扩展关联
【发布时间】:2012-03-13 03:37:06
【问题描述】:

查看 InventoryCategory 摘要时,我可以获取 Inventory,但不能获取 InventoryImages。 CakePHP 报告的错误是[ Model "Inventory" is not associated with model "InventoryImage" ]。这些是我正在使用的模型:

class InventoryCategory extends InventoriesAppModel {
public $hasMany = 
    array(
          'Inventory' 
        , 'InventoryCategoryImage' => array 
            (
                  'className'   => 'Media.MediaImage' 
                , 'foreignKey'  => 'foreign_key' 
                , 'conditions'  => array(
                              'InventoryCategoryImage.model' => 'InventoryCategoryImage' 
                            , 'InventoryCategoryImage.group' => 'Inventory Category Image' 
                            , 
                  )
                , 'dependent'   => true 
                , 
            ) 
        , 
    );

public function containedModels() 
{
    $contain = array(
              'Inventory' 
            , 'InventoryCategoryImage' 
            , 
    );
    return $contain;
}

}

class Inventory extends InventoriesAppModel {

public $belongsTo = 
    array(
          'User' 
        , 'InventoryCategory' 
        , 
    );

public $hasMany = 
    array(
          'InventoryImage' => array
            (
                  'className'   => 'Media.MediaImage' 
                , 'foreignKey'  => 'foreign_key' 
                , 'conditions'  => array(
                              'InventoryImage.model' => 'InventoryImage' 
                            , 'InventoryImage.group' => 'Inventory Image' 
                            , 
                  )
                , 'dependent'   => true 
                , 'order'   => 'InventoryImage.rank ASC, InventoryImage.id ASC' 
            ) 
        , 
    );

public function containedModels() 
{
    $contain = array(
              'User' 
            , 'InventoryCategory' 
            , 
    );
    return $contain;
}

}

【问题讨论】:

  • 'recursive' => 2 在你的 find() 调用中吗?另外,请发布出现该错误的 find 调用。
  • 没有。我不使用递归。改为包含。
  • codepublic function view() { if( !$this->params['id'] ) { $this->redirect( $this->referer() ); } $conditions = ( is_numeric( $this->params['id'] ) ) ?数组( $this->modelClass . '.id = ' => $this->params['id'] ) : 数组( $this->modelClass . '.slug = ' => $this->params['id '] ); $item = $this->{$this->modelClass}->find( 'first' , array( 'conditions' => $conditions , 'contain' => $this->{$this->modelClass}->包含模型())); $this->set(compact('item')); }code

标签: cakephp model model-associations


【解决方案1】:

问题在于,通过在 InventoryCategory 中声明 $hasMany,您可以从其父类中替换并删除 $hasMany 数组。

因此,$hasMany 声明还应包含父关联,以使其正常工作。

另一种方法是将新关联附加到父级的 $hasMany 数组。
这里的问题是:使用什么回调。我不知道(我也在寻找答案)。到目前为止,我认为public function __construct() 是最合适的。

像这样:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct();

    // adding Blog-specific Card-association.
    // Blog is a class extending some other class that does not know about the Class association.
    $this->belongsTo['Card'] = array(
        'className' => 'Card',
        'foreignKey' => 'card_id'
    );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多