如何将表字段映射到主题实体中的不同名称的变量?

时间:2013-03-19 01:32:03

标签: php doctrine-orm

我的模型/实体类中有一个变量$ pk。我想将它映射到表格中的table_pk字段。

我该怎么做?

我正在阅读本手册=> http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-column。但似乎没有什么能做我想做的事。

关于如何使用注释和yaml映射完成此操作的示例将非常感激。

1 个答案:

答案 0 :(得分:6)

这非常简单(仅当您的PK是自动增量时才需要@ORM\GeneratedValue位):

namespace MyNamespace;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="my_entity")
 */
class MyEntity
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="table_pk", type="integer")
     */
    protected $id;
}

与YAML配置映射的同一实体

# MyNamespace.MyEntity.dcm.yml
MyNamespace\MyEntity:
  type: entity
    table: my_entity
  id:
    id:
      type: integer
      column: table_pk
      generator:
        strategy: AUTO

作为奖励,XML映射:

<!-- MyNamespace.MyEntity.dcm.xml -->
<?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
    http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd"
>
    <entity name="MyNamespace\MyEntity" table="table_name">
        <id name="id" type="integer" column="table_pk">
            <generator strategy="AUTO"/>
        </id>
    </entity>
</doctrine-mapping>