2012-02-23 56 views
3

我堅持和沮喪與​​波紋管的錯誤消息:可捕捉的致命錯誤:參數1傳遞給? Symfony2的

Catchable Fatal Error: Argument 1 passed to Medicine\UserBundle\Entity\User ::setUsertype() must be an instance of Medicine\UserBundle\Entity\Usertype, instance of Doctrine\Common\Collections\ArrayCollection given, called in /opt/lampp/htdocs/drugs/vendor/symfony/src/Symfony/Component/Form/Util /PropertyPath.php on line 347 and defined in /opt/lampp/htdocs/drugs/src/ Medicine/UserBundle/Entity/User.php line 224

我認爲這個錯誤是由於我的實體使用多對一場的,我甚至與另一個實體保持一對多嘗試。

我有一個用戶實體和一個usertype實體,usertype_id是用戶表中的manytoone字段。這裏是兩個實體的代碼: -

用戶

namespace Medicine\UserBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UserRepository") 
* @ORM\Table(name="user") 
* @ORM\HasLifecycleCallbacks() 
*/ 

class User 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
* @ORM\HasLifecycleCallbacks() 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $username; 

/** 
* @ORM\ManyToOne(targetEntity="Usertype", inversedBy="users") 
* @ORM\JoinColumn(name="usertype_id", referencedColumnName="id") 
*/ 
protected $usertype; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $image; 

/** 
* Set usertype 
* 
* @param Medicine\UserBundle\Entity\Usertype $usertype 
*/ 
public function setUsertype(\Medicine\UserBundle\Entity\Usertype $usertype) 
{ 
    $this->usertype = $usertype; 
} 

/** 
* Get usertype 
* 
* @return Medicine\UserBundle\Entity\Usertype 
*/ 
public function getUsertype() 
{ 
    return $this->usertype; 
} 
} 

我只是展示有關的代碼,我有所有上面的代碼的getter和setter方法。

用戶類型

namespace Medicine\UserBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UsertypeRepository") 
* @ORM\Table(name="usertype") 
* @ORM\HasLifecycleCallbacks() 
*/ 

class Usertype 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
* @ORM\HasLifecycleCallbacks() 
*/ 
protected $id; 

/** 
* @ORM\Column(type="string") 
*/ 
protected $name; 

/** 
* @ORM\OneToMany(targetEntity="User", mappedBy="usertype") 
*/ 
protected $users; 

public function __construct() 
{ 
    $this->users = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

/** 
* Add users 
* 
* @param Medicine\UserBundle\Entity\User $users 
*/ 
public function addUser(\Medicine\UserBundle\Entity\User $users) 
{ 
    $this->users[] = $users; 
} 

/** 
* Get users 
* 
* @return Doctrine\Common\Collections\Collection 
*/ 
public function getUsers() 
{ 
    return $this->users; 
} 
} 

控制器

當用戶想要登錄時,該執行。他將填寫的用戶名密碼和用戶類型:

public function indexAction() 
{ 
$entity = new User(); 
    $form = $this->createForm(new LoginForm(), $entity); 
    $request = $this->getRequest(); 
    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 
     if ($form->isValid()) { 
     echo "<pre>"; print_r($entity->getUsertype()); exit; 
     $em = $this->getDoctrine() 
       ->getEntityManager(); 
     $em->persist($entity); 
     $userrepository = $em->getRepository('MedicineUserBundle:User'); 
     echo "<pre>"; print_r($entity->getUsertype()); exit; 
     $all = $userrepository->findOneBy(array('login' => $entity->getLogin(), 'password' => $entity->getPassword())); 
     if($all) 
     { 
      return $this->redirect($this->generateUrl('MedicineUserBundle_login')); 
     } 
     } 
    } 

    return $this->render('MedicineUserBundle:User:loginpage.html.twig',array(
     'form' => $form->createView() 
    )); 
} 

登錄表單

public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder 
    ->add('login', 'text', array('label' => 'Username',)) 
     ->add('password','password') 
    ->add('usertype', 'entity', array('class' => 'MedicineUserBundle:Usertype', 'property'=>'name', 'multiple' => true,)) 

    ; 
} 
+0

我認爲問題在於您的LoginForm - 我們可以看到這個嗎? – jstephenson 2012-02-23 13:19:34

+0

@jstephenson: - 我用登錄表單更新了我的問題 – ScoRpion 2012-02-23 13:25:18

回答

6

與你的實體關聯定義組合的'multiple' => true引起此問題。

您應該發現,如果您將多個更改爲false(並且因此只能爲您的用戶選擇一個UserType),那麼事情就可以正常工作。

如果您需要一個用戶的多個UserType,那麼您有多對多關聯 - 一個用戶可以有多個UserType,一個UserType可以有多個用戶。請參閱Doctrine的ManyToMany關聯類型來實現此目的。 Documentation Here

希望這會有所幫助。

相關問題