The Scenario: I have a CakePHP App, With a Plugin called Fruit. The Fruit Plugin contains a route to the TagsController.php with an Action of index which points to index.ctp
The Tags Model has one association. user_id
The Problem: Even with me adding a joins option to the Model->find(‘all’… the [‘User’] index is undefined. I am not sure what to do, here is my code….
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';
}
}
Tags Model
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' => ''
)
);
User Model
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' => ''
),
);
I have been stuck on this problem for several hours now, so any fresh ideas on what I can try are greatly appreciated. I have combed through stackoverflow pretty good and I am just going in circles, I need a fresh pair of eyes on this. Any ideas at all would be awesome.
Thank you!
- Jeff
I think you have tried a lot and made it more complicated, try this code it will give you desired output.
in user model:
in controller:–