Pat*_*awa 2 symfony-forms symfony
我有一个表单,您可以在其中编辑对象"one"的属性.此对象与另一个对象"很多"具有一对多的关系.我希望用户能够从表单中选择将"many"对象分配给"one".我无法弄明白该怎么做!
马上:
\实体\ One.php
class One
{
...
/*
* @ORM\ManyToOne(targetEntity="many", inversedBy="one")
* @ORM\JoinColumn(name="manyId", referencedColumnName="id")
*/
protected $manyId;
...
}
Run Code Online (Sandbox Code Playgroud)
\控制器\ OneController.php
class OneController extends Controller
{
...
public function editAction($oneId, Request $request)
{
if ($oneId) {
$one = $this->getDoctrine()
->getRepository('One')
->find($oneId);
} else {
$one = new One();
}
$em = $this->getDoctrine()->getEntityManager();
$manyEntity = 'Bundle\Entity\Many';
$manyList = new EntityChoiceList($em, $manyEntity);
$form = $this->createFormBuilder($one)
->add('many', 'choice', array('choice_list' => $manyList))
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$entityManager = $this->getDoctrine()->getEntityManager();
$entityManager->persist($one);
}
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
这导致错误消息"类型的预期参数"标量","Proxies\BundleEntityManyProxy"给定".
谢谢你的帮助!
解决了!我应该写的->add('many', 'entity', array('class' => 'BundleMany'))
.
见http://forum.symfony-project.org/viewtopic.php?f=23&t=36604.
归档时间: |
|
查看次数: |
5533 次 |
最近记录: |