【问题标题】:How do I map a table field to a variable of different name in a doctrine entity?如何将表字段映射到学说实体中不同名称的变量?
【发布时间】:2013-03-19 01:32:02
【问题描述】:

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

我该怎么做?

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

非常感谢您提供有关如何使用注释和 yaml 映射来完成此操作的示例。

【问题讨论】:

    标签: php doctrine-orm


    【解决方案1】:

    这很简单(只有当您的 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>
    

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 2021-04-17
      • 1970-01-01
      • 2011-09-20
      • 2019-10-12
      • 1970-01-01
      • 2011-01-14
      相关资源
      最近更新 更多