Reputation: 2388
I have a variable $pk in my model/entity class. I want to map it to the table_pk field in my table.
How do I do this?
I'm reading this manual => http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-column. but nothing seems to do what I want.
an example on how this can be done using annotation and yaml mapping would be greatly appreciated.
Upvotes: 3
Views: 2432
Reputation: 25440
This is quite simple (the @ORM\GeneratedValue
bit is needed only if your PK is auto incremental):
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;
}
And the same entity mapped with YAML configuration
# MyNamespace.MyEntity.dcm.yml
MyNamespace\MyEntity:
type: entity
table: my_entity
id:
id:
type: integer
column: table_pk
generator:
strategy: AUTO
And as a bonus, XML mappings:
<!-- 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>
Upvotes: 7