I know there is this question Default value in Doctrine
My question is how do I set a default value=0 in Doctrine2/Symfony2, when column has ManyToOne relation?
Column product_id
in DB is not null
, I can't change it!
If I do setProductId(0), it is somehow overridden by setProduct(null) and I get.
Integrity constraint violation: 1048 Column 'product_id' cannot be null
If I default $product = 0;
or $productId = 0;
or both, as suggested by doctrine-faqs, I get
Expected value of type "Entity\Product" ...
I want to set it = 0 if its not set!
Is it OK Or even allowed to have $productId and $product properties pointing to same database column?
Code almost look like this:
class ShopLog {
/**
* @var integer
*
* @ORM\Column(name="product_id", type="integer", nullable=false) // , columnDefinition="DEFAULT 0" does not work
*/
private $productId;
/**
* @var Entity\Product
*
* @ORM\ManyToOne(targetEntity="Entity\Product")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false) // , columnDefinition="DEFAULT 0" does not work
* })
*/
private $product;
/**
* Set productId
*
* @param integer $productId
* @return ShopLog
*/
public function setProductId($productId)
{
$this->productId = $productId;
return $this;
}
/**
* Set product
*
* @param \Entity\Product $product
* @return ShopLog
*/
public function setProduct(\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}