原则 1.2:如何防止将约束分配给一对多关系的双方?
有没有办法防止 Doctrine 在一对一关系的双方都分配约束?我尝试将定义从一侧移动到另一侧并使用拥有侧,但它仍然对两个表都施加了约束。当我只希望父表有一个约束时 - 即。父母可能没有关联的孩子。
例如,我本质上想要以下sql模式:
CREATE TABLE `parent_table` (
`child_id` varchar(50) NOT NULL,
`id` integer UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (`id`)
);
CREATE TABLE `child_table` (
`id` integer UNSIGNED NOT NULL auto_increment,
`child_id` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`child_id`),
CONSTRAINT `parent_table_child_id_FK_child_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `parent_table` (`child_id`)
);
但是我得到了这样的结果:
CREATE TABLE `parent_table` (
`child_id` varchar(50) NOT NULL,
`id` integer UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (`id`),
CONSTRAINT `child_table_child_id_FK_parent_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `child_table` (`child_id`)
);
CREATE TABLE `child_table` (
`id` integer UNSIGNED NOT NULL auto_increment,
`child_id` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`child_id`),
CONSTRAINT `parent_table_child_id_FK_child_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `parent_table` (`child_id`)
);
我可以手动删除约束或修改我的访问器以返回/设置集合中的单个实体(使用一对多),但看起来像应该有内置的方法来处理这个问题。
我还使用 Symfony 1.4.4(pear installtion ATM) - 以防它是 sfDoctrinePlugin 问题,而不一定是 Doctrine 本身。
Is there a way to prevent Doctrine from assigning a contraint on both sides of a one-to-one relationship? Ive tried moving the definition from one side to the other and using owning side but it still places a constraint on both tables. when I only want the parent table to have a constraint - ie. its possible for the parent to not have an associated child.
For example iwant the following sql schema essentially:
CREATE TABLE `parent_table` (
`child_id` varchar(50) NOT NULL,
`id` integer UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (`id`)
);
CREATE TABLE `child_table` (
`id` integer UNSIGNED NOT NULL auto_increment,
`child_id` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`child_id`),
CONSTRAINT `parent_table_child_id_FK_child_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `parent_table` (`child_id`)
);
However im getting something like this:
CREATE TABLE `parent_table` (
`child_id` varchar(50) NOT NULL,
`id` integer UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (`id`),
CONSTRAINT `child_table_child_id_FK_parent_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `child_table` (`child_id`)
);
CREATE TABLE `child_table` (
`id` integer UNSIGNED NOT NULL auto_increment,
`child_id` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`child_id`),
CONSTRAINT `parent_table_child_id_FK_child_table_child_id`
FOREIGN KEY (`child_id`)
REFERENCES `parent_table` (`child_id`)
);
I could just remove the constraint manually or modify my accessors to return/set a single entity in the collection (using a one-to-many) but it seems like there should built in way to handle this.
Also im using Symfony 1.4.4 (pear installtion ATM) - in case its an sfDoctrinePlugin issue and not necessarily Doctrine itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sfDoctrinePlugin 以自动创建相对方关系的方式配置条令模型构建器。在 PHP 5.2 中你不能用它做任何事情。
从 PHP 5.3 开始,其中
ReflectionProperty::setAccessible()
方法可用,您可以操作<的受保护/私有属性 code>Doctrine_Relation_Parser 实例,可以通过$this->getTable()->getRelationParser()
获取。因此,您必须重写BaseSomething::setUp()
方法并使用 php 的反射 API 手动删除不需要的关系。您必须使用反射 API,因为Doctrine_Relation_Parser
不提供允许随意删除关系的方法。sfDoctrinePlugin configures doctrine model builder in way that creates opposite side relations automatically. In PHP 5.2 you can't do anything with it.
As of PHP 5.3, where
ReflectionProperty::setAccessible()
method is available, you could manipulate protected/private properties ofDoctrine_Relation_Parser
instance which can be obtained by$this->getTable()->getRelationParser()
. So you have to overrideBaseSomething::setUp()
method and drop unneeded relations manually using php's reflection API. You have to use reflection API becauseDoctrine_Relation_Parser
doesn't provide a method which allows to drop relations at will.我通过在保存记录之前为相反的“空对象”设置教义状态来解决这个问题:
I've solved this by setting the doctrine state for the opposite "null object" BEFORE we save the record: