原则创建外键约束但不生成实体

时间:2019-02-24 14:44:43

标签: mysql orm doctrine-orm doctrine foreign-keys

some_type表仅用于将foo.type的允许值限制为some_table.type中的值,并且将永远不会创建SomeType实体。 Foo应该具有属性type,但不能具有属性someType。如果删除<many-to-one target-entity="SomeType">...</many-to-one>,则Foo#someType将根据需要删除,但外键将从架构中删除。如何指示Doctrine保留外键而不生成Foo#someType

SomeType

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="SomeType" table="some_type">
    <id name="type" type="string" column="type" length="8"/>
  </entity>
</doctrine-mapping>

Foo

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Foo" table="foo">
    <indexes>
      <index name="fk_foo_some_type_idx" columns="type"/>
      <index name="fk_foo_bar1_idx" columns="bar_id"/>
    </indexes>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="type" type="string" column="type" length="8"/>
    <many-to-one target-entity="SomeType">
      <join-columns>
        <join-column name="type" referenced-column-name="type"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="bar" target-entity="Bar" inversed-by="foos" fetch="LAZY">
      <join-columns>
        <join-column name="bar_id" referenced-column-name="id" nullable="false"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>

0 个答案:

没有答案