我有一个变量$pk在我的模型/实体类。我想把它映射到表中的table_pk字段
我该怎么做?
我正在看这本手册=> http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-column。但似乎没有什么能如我所愿。
关于如何使用注释和yaml映射来实现这一点的示例将非常感谢。
这很简单(只有当您的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>