I have got two entities associated with OneToOne association. This is how it all looks like:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="a")
*/
class A
{
....
/**
* @ORM\OneToOne(targetEntity="B", cascade={"all"}, orphanRemoval=true)
* @ORM\JoinColumn(name="b_id", referencedColumnName="id", nullable=true)
*/
private $b;
}
/**
* @ORM\Entity
* @ORM\Table(name="b")
*/
class B
{
....
/**
* @ORM\Column(type="string", length=20)
*/
protected $type;
/**
* @ORM\Column(type="integer")
*/
protected $aId;
}
What I am trying to do is to crate lifeCycleCallback preUpdate on A class. While calling preUpdate A class already has its on ID. I have tried to do something like this:
/**
* @ORM\PreUpdate
*/
public function update()
{
if (is_null($this->getB()))
$this->b = new B();
$this->b->setAId($this->id)->setType('A'/* classname */);
}
B class has to store classname in type filed and object id in aId field. I cannot use foreign keys here.
The problem is that the above update function returns this error:
Notice: Undefined index: 000000006914b7950000000033b7b784 in C:...\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 2735
Any ideas what this error means and how to solve it?
EDIT This is how I fetch A object before passing it to edit form:
$this->getEntityManager()
->createQueryBuilder()
->select('a,b')
->from('MyBundle:A', 'a')
->leftJoin('a.b','b')
->where('a.id = :aId')
->setParameter('aId', $id)
->getQuery()->getSingleResult();
I do edit only A object (in form) and want to create associated B object on first A update and make some changes to this B object on every next update of A.
EDIT2
The above error no longer displays. However changes made to B object in @ORM\PreUpdate
of A object are never persisted even when association to be has cascade={"all"}
.
Here is the example:
/**
* inside A class !
*
* @ORM\PreUpdate
*/
public function update()
{
$this->lastModified = new \DateTime();
$this->getB()
->setSomething('something'); // <-- this is never stored to db by cascade
}