CakePHP的模型关联表瓦特/数据和新表 [英] CakePHP model associations table w/data and new table

查看:231
本文介绍了CakePHP的模型关联表瓦特/数据和新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表(药品),其中列出该表中所有的药物。药品hasOne french_information和french_information毒品的belongsTo。我想为用户能够点击的药物,有关于它的信息显示,然后选择法语,并将它带来了一个视图来添加法语的信息,同时显示所选择的药品信息,与字段添加为它的法国信息并将其存储在french_information表。我会怎么做呢?

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?

推荐答案

您不会的。

有没有必要在学生表中的导师的数据,除非你有学生数据去用它。

There's no need to have the Mentor data in the Students table unless you have student data to go with it.

当创建一个学生,你可以用它对应的Mentor的ID一起将其添加到学生表。如果你愿意,你可以随时在你的学生模型添加一个beforeSave(),以确保导师已经没有之前节省了学生的连接。

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.

如果由于某种原因,你觉得你必须在你的学生表空的数据,可以随时写一些code中的任何操作(运行一次),您通过每个导师循环,建立$学生一个数组并保存它 - 是这样的:

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);
}

或者,你可以写SQL,并直接在数据库上运行它。

Or, you could write SQL and run it directly on the database.

这篇关于CakePHP的模型关联表瓦特/数据和新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆