好的,我在Codeigniter中使用Doctrine2沿着Joel Verhagen行,事情一直很好,但现在我有一个神秘的问题。
销毁物品时,工作正常:
$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并查找调用就在getRepository之后立即死亡。将getRepository移动到__construct会导致update()与find一起消失。
思考?信息?似乎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;
}
}
答案 0 :(得分:-1)
我修好了。不幸的是我不知道为什么。如果有人可以向我解释为什么这会解决它(或者什么是开始时被破坏)我会接受你的答案。
加载Item存储库时,它将无提示失败。加载Type存储库,并在其上执行findAll首先修复它。这可能都是因为Items是Type的子项,但是为了能够加载特定的项目,必须事先做一个findAll()似乎很奇怪。
例如,在__construct()中执行此操作:
$this->typeR = $this->doctrine->em->getRepository('Entities\Type');
$data['types'] = $this->typeR->findAll();
$this->itemR = $this->doctrine->em->getRepository('Entities\Item');
允许我在必要时为该控制器加载项目。
同样,这只是在服务器上。在本地,一切都很好。