S
S
Sergey2016-01-09 21:46:12
symfony
Sergey, 2016-01-09 21:46:12

How can I link to a non-ID column in Doctrine 2.5?

Good day to all. I'm developing a broken service in Symfony 3, and I'm stuck creating a relationship between two tables. The simplified scheme is as follows: two entities: Action - many records and Status - several lines.
Each Action line must be associated with one of the statuses, and this is easily done via

* @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Status")
     * @ORM\JoinColumn(referencedColumnName="id", nullable=false)

in the Action entity annotations. But I have a slightly different task, I need to associate a string from Action with the `code` field of the
Status entity. The source code below with doctrine:sch:update successfully sculpts a database that works as it should through PHPMa, but when you try to create a new Action, the doctrine throws an error Missing value for primary key id on AppBundle\Entity\Status
class Action
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="\AppBundle\Entity\Status")
     * @ORM\JoinColumn(referencedColumnName="code", nullable=false)
     */
    private $status;
}

class Status
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="integer", unique=true)
     */
    private $code;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2016-01-09
@maxloyko

Join-Columns with non-primary keys

S
Sergey, 2016-01-09
Protko @Fesor

The philosophy behind the doctrine is that the database is an implementation detail, and you should design the business entities, their relationships, and their interactions. The database in this case must be completely generated.
What you want to do goes against the philosophy of Doctrine. It is impossible to organize such a connection. You can join anything with anything in queries, but no more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question