I have two tables: plans
and marks
.
For this two tables
<?php
/**
* @Entity
* @Table(name="marks")
*/
class Mark{
/**
* @ManyToOne(targetEntity="Plan", inversedBy="marks")
* @JoinColumn(name="plan_id", referencedColumnName="id")
**/
protected $plan = null;
...
}
And
<?php
/**
* @Entity
* @Table(name="plans")
*/
class Plan{
/**
* @OneToMany(targetEntity="Mark", mappedBy="plan")
*/
private $marks;
...
}
As you can understand from this code in table marks
I have field plan_id
(which store plan id). This field is not mandatory and I want by default that plan_id = 0
, but I can do it. If I set field $plan
in class Mark
= 0, I have error that was expected object. But If I set plan = 0, it save NULL
in the database, I don't want it at all.
Maybe somebody has some advices?
Use lifecycle callbacks on Mark->setPlan()
/**
* @Entity
* @Table(name="marks")
* @ORM\HasLifecycleCallbacks
*/
class Mark{
/**
* @ManyToOne(targetEntity="Plan", inversedBy="marks")
* @JoinColumn(name="plan_id", referencedColumnName="id")
**/
protected $plan = null;
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function setPlan(Plan $plan = null){
if(is_null($var)){
$this->plan = 0;
}
}
But I think that Doctrine will return errors trying to find a plan with id = 0.