R
R
Roquefort2017-11-03 15:49:24
Doctrine ORM
Roquefort, 2017-11-03 15:49:24

Why doesn't Doctrine record the parameter passed to it?

Class Employees
 {
 /**
     * Parent UID
     * @var int
     *
     * @ORM\Column(name="parentId", type="integer", nullable=true)
     */
    private $parentId;

    /**
     * Division ID
     * @var int
     *
     * @ORM\Column(name="divisionId", type="integer", nullable=true)
     */
    private $divisionId = "";

    /**
     * Подразделение сотрудника
     * @ORM\OneToOne(targetEntity="Statistic\Entity\Division")
     * @ORM\JoinColumn(name="divisionId", referencedColumnName="id")
     */
    private $division;

    /**
     * Руководитель сотрудника
     * @ORM\OneToOne(targetEntity="Employees\Entity\Employees")
     * @ORM\JoinColumn(name="parentId", referencedColumnName="id")
     */
    private $parent;
}

When I do
...
$employee->setParentId(4);
$entityManager->persist($employee);
$entityManager->flush();

All previous properties are written to the database, and NULL arrives in parentId. Everything broke when I did @JoinColumn in the parent property. And now we need to network the object in parent. To sign up parentId. Maybe there is an opportunity to do without passing the object, so that it writes exactly what I set in parentId

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2017-11-07
@Roquefort

Remove the parentId field, and everything will work. Such a field in the database will make the doctrine itself, when creating a relationship between tables. You can leave this field in the entity if you so desire, but you still need to establish an association between records with the parent field.
Plus, it looks like you have an error in the relationship type in the parent and division fields. You have a OneToOne relationship type, that is, a manager can have only one employee, and a department can have only one employee. Set the connection type to ManyToOne (see the article ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question