2012-04-16 120 views
2

我有兩個實體,產品和子公司。我想插入一個新的產品,但我得到的錯誤:學說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; 
    } 
} 

回答

1

我不知道你是如何設置你的配置文件的,但是我已經設法使用zend1.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); 
相關問題