K
K
Kolya Vantukh2019-12-02 18:49:51
symfony
Kolya Vantukh, 2019-12-02 18:49:51

Why can't I get an entity from a repository in Doctrine?

There is an essence

/**
 * @Entity
 * @Table(name="`address`")
 */
class Address
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $address_id;
    /**
     * @Column(type="string")
     */
    protected $firstname;
    /**
     * @ManyToOne(targetEntity="Customer", inversedBy="address")
     * @JoinColumn(name="customer_id", referencedColumnName="customer_id")
     */
    protected $customer;

/**
 * @Entity(repositoryClass="\Core\Repositories\CustomerRepository")
 * @Table(name="`customer`")
 */
class Customer
{
    public function __construct()
    {
        $this->addresses = new ArrayCollection();
    }
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $customer_id;
/**
     * @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
     * @JoinColumn(name="customer_id", referencedColumnName="customer_id")
     */
    protected $addresses;

The problem is that I can't get the address via the repository
$this->entityManager->getRepository(Address::class)->findOneBy(['address_id' => 1]);

Gives an error message
Uncaught Doctrine\Common\Proxy\Exception\UnexpectedValueException: Your proxy directory "/tmp" must be writable

But when I disconnect
/**
     * @ManyToOne(targetEntity="Customer", inversedBy="address")
     * @JoinColumn(name="customer_id", referencedColumnName="customer_id")
     */
    protected $customer;

then everything works. So the problem is in the connections, but where? Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2019-12-02
@vkolya

https://www.doctrine-project.org/projects/doctrine...

/**
 * @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
 * @JoinColumn(name="customer_id", referencedColumnName="customer_id")
 */
protected $addresses;

Address::$customer_idis it the primary key?
Didn't notice that you are applying JoinColumn to OneToMany. It doesn't apply , it should be something like this:
/**
 * @Entity
 * @Table(name="`address`")
 */
class Address
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $address_id;
    /**
     * @Column(type="string")
     */
    protected $firstname;
    /**
     * @ManyToOne(targetEntity="Customer", inversedBy="address")
     * @JoinColumn(name="customer_id", referencedColumnName="customer_id")
     */
    protected $customer;
}

/**
 * @Entity(repositoryClass="\Core\Repositories\CustomerRepository")
 * @Table(name="`customer`")
 */
class Customer
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $customer_id;
    
    /**
     * @OneToMany(targetEntity="Address", mappedBy="customer", cascade={"persist", "remove"})
     */
    protected $addresses;

    public function __construct()
    {
        $this->addresses = new ArrayCollection();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question