ZF2 Doctrine2 - 未定义的索引:clientId

时间:2015-09-06 19:51:22

标签: php doctrine-orm zend-framework2

我已经实现了两个带注释的实体:

/**
 * @Entity @Table(name="clients")
 **/
class Client
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

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

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

    /**
     * @OneToMany(targetEntity="Order",mappedBy="clientId", cascade={"persist","remove"})
     * @var Order[]
     **/
    protected $orders;
    ...
}

/**
 * @Entity @Table(name="orders")
 **/
class Order
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

    /**
     * @Column(type="integer")
     * @JoinColumn(name="clientId",referencedColumnName="id")
     */
    protected $clientId;
    ...
}

执行以下操作时:

public function deleteClient($clientId) {
    if (is_int($clientId)) {
        $retr = $this->getClient($clientId);
        if ( $retr ) {
            $orders = $retr->getOrders(); // <- It fails here
            $this->db->getEntityManager()->detach($retr);
            foreach ($orders as $order) {
                $this->deleteOrder($order->getId());
            }
            $q1 = $this->db->getEntityManager()->createQuery(
                'delete from Main\Model\Client u WHERE u.id = '.$clientId);
            $q1->execute();
        }
    }
}

我收到以下错误消息:

Notice: Undefined index: clientId in
...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php    
on line 1758

为什么?

1 个答案:

答案 0 :(得分:1)

您的注释应如下所示:

/**
 * @Entity @Table(name="clients")
 **/
class Client
{
    ...
    /**
     * @OneToMany(targetEntity="Order", mappedBy="client", cascade={"persist","remove"})
     **/
    protected $orders;
    ...
}

/**
 * @Entity @Table(name="orders")
 **/
class Order
{
    ...
    /**
     * @ManyToOne(targetEntity="Client", inversedBy="orders")
     */
    protected $client;
    ...
}