可以在Yii中修复一个外键而不必在数据库中设置它? [英] Possible to Fix a Foreign Key in Yii without having set it up in the Database?

查看:156
本文介绍了可以在Yii中修复一个外键而不必在数据库中设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我希望我的产品

我的数据库GUI使用phpMyAdmin,并且连接到我的网站上的Yii Framework。例如,code>表中有一个外键 department_id ,它是对 id 字段的引用我的部门表。不幸的是,我目前没有设置在phpMyAdmin中正确设置外键,所以 department_id 只是一个索引字段。我现在不能改变phpMyAdmin的配置,所以它就像没有外键和关系设施那样卡住了。



有没有办法修改这些表的模型在Yii所以他们链接?我知道Model Class文件中有一个关系函数,它包含这个信息:

  return array('department_id'=> array(self :: BELONGS_TO,'Departments','id'),

我可以不添加类似于上面的东西吗?是否有更多的legwork?现在是固定的(因为在phpMyAdmin静态,没有更正)?

干杯

解决方案

如果我没有弄错,不需要让mySql为他们强制外键关系仍然在Yii中工作。在mySql中设置FK约束可以确保数据库的完整性,但是我不认为Yii在运行时真的使用了这个。



当最初运行yiic(Gii)来构建项目时,我认为它看起来在DB中建立了模型中的正确关系,但是之后它不会使用它们。
Yii然后使用表格关系的这个知识(来自yiic)通过提供访问关系数据的快捷方法,确保您不会违反mySql约束并获得丑陋的SQL错误等,从而使您的生活更轻松。但是,仍然可以使用Yii关系逻辑,而不受底层的SQL约束。 唯一的问题是,如果Yii搞砸了,并且指定了一个不存在的FK,那么你的数据库将不会捕获这个错误(你的数据完整性将更容易出错)。 b
$ b

要将您的产品链接到部门,只要确保您在产品中有一个department_id字段(听起来就像您这样做)。然后像这样添加一个关系规则到产品:

 'department'=>数组(self :: BELONGS_TO,'Department','department_id'),



 'products'=> array(self :: HAS_MANY,'Product','department_id'),

现在你应该可以使用像正常的关系:

  $ myProductModel-> department; //返回所引用的
$ myDepartmentModel->产品的部门模型; //返回部门中所有产品的型号

祝你好运,让我知道如果我是基地的方式,它不适合你!


I'm using phpMyAdmin for my database GUI and it's connecting to Yii Framework on my website.

I wish for my products table for instance, to have a foreign key department_id which is a reference to the id field of my departments table. Unfortunately, I don't currently have the facility to set the foreign key up properly in phpMyAdmin, so department_id is just an indexed field. I cannot change the configuration of phpMyAdmin at the moment so it's stuck like it is without a foreign key and relationship facility.

Is there a way to modify the Models of these tables in Yii so they link? I know there is a relations function inside the Model Class file that holds this information:

return array('department_id' => array(self::BELONGS_TO, 'Departments', 'id'),

Could I not just add something similar to the above? Is there more legwork? Is it now fixed (as in static, not corrected) because of phpMyAdmin?

Cheers

解决方案

If I'm not mistaken, you don't need to have mySql enforcing foreign key relationships for them to still work in Yii. Setting up FK constraints in mySql ensures proper database integrity, but I don't think Yii actually uses that at runtime.

When initially running yiic (of Gii) to build the project I think it looks at the DB to build the right relations in the Model, but it doesn't use them after that.

Yii then uses this knowledge (from yiic) of the table relationships to make your life easier by providing shortcut methods to access relational data, and to ensure you don't violate mySql constraints and get ugly SQL errors, etc. But you can still use Yii relation logic without the underlying SQL constraints. The only problem will be that if Yii messes up and assigns a non-existing FK or something, your database will not catch this error (your data integrity will be more error prone).

To link your products to departments, just make sure you have a department_id field in the Product (which it sounds like you do). Then add a relation rule like so to Product:

'department' => array(self::BELONGS_TO, 'Department', 'department_id'),

And in your Department model:

'products' => array(self::HAS_MANY, 'Product', 'department_id'),

Now you should be able to use the relation like normal:

$myProductModel->department; // returns the model of the Department referenced
$myDepartmentModel->products; // returns the models of all Products in the department

Good luck, and let me know if I am way off base and it doesn't work for you!

这篇关于可以在Yii中修复一个外键而不必在数据库中设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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