I have a table(drugs) which lists all of the drugs in that table. Drugs hasOne french_information and french_information belongsTo Drugs. I would like for the users to be able to click a drug,have information about it displayed and then select french and have it bring up a view to add french information,while displaying the chosen drugs information, with fields to add french info for it and store it in the french_information table. How would I do that?
问题:
回答1:
You wouldn't.
There's no need to have the Mentor data in the Students table unless you have student data to go with it.
When a student is created, you can add it to the students table along with it's corresponding Mentor's ID. If you'd like, you can always add a beforeSave() in your Student model to make sure that mentor doesn't already have a student attached prior to saving.
If for some reason you feel you MUST have empty data in your students table, you can always write some code in any action (to be run once) where you loop through each Mentor, build an array for $student and save it - something like this:
$mentors = $this->Mentor->find('all');
foreach($mentors as $mentor) {
$this->Student->create();
$student = array('mentor_id'=>$mentor['id']);
$this->Mentor->Student->save($student);
}
Or, you could write SQL and run it directly on the database.
回答2:
This might do the trick. You'll end up with empty student_name
fields, though.
INSERT INTO `students` (mentor_id) SELECT id FROM `mentors`;