Will I suffer consequences later if I add FKs with ON DELETE CASCADE and such?
如果我使用ON DELETE CASCADE添加FK,我会在以后遇到后果吗?
If not, what naming convention should I be using for FKs within MySQL for CakePHP?
如果没有,我应该在MySQL中为CakePHP使用什么命名约定用于FK?
2 个解决方案
#1
You can see the naming conventions laid out here.
您可以在此处查看命名约定。
Cake handles FK/relationships in the code, based on your model associations and implied associations by naming conventions. You can add an extra layer of "enforcement" by defining FK relationships on the database level. If your database honours these, it's harder to shoot yourself in the foot, but it's not necessary. It adds the extra overhead of keeping the relationships in sync in Cake's models and in the database.
Cake根据您的模型关联和命名约定隐含的关联来处理代码中的FK /关系。您可以通过在数据库级别定义FK关系来添加额外的“强制执行”层。如果你的数据库尊重这些,那么你就更难自拔,但这并不是必需的。它增加了在Cake的模型和数据库中保持关系同步的额外开销。
#2
In CakePHP, it doesn't matter if you specify FKs in the database level, however if you do, it would act as you would expect in typical database operations.
在CakePHP中,如果在数据库级别指定FK并不重要,但是如果这样做,它将按照您在典型数据库操作中的预期行事。
If you have 2 tables - students and courses where each student belong to a course, you can state it this way:
如果您有2个表 - 学生和每个学生都属于某个课程的课程,您可以这样说:
<?php
class Student extends AppModel {
var $name = 'Student';
var $belongsTo = array(
'Course' => array(
'className' => 'Course',
'foreignKey' => 'course_id'
)
);
}
?>
The convention is to append "_id" at the back of the singular class name of the model it belongs to.
惯例是在它所属的模型的单数类名称的后面附加“_id”。
If you use CakePHP's naming conventions, you can just state:
如果您使用CakePHP的命名约定,您可以只说:
<?php
class Student extends AppModel {
var $name = 'Student';
var $belongsTo = array('Course');
}
?>
#1
You can see the naming conventions laid out here.
您可以在此处查看命名约定。
Cake handles FK/relationships in the code, based on your model associations and implied associations by naming conventions. You can add an extra layer of "enforcement" by defining FK relationships on the database level. If your database honours these, it's harder to shoot yourself in the foot, but it's not necessary. It adds the extra overhead of keeping the relationships in sync in Cake's models and in the database.
Cake根据您的模型关联和命名约定隐含的关联来处理代码中的FK /关系。您可以通过在数据库级别定义FK关系来添加额外的“强制执行”层。如果你的数据库尊重这些,那么你就更难自拔,但这并不是必需的。它增加了在Cake的模型和数据库中保持关系同步的额外开销。
#2
In CakePHP, it doesn't matter if you specify FKs in the database level, however if you do, it would act as you would expect in typical database operations.
在CakePHP中,如果在数据库级别指定FK并不重要,但是如果这样做,它将按照您在典型数据库操作中的预期行事。
If you have 2 tables - students and courses where each student belong to a course, you can state it this way:
如果您有2个表 - 学生和每个学生都属于某个课程的课程,您可以这样说:
<?php
class Student extends AppModel {
var $name = 'Student';
var $belongsTo = array(
'Course' => array(
'className' => 'Course',
'foreignKey' => 'course_id'
)
);
}
?>
The convention is to append "_id" at the back of the singular class name of the model it belongs to.
惯例是在它所属的模型的单数类名称的后面附加“_id”。
If you use CakePHP's naming conventions, you can just state:
如果您使用CakePHP的命名约定,您可以只说:
<?php
class Student extends AppModel {
var $name = 'Student';
var $belongsTo = array('Course');
}
?>