2012-09-13 34 views
2

方案:我有一個CakePHP應用程序,它帶有一個名爲Fruit的插件。果子插件包含指向Index的動作的IndexController.php的路徑.ctpCakePHP插件模型關聯不起作用

標籤模型有一個關聯。 user_id

問題:即使我添加了一個連接選項到模型 - > find('all'... ['User']索引是未定義的,我不知道該怎麼做,這裏是我的代碼....

TagsController

public function index($ajax = false) { 
    $this->Tag->recursive = 2; 
    $tags = $this->Tag->find('all', array('conditions' => array('User.id' => $this->Auth->user('id')), 'joins'=>array(
    array(
     'table'=>'users', 
     'alias'=>'User',    
     'conditions'=>array(
       'User.id=Tag.user_id' 
     ) 
    ), 
    ))); 

    print_r($tags); 

    $this->set('tags', $this->paginate()); 

    if($ajax == 'ajax') 
    { 
     $this->layout = 'ajax'; 
    } 
} 

標籤模型

App::uses('FruitAppModel', 'Fruit.Model'); 
class Tag extends FruitAppModel { 

public $name = 'Tag'; 
public $uses = array('User'); 
public $belongsTo = array(
    'User' => array(

     'className' => 'User', 
     'foreignKey' => 'user_id', 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ) 
); 

用戶模型

App::uses('AppModel', 'Model'); 

class User extends AppModel { 

public $name = 'User'; 
public $displayField = 'username'; 

public $hasMany = array(  
    'Tag' => array(
     'className' => 'Fruit.Tag', 
     'foreignKey' => 'tag_id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    ), 

); 

我一直被困在這個問題上幾個小時了,所以任何新鮮的想法,我可以嘗試是非常感謝。我已經通過stackoverflow很好的梳理,我只是在圈子裏,我需要一個新的眼睛在這個。任何想法都很棒。

謝謝!

  • 傑夫

回答

2

我想你已經嘗試了很多,變得更加複雜,試試這個代碼,它會給你所需的輸出。

在用戶模式:

public $hasMany = array(  
'Tag' => array(
    'className' => 'Fruit.Tag', 
    'foreignKey' => 'user_id', 
    'dependent' => false, 
    'conditions' => '', 
    'fields' => '', 
    'order' => '', 
    'limit' => '', 
    'offset' => '', 
    'exclusive' => '', 
    'finderQuery' => '', 
    'counterQuery' => '' 
), 

在控制器: -

$this->Tag->recursive = 1; 

$this->paginate = array(
    'conditions' => array('Tag.user_id' => $this->Auth->user('id')) 
); 
$data = $this->paginate('Tag'); 
pr($data); 
$this->set(compact('data')); 
+0

這樣做相反的是什麼?如果我想將插件中的模型與屬於「核心」模型? – kappa