2012-07-05 46 views
1

我有一個模型,我想檢查更多的插件,如果插件加載,然後將模型插入到人模型的插件。我使用這種方法?但在模型和行動上有不同的結果。 另一種方法比構建更多的插件檢查更好。cakephp附加更多屬於

class Comment extends AppModel { 

/** 
* @see Model::$belongsTo 
*/ 
    public $belongsTo = array(
     'Content' => array(
      'className' => 'Content', 
      'foreignKey' => 'object_id', 
      'conditions' => array(
       'Comment.object_id = Content.id', 
      ) 
     ), 
    ); 

/** 
* @see Model::__construct 
*/ 
    public function __construct($id = false, $table = null, $ds = null) { 
     // parent 
     parent::__construct($id, $table, $ds); 

     // check for newsstudio 
     if (CakePlugin::loaded('NewModel')) { 
      $this->bindModel(
       array('belongsTo' => array(
        'NewModel' => array(
         'className' => 'NewModel.NewModel', 
         'foreignKey' => 'object_id', 
         'conditions' => array(
          'Comment.object_id = NewModel.id', 
         ) 
        ) 
       ) 
      )); 
     } 

     var_dump($this->belongsTo); // correct! NewModel added to blongsto 
    } 
} 

// but in action during use. Plugin loaded but 
var_dump($this->Comment->belongsTo); // incorrect! just `Content` added 

回答

0

考慮你正在做它在__construct,你可能也只是在調用這將節省一些CPU週期,你是不是做一個額外的方法調用父前添加到$belongsTo財產。

class Comment extends AppModel { 

/** 
* @see Model::$belongsTo 
*/ 
    public $belongsTo = array(
     'Content' => array(
      'className' => 'Content', 
      'foreignKey' => 'object_id', 
      'conditions' => array(
       'Comment.object_id = Content.id', 
      ) 
     ), 
    ); 

/** 
* @see Model::__construct 
*/ 
    public function __construct($id = false, $table = null, $ds = null) { 
     // check for newsstudio 
     if (CakePlugin::loaded('NewModel')) { 
      $this->belongsTo['NewModel'] = array(
         'className' => 'NewModel.NewModel', 
         'foreignKey' => 'object_id', 
         'conditions' => array(
          'Comment.object_id = NewModel.id', 
         ) 
        ); 
     } 
     parent::__construct($id, $table, $ds); 
    } 
}