这是modiffilmAction()的一部分,用于编辑电影片名和演员。 Films和Actors之间存在ManyToMany关联。只有标题填写表格。如果此代码可以根据您的要求工作,您能告诉我如何填写表格吗?或者这段代码有什么问题。我没有找到任何例子。
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb ->select(array('f','a'))
->from('Cine\Entity\Films', 'f')
->where($qb->expr()->eq('f.id', '?1'))
->innerJoin('f.actors', 'a')
->setParameter(1, $id);
$query = $qb->getQuery();
$film = $query->getResult();
$form = new FilmForm($em);
$form->bind($film);
...
如果我替换查询, $ film = $ em-> find('Cine \ Entity \ Films',$ id); 在关联中使用fetch =“eager”,结果相同:没有绑定数据。
我想我还没有理解一些非常简单的东西,但我是zf2和doctrine 2的新手...... 谢谢你的帮助
答案 0 :(得分:0)
尝试这样的事情;
控制器操作:
public function editAction()
{
$film = new Film;
if ($this->params('id') > 0) {
$film = $this->getEntityManager()->getRepository('Cine\Entity\Film')->find($this->params('id'));
}
$form = new FilmForm($this->getEntityManager());
$form->setHydrator(new DoctrineEntity($this->getEntityManager(),'Cine\Entity\Film'));
$form->bind($film);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($film->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$em = $this->getEntityManager();
$em->persist($film);
$em->flush();
$this->flashMessenger()->addSuccessMessage('Film Saved');
return $this->redirect()->toRoute('cine/default', array('controller' => 'film','action' => 'index'));
}
}
return new ViewModel(array(
'film' => $film,
'form' => $form
));
}
<强>形式:强>
<?php
namespace Cine\Form;
use Doctrine\ORM\EntityManager;
use Zend\Form\Form;
use Zend\Form\Element;
class FilmForm extends Form
{
public function __construct(EntityManager $em)
{
parent::__construct('film');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
$this->add(array(
'name' => 'title',
'type' => 'text',
'options' => array('label' => 'Film Title'),
'attributes' => array(
'class' => 'input-xxlarge'
)
));
$this->add(array(
'name' => 'actors',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => array(
'label' => 'Actors',
'empty_option' => 'Select Actors',
'object_manager' => $em,
'target_class' => 'Cine\Entity\Actor',
'property' => 'name'
),
'attributes' => array(
'multiple' => 'multiple',
'required' => false,
'class' => 'chosen'
)
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
),
));
}
}
查看:强>
<?php
$title = 'Edit Film';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php $form->setAttribute('action', $this->url('cine/default', array('controller' => 'film', 'action' => 'edit', 'id' => $film->getId())));
$form->setAttribute('class', 'form-horizontal');
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
//Film Title
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('actors'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>