繁体   English   中英

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

[英]Doctrine 2 - Integrity constraint violation error

我有两个实体,产品和会员。 我想插入新产品,但出现错误:

违反完整性约束:1048列“ affiliateId”不能为空

这是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;
    }
}

关联实体:

<?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;
    }
}

我不确定您如何设置配置文件,但是我设法使其与zend 1.10框架一起使用,并且在下面(仅更改名称空间和类引用以匹配您的配置文件,代码执行也位于底部)。页)

<?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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM