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

mrj*_*per 3 php doctrine-orm

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

我该怎么做呢?

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

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

Ocr*_*ius 6

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

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;
}
Run Code Online (Sandbox Code Playgroud)

并且使用YAML配置映射相同的实体

# MyNamespace.MyEntity.dcm.yml
MyNamespace\MyEntity:
  type: entity
    table: my_entity
  id:
    id:
      type: integer
      column: table_pk
      generator:
        strategy: AUTO
Run Code Online (Sandbox Code Playgroud)

作为奖励,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>
Run Code Online (Sandbox Code Playgroud)