好的,我按照Joel Verhagen的思路在 Codeigniter 中使用 Doctrine2 ,一切正常,但现在我遇到了一个神秘的问题。
销毁物品时,一切正常:
$delete = $this->doctrine->em->getRepository('Entities\Item')->findOneBy(array('slug' => $item));
$this->doctrine->em->remove($delete);
$this->doctrine->em->flush();
但是在 getRepository() 之后,更新项目会默默地失败:
$item = $this->doctrine->em->getRepository('Entities\Item')->findOneBy(array('id' => $id));
$item->setDescription(html_entity_decode($_POST['content']));
$this->doctrine->em->persist($item);
$this->doctrine->em->flush();
相同的控制器,这就是每个动作中的所有代码。我所有的其他控制器都很好,但这失败了,没有任何日志。拆分 getRepository 和 find 调用在 getRepository 之后立即终止。将 getRepository 移动到 __construct 会导致 update() 因查找而死。
想法?线索?似乎 SO 上的其他任何人最终要么走向不同的方向,要么从未解决过问题。
谢谢!
编辑:添加模型
yaml
物品
Entities\Item:
type: entity
table: items
id:
id:
type: integer
primary: true
notnull: true
generator:
strategy: AUTO
fields:
title:
type: string(255)
notnull: true
slug:
type: string(255)
notnull: true
description:
type: string(255)
manyToOne:
type:
targetEntity: Type
inversedBy: item
joinColumn:
name: type_id
referencedColumnName: id
options:
charset: utf8
type: InnoDB
类型
Entities\Type:
type: entity
table: types
id:
id:
type: integer
primary: true
notnull: true
generator:
strategy: AUTO
fields:
title:
type: string(255)
notnull: true
slug:
type: string(255)
notnull: true
description:
type: string(255)
oneToMany:
items:
targetEntity: Item
orphanRemoval: true
mappedBy: type
manyToMany:
fields:
targetEntity: Field
inversedBy: types
joinTable:
name: fields_types
joinColumns:
type_id:
referencedColumnName: id
inverseJoinColumns:
field_id:
referencedColumnName: id
options:
charset: utf8
type: InnoDB
PHP
物品
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\Item
*/
class Item
{
/**
* @var integer $id
*/
private $id;
/**
* @var string $title
*/
private $title;
/**
* @var string $description
*/
private $description;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $fields;
/**
* Constructor
*/
public function __construct()
{
$this->fields = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Item
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return Item
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add fields
*
* @param Entities\Field $fields
* @return Item
*/
public function addField(\Entities\Field $fields)
{
$this->fields[] = $fields;
return $this;
}
/**
* Remove fields
*
* @param Entities\Field $fields
*/
public function removeField(\Entities\Field $fields)
{
$this->fields->removeElement($fields);
}
/**
* Get fields
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFields()
{
return $this->fields;
}
/**
* @var Entities\Type
*/
private $types;
/**
* Set types
*
* @param Entities\Type $types
* @return Item
*/
public function setTypes(\Entities\Type $types = null)
{
$this->types = $types;
return $this;
}
/**
* Get types
*
* @return Entities\Type
*/
public function getTypes()
{
return $this->types;
}
/**
* @var Entities\Type
*/
private $type;
/**
* Set type
*
* @param Entities\Type $type
* @return Item
*/
public function setType(\Entities\Type $type = null)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return Entities\Type
*/
public function getType()
{
return $this->type;
}
/**
* @var string $slug
*/
private $slug;
/**
* Set slug
*
* @param string $slug
* @return Item
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
类型
<?php
namespace Entities;
use Doctrine\ORM\Mapping as ORM;
/**
* Entities\Type
*/
class Type
{
/**
* @var integer $id
*/
private $id;
/**
* @var string $title
*/
private $title;
/**
* @var string $description
*/
private $description;
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $items;
/**
* Constructor
*/
public function __construct()
{
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Type
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* @param string $description
* @return Type
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add items
*
* @param Entities\Item $items
* @return Type
*/
public function addItem(\Entities\Item $items)
{
$this->items[] = $items;
return $this;
}
/**
* Remove items
*
* @param Entities\Item $items
*/
public function removeItem(\Entities\Item $items)
{
$this->items->removeElement($items);
}
/**
* Get items
*
* @return Doctrine\Common\Collections\Collection
*/
public function getItems()
{
return $this->items;
}
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $fields;
/**
* Add fields
*
* @param Entities\Field $fields
* @return Type
*/
public function addField(\Entities\Field $fields)
{
$this->fields[] = $fields;
return $this;
}
/**
* Remove fields
*
* @param Entities\Field $fields
*/
public function removeField(\Entities\Field $fields)
{
$this->fields->removeElement($fields);
}
/**
* Get fields
*
* @return Doctrine\Common\Collections\Collection
*/
public function getFields()
{
return $this->fields;
}
/**
* @var string $slug
*/
private $slug;
/**
* Set slug
*
* @param string $slug
* @return Type
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}