I’m trying to add relationships to my database using the annotation method in my User Entity and Role Entity classes, however when I update the meta data and run an update from the command line and then go to phpMyAdmin there are no relationships present using the designer tool. I’m trying to write all database changes using Symfony2 and Doctrine to keep it consistent. Here is what I currently have:
<?php
namespace XXX\XXXBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* @ORM\Table(name="role")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class Role
{
/**
*
* @ORM\OneToMany(targetEntity="User", mappedBy="role")
*/
private $users;
}
<?php
namespace XXX\XXXBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user",indexes={@ORM\Index(name="role", columns={"role_id"})})
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="role_id", type="integer")
* @ORM\ManyToOne(targetEntity="Role", inversedBy="users")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
*/
private $role;
}
$role should not contain an @ORM\Column definition. The role_id column will be automatically created by the ORM based on the name parameter in @JoinColumn. $role will be used to populate the associated Role entity, not an integer.