Doctrine 2 @joincolumns

时间:2016-02-21 12:15:48

标签: php orm doctrine-orm

我有两个实体Page和File。一页有很多文件。我的其他实体也有很多文件,所以我通过两列(entity_id和entity_type)加入Page和File。当我有样本数据并且我得到它时,一切正常。当我尝试将页面和文件添加到数据库时,我发现了问题,文件从页面获取了ID,但是没有实现entity_type。

创建实体:

 $page = new \CmsIr\Page\Entity\Page();
 $page->exchangeArray($form->getData());
 $page->setEntityType('page');

 $this->getEm()->persist($page);
 $this->getEm()->flush();

 foreach($scannedDirectory as $file) {
     $mimeType = $this->getFileService()->getMimeContentType($this->uploadDir . '/' . $file);

     $pageFile = new \CmsIr\File\Entity\File();
     $pageFile->setFilename($file);
     $pageFile->setPage($page);
     $pageFile->setMimeType($mimeType);

     $this->getEm()->persist($pageFile);
     $this->getEm()->flush();                       
 }

错误看起来像:

An exception occurred while executing 'INSERT INTO cms_file (entity_type, entity_id, filename, mime_type) VALUES (?, ?, ?, ?)' with params [null, 5, "140343_las-promienie-slonca-56c9a66d27830.jpg", "image\/jpeg; charset=binary"]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'entity_type' cannot be null

页:

/**
* @ORM\Entity
* @ORM\Table(name="cms_page")
*/
class Page
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
protected $id;

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

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

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

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

/**
*  @ORM\OneToOne(targetEntity="CmsIr\System\Entity\Status")
*  @ORM\JoinColumn(name="status_id", referencedColumnName="id")
*/
protected $status;

/** @ORM\Column(type="text") */
protected $content;

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

/**
 * @ORM\OneToMany(targetEntity="CmsIr\File\Entity\File", mappedBy="page")
 */
protected $files;

public function __construct()
{
    $this->files = new \Doctrine\Common\Collections\ArrayCollection();
}

文件:

/**
* @ORM\Entity
* @ORM\Table(name="cms_file")
*/
class File
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
protected $id;

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

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

/** @ORM\Column(type="text") */
protected $filename;

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

/**
 * @ORM\ManyToOne(targetEntity="CmsIr\Page\Entity\Page")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="entity_id", referencedColumnName="id"),
 *   @ORM\JoinColumn(name="entity_type", referencedColumnName="entity_type"),
 * })
**/
protected $page;

0 个答案:

没有答案