Foreign Key in 2 tables (CakePHP) - php

I have 3 tables: staffs, users and threads.
Both the staffs and users can create threads and a thread belongs to a user.
In thread, there is a user_id (FK - BelongsTo). The question is, How can I identify who created the thread. If I create a field called creator_id I still don't know If it's a staff or a user who created the thread.
I also tried a different approach by creating a field called creator (enum('staff', 'user)). The limitation with this method is, when I am using Thread->find(), I couldn't figure out a way to retrieve the information of the creator.
What options do I have to achieve what I want?

Do the Model Association as the following in Model for Thread. Create a field called creator_id and store the id of the user/staff who created the thread,
public $belongsTo = array(
'Client' => array(
'className' => 'Staff',
'foreignKey' => 'creator_id',
'conditions' => array('Thread.creator' => 'staff'),
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'creator_id',
'conditions' => array('Thread.creator' => 'user'),
'fields' => '',
'order' => ''
)
);

Related

Delete record from multiple tables when using a View CakePHP 2.8.x

I am using CakePHP 2.8.x and i'm looking for a way to delete records from 2 tables with 1 delete action.
I created a View from 3 tables: visitors, guests and registrations. These tables are connected through visitor_id.
In the Visitor Model i added the View in $useTable. Now when i press delete on, for example, visitor 1, i want to delete the records from visitor 1's guests and registrations tables and keep the visitor information in the visitor's table
I did found $this->Visitor->delete($id), but what parameters do i need to add to this function, so i can delete just the records from the guests and registrations tables?
If i need to give more information, i'm happy to help! Thx in advance
Update: The tables are associated. These are the models:
Visitor:
public $hasMany = array(
'Guest' => array(
'className' => 'Guest',
'foreignKey' => 'visitor_id',
'dependent' => false,
'exclusive' => false,
),
'Registration' => array(
'className' => Registration',
'foreignKey' => 'visitor_id',
'dependent' => false,
'exclusive' => false,
),
);
public $belongsTo = array(
);
Registration:
public $belongsTo = array(
'Visitor' => array(
'className' => 'Visitor',
'foreignKey' => 'visitor_id',
),
);
Guest:
public $belongsTo = array(
'Visitor' => array(
'className' => 'Visitor',
'foreignKey' => 'visitor_id',
),
there's the deleteAll() function in cake 2 (see the cookbook)
you have to pass to the function an array of conditions. In your case you want to delete all the Guests that have visitor_id = $id (the same for Registration).
$this->Visitor->Guest->deleteAll(array('visitor_id' => $id));
$this->Visitor->Registration->deleteAll(array('visitor_id' => $id));
this will delete all the Guests and the Registrations of the Visitor without touching the Visitor itself
you can try it,its worked for me
public function your_action($id=null){
$this->MainMdel->ConnectedModel1->deleteAll(array('ConnectedModel_id' => $id));
$this->MainMdel->ConnectedModel2->deleteAll(array('ConnectedModel_id' => $id));
}

Cakephp hasManyThrough relationship with user_id being used twice

I have created hasManyThrough table like below:
id user_id friend_id
I also have users table like below:
id username password
I want to have both user_id and friend_id belongsTo relation with user's id column. So I have written below two models:
Friend model
class Friend extends AppModel {
var $name = 'Friend';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Friend' => array(
'className' => 'User',
'foreignKey' => 'friend_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
User model
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array(
'User' => array(
'className' => 'Friend',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Friend' => array(
'className' => 'Friend',
'foreignKey' => 'friend_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
I'm not sure why but when I try to view this via controller in scaffolding, it comes up with 500 server error?? Is there something wrong with the model config that is creating wrong SQL?
OK it seems like error is due to Zend Optimizer according to some forums. This is creating Segmentation Fault when baking!
I cannot turn off this in the third party hosted server so I might have to move to my local server and test so I can see Cake errors.
OK below is the error (it's very long so I'm not going to put all but below gives a bit of an idea):
Fatal error: Maximum function nesting level of '100' reached, aborting! in /usr/share/php/cake/libs/debugger.php on line 248 Call Stack: 0.0011 352048 1. {main}()
Most probably problem is occurring due to nesting of Friend Model construct and which is occurring due to use of same alias of Friend for User and Friend table has well. Here is what happening i guess:
Friend Model being initialized, which in its constructs during model binding initialize User Model.
Now when User Model is initialized, during its binding Process, It Tries to bind Friend Model with alias as User, but due to use of Singleton pattern in Models, Instead of Friend Model instance ClassRegistry returns instance of User, resulting User model binding to itself.
Similarly is the case when User is Initialized first.Change Alias of relations like Friend_2,
or something else, ie Dont repeat Alias in Model relations.
Look at my answer in CakePHP Twitter-clone: Can't get follow-system to work. It`s the same but with friends rather then followers.
You just make a user_users table with a user_id (User ID) field and child_user_id (Friend ID), you can use the user_users table again to make similar relations eg user HABTM followers, user HABTM followers
Sorry for the late response, I thought I posted the answer a day ago but clearly it didn't post.

belongsTo relationship & filtering in cakePHP

I'm a newbie to cakePHP and I've taken on creating a very small "status reporting" project, that would allow a user to report their current status on a project that they were assigned on.
I'm currently using the acl, auth, and session components to allow for multiple tier users to manage one another with an admin creating users, projects, and assigning them to one another. I have also fixed it so that when a user is logged in and goes to add a "status" that their login session automatically takes care of the "user_id" for the status:
$this->data['Status']['user_id'] = $this->Auth->user('id');
What I need help doing now is filter the options for the project that the user can pick to add a "status" to.
I currently have a setup with a table that holds the relationships between users and projects named *projects_users* with *id, project_id, user_id* as fields. But, after baking this setup, when the user is adding a "status" the drop down menu provides all projects rather than strictly the ones they are "assigned to."
I would like to filter the user's options to the projects that they are assigned to. Is there considerable more relationships I should be setting up or is this a pretty easy little function to write? Any help would be greatly appreciated and if I have left out information, I'd be glad to post anything else.
Here is the Status Model setup:
class Status extends AppModel {
var $name = 'Status';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Project' => array(
'className' => 'Project',
'foreignKey' => 'project_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
My User Model looks like this:
var $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $hasMany = array(
'Status' => array(
'className' => 'Status',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
var $hasAndBelongsToMany = array(
'Project' => array(
'className' => 'Project',
'joinTable' => 'projects_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'project_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
In the database, You should have a column called project_id in the user table that is associated with the project table's id column.
Then, in your User model, you should create a hasMany relationship with the Project model.
class User extends AppModel {
var $name = 'User';
var $hasMany = 'Project';
}
Now, when you fetch a particular user, you will get all projects that the user is associated with.

which cakephp relationship should be used for groups of users? can haveMany use array for foreign_ID?

In my tennis application, my 'match' table has two players (obviously). I've used player1_id and player2_id as a way to track the user ids. I haven't included a user_id foreign key though.
There is also a 'user' table where I want to pull player's names from.
Because 'match' has more than one user and users have more than one model, I'm wondering if the following model configuration will work:
I've setup the user model like this:
var $name = 'User';
var $hasMany = array(
'Match' => array(
'className' => 'Match',
'foreignKey' => array( 'player1_id', 'player2_id'),
'dependent' => true,
)
and the match model like this:
var $name = 'Match';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
)
I need to display the user first/last name for each player where user_id = player1_id or player2_id. Will this work? Can a foreign key use an array? And can a model operation use an 'or' when searching?
Or is there a better way to structure a table (like a match, or could be a group meeting) with more than one user?
Cheers,
Paul
I don't think an array as a foreign key on the User > Match relationship would work, but then again I have never tried. The single Match > User relationship with user_id as the foreign won't work though, since, as you said, that foreign id does not exist.
Conceptually the cleanest way would be a proper hasAndBelongsToMany relationship. After all, a match has many players, and players have many matches. So you're really looking at a many-to-many relationship.
To fake it with a belongsTo/hasMany relationship, you would need to do this:
// user model
var $hasMany = array(
'MatchAsPlayer1' => array(
'className' => 'Match',
'foreignKey' => 'player1_id',
),
'MatchAsPlayer2' => array(
'className' => 'Match',
'foreignKey' => 'player2_id',
)
);
// match model
var $belongsTo = array(
'Player1' => array(
'className' => 'User',
'foreignKey' => 'player1_id'
),
'Player2' => array(
'className' => 'User',
'foreignKey' => 'player2_id'
)
);
The ugly part is in the User model, where you'd receive related matches split into ['MatchAsPlayer1'] and ['MatchAsPlayer2']. You could do some trickery in the afterFind callback to merge those, but it's not a nice solution overall. Just go for the hasAndBelongsToMany. :)

How do I query data in CakePHP using HABTM relationships?

I'm working on a CakePHP 1.2 application. I have a model "User" defined with a few HABTM relationships with other tables through a join table.
I'm now tasked with finding User information based on the data stored in one of these HABTM tables. Unfortunately, when the query executes, my condition is rejected with an error about a missing table. Upon inspection it seems that CakePHP is not including any of the HABTM tables in the select statement.
My User HABTM relationship is as follows:
var $hasAndBelongsToMany = array(
'Course' => array(
'className' => 'Course',
'joinTable' => 'course_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'course_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => false,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'School' => array(
'className' => 'School',
'joinTable' => 'school_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'school_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => false,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Team' => array(
'className' => 'Team',
'joinTable' => 'team_members',
'foreignKey' => 'user_id',
'associationForeignKey' => 'team_id',
'conditions' => '',
'order' => '',
'limit' => '',
'uniq' => false,
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
The error is:
SQL Error: 1054: Unknown column
'School.name' in 'where clause'
And finally, the query it is trying to execute
SELECT
`User`.`id`, `User`.`username`, `User`.`password`, `User`.`firstName`, `User`.`lastName`, `User`.`email
`, `User`.`phone`, `User`.`streetAddress`, `User`.`city`, `User`.`province`, `User`.`country`, `User
`.`postal`, `User`.`userlevel`, `User`.`modified`, `User`.`created`, `User`.`deleted`, `User`.`deleted_date
` FROM `users` AS `User` WHERE `User`.`id` = 6 AND `School`.`name` LIKE '%Test%' LIMIT 1
Turn your debug level up to 2 and look at the SQL output. Find the query that your code is generating and you'll notice there are several. The ORM layer in CakePHP doesn't join HABTM related tables in the first query. It gets the results from the first select, then separately fetches the HABTM data for each item. Because the join table is not in the first query, your condition, which is intended for use on a joined table, results in the error you are seeing.
The cook book has a section on HABTM associations and fetching data based on conditions in the HABTM table that will fit your requirements.
From the Cookbook: http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
One option is to search the Tag model (instead of Recipe), which will also give us all of the associated Recipes.
$this->Recipe->Tag->find('all', array(
'conditions' => array('Tag.name' => 'Dessert')));
We could also use the join table model (which CakePHP provides for us), to search for a given ID.
$this->Recipe->bindModel(array('hasOne' => array('RecipesTag')));
$this->Recipe->find('all', array(
'fields' => array('Recipe.*'),
'conditions' => array('RecipesTag.tag_id' => 124) // id of Dessert
));
It's also possible to create an exotic association for the purpose of creating as many joins as necessary to allow filtering, for example:
$this->Recipe->bindModel(array('hasOne' => array('RecipesTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = RecipesTag.tag_id')
))));
$this->Recipe->find('all', array(
'fields' => array('Recipe.*'),
'conditions' => array('FilterTag.name' => 'Dessert')
));
Your table should be called "schools_users" and not "school_members" because it's many-to-many, thus using the plural form in the table name on both sides is appropiate.
You also set the Model ClassName "School" as Alias for the HABTM. You should change that to something more generic like "Schools" as in "User is in and has many SchoolS" to avoid conflicts.
And another hint: Try to find the user "via" the School Model rather than the User Model.
$this->User->Schools->find()
Hope this helps.
FWIW, your join tables do appear to be "oddly named" insofar as they don't follow the convention described here:
http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
In any case, good luck, I remember HABTM being a butt-pain in CakePHP.

Categories