原则 1.2:如何防止将约束分配给一对多关系的双方?

发布于 2024-08-29 06:59:01 字数 1376 浏览 7 评论 0原文

有没有办法防止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-09-05 06:59:01

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 of Doctrine_Relation_Parser instance which can be obtained by $this->getTable()->getRelationParser(). So you have to override BaseSomething::setUp() method and drop unneeded relations manually using php's reflection API. You have to use reflection API because Doctrine_Relation_Parser doesn't provide a method which allows to drop relations at will.

烟花肆意 2024-09-05 06:59:01

我通过在保存记录之前为相反的“空对象”设置教义状态来解决这个问题:

/* @var $address Address */
$address = $form->getObject();

// the null object is here the "address position", let's change it's state
$address->getAddressPosition()->state(Doctrine_Record::STATE_CLEAN);

// now we can save the address and doctrine won't insert the null object
$address = $form->save();

I've solved this by setting the doctrine state for the opposite "null object" BEFORE we save the record:

/* @var $address Address */
$address = $form->getObject();

// the null object is here the "address position", let's change it's state
$address->getAddressPosition()->state(Doctrine_Record::STATE_CLEAN);

// now we can save the address and doctrine won't insert the null object
$address = $form->save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文