I am trying to add a ManyToOne column to an already existing table with rows in it.
I want it to be nullable=false
!
Since it is not nullable, I'm trying to set a default value, but there is no option to do it. If I don't set a default value, the shema update crashes and is not able to create the foreign key since there is no row with id of 0 in the table I created
The code of the table refered by the foreign key:
<?php
namespace MyBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints;
/**
* @ORM\Table(name="authority")
* @ORM\Entity()
*/
class Authority
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @Constraints\NotBlank(message="name cannot be blank.")
* @ORM\Column(type="string")
*/
protected $name;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime", nullable=true)
*/
protected $createdAt;
/**
* Authority constructor.
*/
public function __construct()
{
$this->createdAt = new \DateTime();
}
}
The code of the table where I want to add the foreign key:
/**
* @var Authority
* @ORM\ManyToOne(targetEntity="MyBundle\Entity\authority", cascade={"persist"})
* @ORM\JoinColumn(referencedColumnName="id", nullable=false)
*/
protected $authority;
I tried:
protected $authority = 1;
or
* @ORM\JoinColumn(referencedColumnName="id", nullable=false, default=1)
or
public function __construct()
{
$this->authority = 1;
}
I don't want to have to change the database manually.