zf2 fieldset ArrayCollection 是空的,而它应该被填充
zf2 fieldset ArrayCollection is empty while it is expected to be filled
我有一个表单 ProductForm,在该表单中我调用了基本字段集 ProductFieldset,在 ProductFieldset 中我调用了 CategoryFieldset。实体之间的关系是OneToOne。
实体如下:
namespace Trunk\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var integer
*
* @ORM\Column(name="CategoryId", type="integer", nullable=false)
*/
protected $categoryid;
/**
* @ORM\OneToOne(targetEntity="Trunk\Entity\Category", mappedBy="product")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;
public function __construct() {
$this->category = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return int
*/
public function getCategoryid()
{
return $this->categoryid;
}
/**
* @param int $categoryid
*/
public function setCategoryid($categoryid)
{
$this->categoryid = $categoryid;
}
public function addCategory(Category $category)
{
if (! $this->category->contains($category)) {
$this->category->add($category);
}
return $this;
}
public function removeCategory(Category $category)
{
if ($this->category->contains($category)) {
$this->category->removeElement($category);
}
return $this;
}
public function getCategory()
{
return $this->category;
}
}
这是类别实体:
namespace Trunk\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="ParentId", type="integer", nullable=false)
*/
private $parentid = '0';
/**
* @var string
*
* @ORM\Column(name="Title", type="string", length=255, nullable=false)
*/
private $title;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Category
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
表单提交后的结果为:
object(Trunk\Entity\Product)[480]
protected 'id' => null
protected 'categoryid' => null
protected 'category' =>
object(Doctrine\Common\Collections\ArrayCollection)[482]
private 'elements' =>
array (size=0)
empty
我用过 Doctrine 保湿剂,但没有效果。
你能告诉我一些想法为什么总是空的吗?提前致谢!
More code
$form = $this->form;
$form->setAttribute('action', $this->url . '/admin/holiday/add/bg');
$form->prepare();
echo $this->form()->openTag($form);
$holiday = $form->get('product');
echo $this->formElement($holiday->get('category')->get('categoryid'));
echo $this->form()->closeTag();
这是控制器中的添加动作:
public function addAction()
{
$oRequest = $this->getRequest();
if ($oRequest->isPost()) {
$aPost = $oRequest->getPost('product');
$this->productForm->setData($oRequest->getPost());
if ($this->productForm->isValid()) {
try {
$result = $this->productService->addProduct($this->productForm->getData());
if ($result) {
return $this->redirect()->toRoute('trunk', array('controller' => 'product', 'action' => 'edit', 'id' => $result->getLang(), 'param' => $result->getId()));
}
} catch (\Exception $e) {
// some DB error happened,, log it and let the user know
exit;
}
}
}
return new ViewModel(array(
'form' => $this->productForm,
));
}
下面的代码用于水化 CategoryFieldset 中的对象
$this->setHydrator(new DoctrineHydrator($this->getObjectManager(), 'Trunk\Entity\Category'))
->setObject(new Category());
这就是我在 ProductFieldset 中添加 CategoryFieldset 的方式
$this->add(array(
'type' => 'Trunk\Form\CategoryFieldset',
'name' => 'category'
));
编辑 1:
ProductEntity 中的 ManyToOne 关系
/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;
CategoryEntity 中的 OneToMany 关系
/**
* @ORM\OneToMany(targetEntity="Trunk\Entity\Product", mappedBy="category")
*/
protected $products;
我收到以下错误
Could not resolve type of column "id" of class "Trunk\Entity\Category"
我检查了 PersistHelper.php getTypeOfColumn 函数中的代码,发现它正在寻找 joinColumns。
The question is resolved!
要修复我上次发布的关系错误,我必须在 ProductEntity 中使用此注释
/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="CategoryId", referencedColumnName="Id")
*/
protected $category;
换句话说,我将引用列名称从 "id" 更改为 Id(区分大小写)。
希望这个问题和例子对其他同事也有帮助。
我有一个表单 ProductForm,在该表单中我调用了基本字段集 ProductFieldset,在 ProductFieldset 中我调用了 CategoryFieldset。实体之间的关系是OneToOne。
实体如下:
namespace Trunk\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity
*/
class Product
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var integer
*
* @ORM\Column(name="CategoryId", type="integer", nullable=false)
*/
protected $categoryid;
/**
* @ORM\OneToOne(targetEntity="Trunk\Entity\Category", mappedBy="product")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;
public function __construct() {
$this->category = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return int
*/
public function getCategoryid()
{
return $this->categoryid;
}
/**
* @param int $categoryid
*/
public function setCategoryid($categoryid)
{
$this->categoryid = $categoryid;
}
public function addCategory(Category $category)
{
if (! $this->category->contains($category)) {
$this->category->add($category);
}
return $this;
}
public function removeCategory(Category $category)
{
if ($this->category->contains($category)) {
$this->category->removeElement($category);
}
return $this;
}
public function getCategory()
{
return $this->category;
}
}
这是类别实体:
namespace Trunk\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="Id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer
*
* @ORM\Column(name="ParentId", type="integer", nullable=false)
*/
private $parentid = '0';
/**
* @var string
*
* @ORM\Column(name="Title", type="string", length=255, nullable=false)
*/
private $title;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Category
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
表单提交后的结果为:
object(Trunk\Entity\Product)[480]
protected 'id' => null
protected 'categoryid' => null
protected 'category' =>
object(Doctrine\Common\Collections\ArrayCollection)[482]
private 'elements' =>
array (size=0)
empty
我用过 Doctrine 保湿剂,但没有效果。
你能告诉我一些想法为什么总是空的吗?提前致谢!
More code
$form = $this->form;
$form->setAttribute('action', $this->url . '/admin/holiday/add/bg');
$form->prepare();
echo $this->form()->openTag($form);
$holiday = $form->get('product');
echo $this->formElement($holiday->get('category')->get('categoryid'));
echo $this->form()->closeTag();
这是控制器中的添加动作:
public function addAction()
{
$oRequest = $this->getRequest();
if ($oRequest->isPost()) {
$aPost = $oRequest->getPost('product');
$this->productForm->setData($oRequest->getPost());
if ($this->productForm->isValid()) {
try {
$result = $this->productService->addProduct($this->productForm->getData());
if ($result) {
return $this->redirect()->toRoute('trunk', array('controller' => 'product', 'action' => 'edit', 'id' => $result->getLang(), 'param' => $result->getId()));
}
} catch (\Exception $e) {
// some DB error happened,, log it and let the user know
exit;
}
}
}
return new ViewModel(array(
'form' => $this->productForm,
));
}
下面的代码用于水化 CategoryFieldset 中的对象
$this->setHydrator(new DoctrineHydrator($this->getObjectManager(), 'Trunk\Entity\Category'))
->setObject(new Category());
这就是我在 ProductFieldset 中添加 CategoryFieldset 的方式
$this->add(array(
'type' => 'Trunk\Form\CategoryFieldset',
'name' => 'category'
));
编辑 1:
ProductEntity 中的 ManyToOne 关系
/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="categoryid", referencedColumnName="id")
*/
protected $category;
CategoryEntity 中的 OneToMany 关系
/**
* @ORM\OneToMany(targetEntity="Trunk\Entity\Product", mappedBy="category")
*/
protected $products;
我收到以下错误
Could not resolve type of column "id" of class "Trunk\Entity\Category"
我检查了 PersistHelper.php getTypeOfColumn 函数中的代码,发现它正在寻找 joinColumns。
The question is resolved!
要修复我上次发布的关系错误,我必须在 ProductEntity 中使用此注释
/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="CategoryId", referencedColumnName="Id")
*/
protected $category;
换句话说,我将引用列名称从 "id" 更改为 Id(区分大小写)。
希望这个问题和例子对其他同事也有帮助。