问题
First of all i'm sorry about the name of this question i had a hard time figure out what to call it:
Here is my problem i have the following two data tables:
Users
Employees.
These two data-tables are connected in a 1 to 1 relationship
Now both of these has a Model a view and a controller. Now my question is kinda split into two parts first the hard part:
Whenever an employee wants to create a new user (new employee) he has to insert the data into two tables first in the users
table and then after getting the user_id
add a record in the employee
table
the employee table looks like this:
users_idUsers client_id
Where users_idUsers
is the userid
from the user table
How will i go around this? is it possible to add the functions of another model into my employee model?
The second question is kinda an extension of this and is relatively simple.. i want to display all the employees however the view should be joined with the users so that you can see the username of the person in the view.
It should be said that i am quite new to Cake so i hope that some of you will be able to help me out.
回答1:
What you're after is linking two models together. CakePHP has a solid relationship mapper that does all this for you without needing to "take the id from the last insert and do something else with it".
If I'm not mistaken, your problem put into simple terms is:
An Employee hasMany users
A User belongs to one Employee
For a 1-to-many model relationships CakePHP has something called "hasMany": http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany
It's in the same way a user for instance can make many comments; An employee can create many users.
On the flipside of that relationship between a user and a employee, you'd want a "belongsTo" mapping, so you know that the user was added by some employee: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto
This way you can find what employee that added "user X" by searching "through the user", so to speak.
When you have mapped up the models together, things like retrieving data (like findBy: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findby) and managing model relationships becomes much easier as you don't have to think much about how relations are chained together (that's what the model mapping is for as outlined above), but rather on what you want to retrieve.
In turn it allows you to do things like this intuitively
$this->Employee->find('all', array(
'recursive' => 1,
'fields' => array('employee.name, employee.age')
));
or if you wanted to find say what employee that added the user "Sarah".
$this->Employee->User->find('all', array(
'conditions' => array('User.name' => 'Sarah'),
'fields' => 'Employee.*',
'recursive' => 0))
which would give back an associative array with employees.
As a sidenote: The use of recursive = 1 is abit old fashioned. There is something much better called "Containable" that allows you to kind of inject models based on what you need. It's outside the scope of this problem, but I though I'd mention it.
来源:https://stackoverflow.com/questions/18144184/cakephp-working-with-two-controllers-models