0

Im tying to add communication parts to a rootCommunication in my data-fixture, there is no error, but only just NULL in the database field 'root_communication_id'. Why?

Parts of my Model 'Communication'

/**
 * Communication
 *
 * @ORM\Table(name="communication")
 * @ORM\Entity(repositoryClass="Mother\BaseBundle\Entity\Repository\CommunicationRepository")
 */
class Communication
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="message", type="text", nullable=true)
     */
    private $message;

     /**
     * @ORM\ManyToOne(targetEntity="Communication", inversedBy="childrenCommunication", cascade={"persist"})
     * @ORM\JoinColumn(name="root_communication_id", referencedColumnName="id", nullable=true)
     * 
     */
    private $rootCommunication;

    /**
    * @ORM\OneToMany(targetEntity="Communication", mappedBy="rootCommunication")
    * 
    */
    private $childrenCommunication;

}

In a first data-fixture i added three communications to the database, in this secound fixture i add the childrenCommunication to the rootCommunication.

     /**
     * {@inheritDoc}
     */
    public function load( ObjectManager $manager ){

        $contentRepo = $this->container->get('doctrine')->getManager()->getRepository('MotherBaseBundle:Communication');


        $communication1 = $contentRepo->find( $this->getReference('communication1')->getId() );
        $communication1->addChildrenCommunication( $this->getReference('communication2') );
        $communication1->addChildrenCommunication( $this->getReference('communication3') );


        $manager->persist( $communication1 );
        $manager->flush();
}

1 Answers1

0

I assume you are not setting the rootCommunication when you are adding the child.

You should add an auto setter to the add method, like..

public function addChildrenCommunication(CommunicationInterface $communication)
{
    if (!$this->childrenCommunication->contains($communication)) {
        $this->childrenCommunication->add($communication);
        $communication->setRootCommunication($this);
    }

    return $this;
}

.. and the same for the remove..

qooplmao
  • 17,622
  • 2
  • 44
  • 69