K
K
Kolya Vantukh2019-12-01 12:31:44
symfony
Kolya Vantukh, 2019-12-01 12:31:44

Why is join column in Doctrine assigned to null before flush?

There are two entities

/**
 * @Entity
 * @Table(name="`order`")
 */
class Order
{
    public function __construct()
    {
        $this->products = new ArrayCollection();
        $this->totals = new ArrayCollection();
    }
    /**
     * @Id
     * @Column(type="integer")
     */
    protected $order_id;
    /**
     * @Id
     * @Column(type="integer")
     */
    protected $order_code;
    /**
     * @Column(type="integer")
     */
    protected $warehouse_id;
    /**
     * @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
     * @JoinColumn(name="order_code", referencedColumnName="order_code")
     */
    protected $boxberry_ttn;
}

and
/**
 * @Entity
 * @Table(name="`ttn_boxberry`")
 */
class BoxberryTtn
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $id;
    /**
     * @Column(type="string")
     */
    protected $ttn_num;
    /**
     * @Column(type="float", scale=2)
     */
    protected $delivery_cost;
    /**
     * @Column(type="string")
     */
    protected $order_code;
}

Communication one to one. When an order is created, the relationship is not added immediately. The problem is that before the order is saved and flush() is called, the order_code field of the Order entity is set to a value, but when flush() is executed directly, the order_code field is set to zero. Apparently, the problem is in the definition of links, because if I remove @JoinColumn(name="order_code", referencedColumnName="order_code") in the mapping, then everything goes through. Where did I screw up that the doctrine behaves so strangely?

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...

/**
 * @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
 * @JoinColumn(name="order_code", referencedColumnName="id")
 */
protected $boxberry_ttn;

If you want to have a connection through order_code, then make it a primary key:
/**
 * @Entity
 * @Table(name="`ttn_boxberry`")
 */
class BoxberryTtn
{
    /**
     * @Id
     * @Column(type="string")
     * @GeneratedValue(strategy="NONE")
     */
    protected $id;
    
    public function __construct(string $id)
    {
        $this->id = $tid;
    }
}

Plus, you have two fieldsorder_code
/**
 * @Entity
 * @Table(name="`order`")
 */
class Order
{
    /**
     * @Id
     * @Column(type="integer")
     */
    protected $order_code;

    /**
     * @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", mappedBy="ttn_boxberry", cascade={"persist", "remove"})
     * @JoinColumn(name="order_code", referencedColumnName="order_code")
     */
    protected $boxberry_ttn;
}

They can also overwrite each other.
/**
 * @Entity
 * @Table(name="`order`")
 */
class Order
{
    /**
     * @Id
     * @Column(type="integer")
     */
    protected $order_id;

    /**
     * @Id
     * @Column(type="integer")
     */
    protected $order_code;

    /**
     * @Column(type="integer")
     */
    protected $warehouse_id;

    /**
     * @OneToOne(targetEntity="\Core\Entities\Delivery\BoxberryTtn", cascade={"persist", "remove"})
     * @JoinColumn(referencedColumnName="order_code")
     */
    protected $boxberry_ttn;

    public function __construct(int $order_id, string $order_code)
    {
        $this->order_id = $order_id;
        $this->order_code = $order_code;
    }
}

/**
 * @Entity
 * @Table(name="`ttn_boxberry`")
 */
class BoxberryTtn
{
    /**
     * @Id
     * @Column(type="string")
     * @GeneratedValue(strategy="NONE")
     */
    protected $order_code;

    /**
     * @Column(type="string")
     */
    protected $ttn_num;

    /**
     * @Column(type="float", scale=2)
     */
    protected $delivery_cost;

    public function __construct(Order $order)
    {
        $this->order_code = $order->getOrderCode();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question