2

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

违反完整性约束: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;
    }
}
4

1 回答 1

0

我不确定你是如何设置你的配置文件的,但是我已经设法让它与 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);
于 2012-04-30T09:22:41.993 回答