将用户保存到frosuserbundle中的行


save user to row from frosuserbundle

我得到以下错误:

Catchable Fatal Error: Argument 1 passed to Rowoco'AllgemeinBundle'Entity'Place::setIdUser() must 
be an instance of Rowoco'AllgemeinBundle'Entity'FosUser, instance of   
Rowoco'UserBundle'Entity'User given, called in 
/var/www/symfony/src/Rowoco/AllgemeinBundle/Controller/AllgemeinAjaxController.php on line 64 and 
defined in /var/www/symfony/src/Rowoco/AllgemeinBundle/Entity/Place.php line 331

在以下功能中,我尝试添加一个新位置:

public function test()
{
    $em = $this->getDoctrine()->getManager();
    $stateRepo = $em->getRepository( "RowocoAllgemeinBundle:State" );
    $stateEntity = $stateRepo->find(1);
    $user = $this->get('security.context')->getToken()->getUser();
    $place = new Place();
    $place->setAdddate( "2014-01-01 12:12:10" );
    $place->setTitle( "test" );
    $place->setDescription( "testdescription" );
    $place->setSpecial( "special" );
    $place->setStreet( "strasse" );
    $place->setZipcode( "12345" );
    $place->setIdState( $stateEntity );
    $place->setIdUser( $user );

    $em->persist( $place );
    $em->flush();
    //return new Response('Created product id '.$product->getId());
    return true;
}

在桌子的位置,我有两个外国钥匙

  1. id状态
  2. id用户

如何修复此错误?

代码中的一些内容:

  • 首先,错误是告诉你问题出在哪里,看看这一行:

可捕获的致命错误:参数1传递到Rowoco''AllgemeinBundle''Entity''Place::setIdUser()必须是Rowoco''AllgemeinBundle''Entity''FosUser的实例Rowoco''UserBundle''实体''用户

您正在将一个完整的对象传递给setIdUser()方法,BTW期望FOSUser实例

  • 其次,如果您使用的是Symfony>2.1.x,则应该使用$this->getUser(),而不是像现在这样(这是一个提示)

  • 作为第三个,如果您只需要插入id_user作为有问题的状态,为什么要传递整个对象?为什么不直接传递当前登录的用户id?类似$this->getUser()->getId()的东西?