douduan5753 2015-07-20 23:35
浏览 63
已采纳

可捕获的致命错误:symfony2

i'm new in symfony2 and started a simple project of symblog to create a crud of Posts and comments, when i created a crud of comments everything gone fine, but now i have to persist, merge and remove my posts and i have this error:

Catchable Fatal Error: Argument 1 passed to NoticiaBundle\Repository\NoticiaRepository::adicionar() must be an instance of Proxies__CG__\NoticiaBundle\Entity\Noticia, instance of NoticiaBundle\Entity\Noticia given, called in C:\Desenvolvimento\wamp\www\myproject\src\NoticiaBundle\Controller\NoticiaController.php on line 49 and defined

Controller Persist Function:

/**
 * @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());
}

Persist repository

 public function adicionar(Noticia $noticia) {

    $this->_em->persist($noticia);
    $this->_em->flush();
    return true;

}

And my 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';
    }
}

Anyone can help me??

  • 写回答

1条回答 默认 最新

  • douyinjiao9351 2015-07-20 23:36
    关注

    In your Repository where you defined adicionar, check your use statement. You need to make it refer to the actual model, not the proxy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥50 WPF Lidgren.Network.Core2连接问题
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建