2016-02-19 58 views
0

我正在尝试在asymfony2表单类型中使用doctrine。如何在symfony2中使用Doctrine AbstractType

在services.yml我:

fyp_user.profile.form.type: 
    class: FYP\UserBundle\Form\Type\ProfileFormType 
    arguments: 
     - fos_user.model.user.class 
     - @doctrine.orm.entity_manager 
    tags: 
     - { name: form.type, alias: fyp_user_profile } 

我班的样子:

use Doctrine\ORM\EntityManager; 

class ProfileFormType extends AbstractType 
{ 
    private $em; 
    private $session; 

    public function __construct(EntityManager $em) 
    { 
     $this->em = $em; 
    } 

我做这样的事情:

$cust = $this->em->getRepository('MyBundle:Customers')->findOneByEmail($email); 

我不断收到

Type error: Argument 1 passed to FYP\UserBundle\Form\Type\ProfileFormType::__construct() must be an instance of Doctrine\ORM\EntityManager, string given, called in /Applications/MAMP/htdocs/upgrade/app/cache/dev/appDevDebugProjectContainer.php on line 3594 

谢谢。

回答

2

服务定义和类构造函数之间存在不匹配。在服务定义中,您有两个参数:fos_user.model.user.class@doctrine.orm.entity_manager,并且您的构造函数只接受一个参数(EntityManager)。只需从服务定义中的arguments列表中删除第一个条目,您就会很好