Doctrine orm 原则2-完整性约束违反错误

Doctrine orm 原则2-完整性约束违反错误,doctrine-orm,zend-framework2,Doctrine Orm,Zend Framework2,我有两个实体,产品和附属公司。我想插入新产品,但出现错误: 完整性约束冲突:1048列“affiliateId”不能为null 这是php代码: $affiliate = $this->_em->getRepository("Common\Entity\Affiliate")->find(1); $product = new Product(); $product->setAffiliate($af

我有两个实体,产品和附属公司。我想插入新产品,但出现错误:

完整性约束冲突:1048列“affiliateId”不能为null

这是php代码:

            $affiliate = $this->_em->getRepository("Common\Entity\Affiliate")->find(1);

            $product = new Product();
            $product->setAffiliate($affiliate);
            $product->statusId = $form->getValue('statusId');
            [...]
            $product->created = new \DateTime("now");

            $this->_em->persist($product);
            $this->_em->flush();
产品实体:

<?php

namespace Common\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * A product.
 *
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $statusId;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;


    /**
     * @ORM\ManyToOne(targetEntity="Affiliate", inversedBy="product")
     * @ORM\JoinColumn(name="affiliateId", referencedColumnName="id")
     */
    protected $affiliate;

    public function setAffiliate($affiliate) { 
        $this->affiliate = $affiliate; 
    }

    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property) {
        return $this->$property;
    }


    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value) {
        $this->$property = $value;
    }
}

我不确定您是如何设置配置文件的,但是我已经设法让它与zend 1.10 framework一起工作,下面就是(只需更改名称空间和类引用以匹配您的名称空间和类引用,代码执行也在页面底部)


我不确定您是如何设置配置文件的,但是我已经设法让它与zend 1.10 framework一起工作,下面就是(只需更改名称空间和类引用以匹配您的名称空间和类引用,代码执行也在页面底部)

<?php

namespace Common\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * A product.
 *
 * @ORM\Entity
 * @ORM\Table(name="affiliate")
 */
class Affiliate {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer");
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

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


    /**
     * @ORM\Column(type="datetime")
     */
    protected $lastModified;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="affiliate")
     */
    protected $product;



    public function getId() { 
        return $this->id; 
    }

    public function getName() { 
        return $this->name; 
    }


    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property) {
        return $this->$property;
    }


    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value) {
        $this->$property = $value;
    }
}
<?php
namespace GR\Entity;
/**
 *
 * @Table(name="affiliate")
 * @Entity
 */
class Affiliate {

    /**
     * @var integer $id
     * @Column(name="id", type="integer", nullable=false);
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

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


    /**
     * @Column(type="datetime", nullable=false)
     */
    protected $modified_on;

    /**
     * @Column(type="datetime", nullable=false)
     */
    protected $created_on;

    /**
     * @OneToMany(targetEntity="GR\Entity\Product", mappedBy="affiliate", cascade={"persist", "remove"})
     * @param \Doctrine\Common\Collections\Collection $property
     */

    protected $products;
    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property)
    {
        return $this->$property;
    }


    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
    }

    public function __construct()
    {
        $this->created_on = new \DateTime("now");
    }
}

<?php
namespace GR\Entity;
/**
 * 
 * @Table(name="product")
 * @Entity
 */
class Product {

    /**
     *
     * @var integer $id
     * @Column(name="id", type="integer",nullable=false)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

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

    /**
     * @Column(type="datetime",nullable=false)
     */
    protected $created_on;

    /**
     * @var GR_Entity_Affiliate
     * @ManyToOne(targetEntity="GR\Entity\Affiliate")
     * @JoinColumns({
     *  @JoinColumn(name="affiliate_id", referencedColumnName="id")
     * })
     **/
    protected $affiliate;

    /**
     * Magic getter to expose protected properties.
     *
     * @param string $property
     * @return mixed
     */
    public function __get($property)
    {
        return $this->$property;
    }

    /**
     * Magic setter to save protected properties.
     *
     * @param string $property
     * @param mixed $value
     */
    public function __set($property, $value)
    {
        $this->$property = $value;
    }

    public function __construct()
    {
        $this->created_on = new \DateTime("now");
    }
}
$p1 = new GR\Entity\Product();
    $p1->status_id = 2;

    $p2 = new GR\Entity\Product();
    $p2->status_id = 3;

    $a = new GR\Entity\Affiliate();
    $a->name = 'test1';
    $a->modified_on = new DateTime('now');
    $a->created_on = new DateTime('now');
    $a->product = array ($p1, $p2);

    $doctrineContainer = Zend_Registry::get('doctrine');
    $em = $doctrineContainer->getEntityManager(); 
    $em->persist($a);
    $em->flush();

    $affiliate = $em->createQuery('select a from GR\Entity\Affiliate a')->execute();
    var_dump($affiliate[0]->products[0]->id);