我有一个表单ProductForm,在该表单中我调用了base fieldset 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中的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;
我收到以下错误
无法解析列的类型" id"类" Trunk \ Entity \ Category"
我检查了PersistHelper.php getTypeOfColumn函数中的代码,发现它正在寻找joinColumns。
答案 0 :(得分:0)
问题已解决!
要修复我上次发布的关系错误,我必须在ProductEntity中使用此注释
/**
* @ORM\ManyToOne(targetEntity="Trunk\Entity\Category", inversedBy="products")
* @ORM\JoinColumn(name="CategoryId", referencedColumnName="Id")
*/
protected $category;
换句话说,我将引用列名称从“id”更改为Id(区分大小写)。
我希望这个问题和例子能够帮助其他同事。