【发布时间】:2016-12-13 01:24:36
【问题描述】:
我有一个表单 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 保湿剂,但没有任何效果。
你能给我一些想法,为什么总是空的?提前谢谢!
更多代码
$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 中的多对一关系
/**
* @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;
我收到以下错误
无法解析类“Trunk\Entity\Category”的列“id”的类型
我检查了 PersistHelper.php getTypeOfColumn 函数中的代码,发现它正在寻找 joinColumns。
【问题讨论】:
-
您是否在视图中使用 $form->prepare() 准备表单?
-
嗨@Hooli,我已经从视图和控制器中添加了代码。你认为我遗漏了什么吗?
-
您可能需要查看
Product#categories的映射声明。您将其声明为与类别严格的一对一关系(即产品有一个类别,每个类别只有一个产品)。然而你在谈论集合,这表明这可能是一种多对一的关系。 -
嗨@Fge 我已经修复了这种关系,因为你注意到我使用了错误的一对一。我需要多对一。我将在上面添加这些更改。
标签: php forms doctrine-orm zend-framework2 entity-relationship