我是symfony2的新手,并开始了一个简单的symblog项目来创建帖子和评论,当我创建一个评论的一切都很好,但现在我必须坚持,合并和删除我的帖子和我有这个错误:
捕获致命错误:参数1传递给 NoticiaBundle \ Repository \ NoticiaRepository :: adicionar()必须是 代理实例_CG __ \ NoticiaBundle \ Entity \ Noticia,实例 NoticiaBundle \ Entity \ Noticia给出,调用 C:\ Desenvolvimento \瓦帕\ WWW \ myproject的\ SRC \ NoticiaBundle \控制器\ NoticiaController.php 在第49行并定义
控制器持续功能:
/**
* @Route("/cadastrar_noticia", name="_cadastrar_noticia")
* @Template()
*/
public function cadastrarAction() {
$noticia = new Noticia();
$request = $this->getRequest();
$form = $this->createForm(new NoticiaType(), $noticia);
$form->handleRequest($request);
if ($form->isValid()) {
try {
$noticia = $form->getData();
$noticiaRepository = $this->getDoctrine()->getRepository('NoticiaBundle:Noticia');
$noticiaRepository->adicionar($noticia);
$this->addFlash('success', 'Noticia cadastrada com sucesso');
return $this->redirectToRoute('_imagem_raias_index');
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
return array("form" => $form->createView());
}
持久存储库
public function adicionar(Noticia $noticia) {
$this->_em->persist($noticia);
$this->_em->flush();
return true;
}
我的FormType
<?php
namespace NoticiaBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class NoticiaType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titulo',null, array('attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label col-lg-2')
))
->add('imagem',null, array('attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label col-lg-2')
))
->add('noticia','textarea', array('attr' => array('class' => 'form-control', 'rows' => 15),
'label_attr' => array('class' => 'control-label col-lg-2')
))
->add('tags',null, array('attr' => array('class' => 'form-control'),
'label_attr' => array('class' => 'control-label col-lg-2')
))
->add('salvar', 'submit', array('attr' => array('class' => 'btn btn-primary pull-right')));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'NoticiaBundle\Entity\Noticia'
));
}
/**
* @return string
*/
public function getName()
{
return 'noticiabundle_noticia';
}
}
任何人都可以帮助我吗?
答案 0 :(得分:0)
在您定义adicionar
的存储库中,查看您的use
声明。你需要让它引用实际的模型,而不是代理。