I have three tables: Users, Profiles, and Friends.
They are connected like so:
public $name = 'User';
public $hasOne = 'Profile';
public $hasMany = array(
'Post',
'Answer'
);
public $hasAndBelongsToMany = array(
'User'=>array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
and the profile model is just a belongsTo user so I have not shown that, and the friends model is virtual by doing the association in the user model.
So to get a list of friends for a user. I pass the username like so in the friends controller:
public function index( $username )
{
$friends = $this->User->find('first',
array(
'conditions'=>array(
'User.username'=>$username
),
'contain'=>array(
'Profile',
'Friend'=>array(
'conditions'=>array(
'Friend.status'=>1
),
'contain'=>'Profile'
)
)
)
);
$this->set('friends', $friends);
}
Which should pull the data for the friends and there associated Profile info!
However I get this error: Model "Friend" is not associated with model "Profile" [APP/Cake/Model/Behavior/ContainableBehavior.php, line 339] so something isn't working correctly...
Can anyone help?
I tried changing the name of he hasAndBelongsToMany to Friends in the model but that throw an error that the table wasn't unqiue... so that's not the solution either.
Tables:
**users**
id
username
password
**profiles**
id
name
user_id
**friends**
id
user_from
user_to
status
If I change the Model to become:
public $hasAndBelongsToMany = array(
'Friend'=>array(
'className' => 'Friend',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
then I get this error:
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'Friend'
SQL Query: SELECT `Friend`.`id`, `Friend`.`user_from`, `Friend`.`user_to`, `Friend`.`created`, `Friend`.`status`, `Friend`.`id`, `Friend`.`user_from`, `Friend`.`user_to`, `Friend`.`created`, `Friend`.`status` FROM `db52704_favorr`.`friends` AS `Friend` JOIN `db52704_favorr`.`friends` AS `Friend` ON (`Friend`.`user_from` = 6 AND `Friend`.`user_to` = `Friend`.`id`)
Models and aliases of related models should be unique. In your case the problem is:
public $name = 'User';
...
public $hasAndBelongsToMany = array(
'User'=>array(
...
If friends is the join table name, than you could use Friend as the alias of the friend User.
Second thing is you're trying to contain both User => Profile and User => Friend => Profile which is probably also problematic.
In my experience, deep contains can get quite inefficient. You might consider dropping the Friend's Profile from the contain, and then extracting the friend user ids and getting the profiles separately.
Your Friend model has no user_id and thats why User model can't make contact with Friend. Try with the following:
public function index( $username )
{
$this->User->Behaviors->attach('Containable');
$friends = $this->User->find('first',
array(
'conditions'=>array(
'User.username'=>$username
),
'contain'=>array(
'Profile',
'Friend'=>array(
'conditions'=>array(
'Friend.status'=>1
)
)
)
)
);
$this->set('friends', $friends);
}
I think you are looking for something like this:
User.php
public $hasAndBelongsToMany = array(
'Friend'=>array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_from',
'associationForeignKey' => 'user_to'
)
);
This will alias the User table as a Friend to itself in a habtm relationship. To keep with convention, the join table should be called friends_users with user_id and friend_id keys.
Here's the result I get with a test app I made (just add your conditions):
$this->User->contain(array(
'Profile',
'Friend' => array(
'Profile',
),
));
debug($this->User->find('all', array('conditions' => array('User.id' => 1))));
array(
(int) 0 => array(
'User' => array(
'id' => '1',
'name' => 'user1'
),
'Profile' => array(
'id' => '1',
'user_id' => '1'
),
'Friend' => array(
(int) 0 => array(
'id' => '2',
'name' => 'user2',
'FriendsUser' => array(
'id' => '1',
'user_id' => '1',
'friend_id' => '2'
),
'Profile' => array(
'id' => '2',
'user_id' => '2'
)
)
)
)
)
Related
It's probably a very basic problem, but I can't wrap my head around it how to solve this properly.
In my application, I've got users who have multiple addresses with different purposes. For example, a user can have an Address and an InvoiceAddress. I want to store both addresses in the same table (the Address model) and then assign them to one User.
The respective models look something like:
Address:
public $belongsTo = array(
'Address' => array(
'className' => 'Address',
),
'InvoiceAddress' => array(
'className' => 'Address',
),
);
And the User:
public $hasOne = array(
'Address' => array(
'className' => 'Address',
),
'InvoiceAddress' => array(
'className' => 'Address',
),
);
When a user would have one address, I'd add a user_id as foreign key to the Address model and be done with it. But obviously, something has to be done differently for CakePhp to be able to distinguish between the address and the invoice address.
In my mind the solution is something like adding a address_id and invoice_address_id to the User table, but I can't seem to get it working in any way.
If you want to add in your user table the related fields then it is better to add them in the following form address_user_id and the invoice_address_user_id. Then you must have a belongsTo relation in your user model that looks like:
public $belongsTo = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'address_user_id',
),
'InvoiceAddress' => array(
'className' => 'Address',
'foreignKey' => 'invoice_address_user_id',
),
);
So when you execute the following command $this->User->find('all'); the result will be
$result = array(0 =>
array('User' => array(...),
'Address' => array(...), // it will bring only one address row
'InvoiceAddress' => array(...) // it will bring only one address row
),
1 => ...
2 => ...
);
Then in your address Model the relationships you must add are:
public $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'address_user_id',
),
'InvoiceUser' => array(
'className' => 'User',
'foreignKey' => 'invoice_address_user_id',
)
);
I am having problems with the CakePHP belongsTo and hasAndBelongsToMany relationships in CakePHP 2.x
Example situation
Table users
id
organisation_id
Table organisations
id
name
Table user_organisation_permissions
id
user_id
organisation_id
UserModel
hasAndBelongsToMany(Organisation);
belongsTo(Organisation)
A user belongs to one organisation but it has permissions on multiple organisations, resulting in the following conflict:
$aUser = $this->User->findById(1);
print_r($aUser);
// Output
# With the belongsTo relation
array(
'User' => array(
'id' => 1,
'organisation_id' => 1
'name' => 'Test User'
),
'Organisation' => array(
'id' => 1,
'name' => 'Test organisation'
)
);
# With the hasAndBelongsToMany relation
array(
'User' => array(
'id' => 1,
'organisation_id' => 1
'name' => 'Test User'
),
'Organisation' => array(
1 => array(
'id' => 1,
'name' => 'Test organisation'
),
2 => array(
'id' => 1,
'name' => 'Test organisation'
)
)
);
# When both relations are enabled it doesn't work
Does anybody have a solution for this conflict?
Is there a "native" CakePHP solution for this conflict?
The answer is actually in the CakePHP 2.x Cookbook.
Multiple relations to the same model
There are cases where a Model has more than one relation to another Model. For example, you might have a Message model that has two relations to the User model: one relation to the user who sends a message, and a second to the user who receives the message. The messages table will have a field user_id, but also a field recipient_id. Now your Message model can look something like:
class Message extends AppModel {
public $belongsTo = array(
'Sender' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Recipient' => array(
'className' => 'User',
'foreignKey' => 'recipient_id'
)
);
}
Source: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#multiple-relations-to-the-same-model
In kid model i have defined this below HABTM relation.
public $hasAndBelongsToMany = array(
'Pics' => array(
'className' => 'Pic',
'joinTable' => 'pics_kids',
'foreignKey' => 'kid_id',
'associationForeignKey' => 'pic_id'
)
);
Pics table store details of pics like comment, pic_url and date and pics_kids table have pic_id and kid_id id's that relate pic and kids . If i have to retrieve record based on kid_id and date(date of pic in pics table) how to retrieve it?
$kid_pics=$this->kid->find('all', array(
'conditions' => array(
'kid.id' => 1,
'Pic.date' => '26/07/2014'
)
));
if i use above query i get error that no column Pic.date in where claue.
Add a condition to your HATBM relationship dynamically:
$this->Kid->hasAndBelongsToMany['Pic']['conditions'] = array(
'Pic.date' => '26/07/2014'
) ;
$kid_pics = $this->Kid->real(null, 1) ;
You can use built-in Containable Behavior
$this->Kid->Behaviors->load('Containable');
$kid_pics = $this->Kid->find('all', array(
'conditions' => array(
'Kid.id' => 1
),
'contain' => array(
'Pic' => array(
'conditions' => array(
'Pic.date' => '26/07/2014'
)
)
)
));
I am trying to display a field from a 3rd table in a relationship, and after checking posts here and the docs, I am still stuck.I have 3 models all related in some way. I have found similar posts here but I am still not getting it to work. I am learning so sorry if I have missed this somewhere in the docs but I have read a fair bit and have tried a lot. I am guessing too much now on this so I need help.
1)Tutorsession - belongsto teachers,
2) Teacher -has 1 user,hasmany tutorsessions
3) User- has 1 teacher, //////I want to display a field from this table given I display tutorsessions
In controller
$this->set('tutor',
$this->Tutorsession->find('first',
array(
'conditions' => array('Teacher.user_id' => $id),
'contain' => 'User.username'
)
)
); //////////no error but no results
from view echo '<td>'. $item['User']['username'].'</td>'; ///////error user undefined
The tutorsession automatically gets rows from teacher table but not user table witht he model setup on a findall.
I want to display a username from the user table. I display the tutorsession table , I then can display the teacher table with the model association but I cant go from the teacher table to the user table to get a user name from the user id as the common field. I have checked the docs below and I am not sure why my code isnt ble to display a username from users table.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
update: here are the models
class User extends AppModel {
public $hasOne = array(
'Teacher' => array(
'className' => 'Teacher',
'dependent' => true
)
class Tutorsession extends AppModel
{
public $name='Tutorsession';
public $belongsTo = array(
'Teacher' => array(
'className' => 'Teacher',
'foreignKey' => 'teacher_id'
)
class Teacher extends AppModel
{
public $name='Teacher';
public $hasMany = array('Tutorsession');
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
Add containable behavior in your Model
public $actsAs = array('Containable');
Execute following query In controller
$contain = array('Teacher' => array('User'));
$this->set('tutor', $this->Tutorsession->find('first',array(
'conditions' => array('Teacher.user_id' => $id),
'contain' => $contain
)));
Try this:
$this->Tutorsession->recursive = 2;
$this->Tutorsession->Teacher->contain('User');
$this->set('tutor',
$this->Tutorsession->find('first',
array(
'conditions' => array('Teacher.user_id' => $id)
)
)
);
$tutor=$this->Teacher->find('first',
array(
'conditions' => array('Teacher.user_id' => $id)
)
);
debug($tutor); exit();
Your result similar:
'Teacher' => array(
'id' => '1',
'name' => 'abc',
'created' => '1397040385',
'updated' => '1397725860'
),
'User' => array(
'id' => '4',
'name' => 'administrators',
'created' => '1397032953',
'modified' => '1397032953'
),
'Tutorsession' => array(
0=>array(
'id' => '3',
'name'=>'abc',
'created' => '1400729137'
),
1=>array(
'id' => '4',
'name'=>'abc',
'created' => '1400729137'
)
)
Ensure your models are correct
As I understood from the cakephp-documentation, one of the advantages of the 'containable'-Behavior is being able to fetch fewer data if you need fewer data...
But that doesn't seem to work in my case of a connection between users and usergroups.
My associations look like:
Group
hasMany: Membership
User
hasMany: Membership
Membership
belongsTo: User, Group
(I'm not using HABTM, instead use the Model 'Membership' in between to join users and groups).
All Models implement the 'Containable'-Behavior.
Now I want to get all the members of a group with a certain id, only their ids and mail-addresses. My query is built like that:
$members = $this->Membership->find('all', array(
'conditions' => array(
'group_id' => $id
),
'contain' => array(
'User' => array(
'fields' => array('id', 'fullName')
),
)
));
But the resulting array looks like:
array(
(int) 0 => array(
'Membership' => array(
'id' => '1',
'group_id' => '1',
'user_id' => '1',
'role' => 'member'
),
'Group' => array(
'id' => '1',
'name' => 'Foo Group'
),
'User' => array(
'password' => '*****',
'id' => '1',
'name' => 'Dr. Foo'
'mail' => 'foo#bar.baz',
'role' => 'admin'
)
)
)
So there are definietely more fields fetched than I wanted to... (it's the same thing btw wenn I set the 'contain'-key to:
'contain' => array(
'User.fullName', 'User.id'
)
Am I using the containable-behavior wrong?
Your models don't seem to be acting containabl-y at all. Have you set your models to act as containable?
class Post extends AppModel {
public $actsAs = array('Containable');
}
If so, maybe the problem is with the recursion (to avoid getting the Group array with the query). Containable behavior should handle the recursion level on its own, but try setting it on the AppModel just to be sure
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
Your first attempt
'contain' => array(
'User' => array(
'fields' => array('id', 'fullName')
),
)
looks good in terms of syntax, so it probably the actAs thing.
Also, for debugging also, try
$this->Membership->contain('User');
$this->Membership->find('all', array(
'conditions' => array(
'group_id' => $id
));
and see if you get the expected results that way.