gpt4 book ai didi

symfony - 可捕获的 fatal error : Argument 1 passed to ? Symfony2

转载 作者:行者123 更新时间:2023-12-01 11:02:34 25 4
gpt4 key购买 nike

我对下面的错误消息感到困惑和沮丧:

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

我认为这个错误是由于在我的实体中使用了 manytoone 字段,我什至尝试在另一个实体中保留 onetomany。

我有一个用户实体和一个用户类型实体,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;
}
}

Controller

这在用户想要登录时执行。他会填写用户名密码和一个UserType:

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, ))

;
}

最佳答案

'multiple' => true 与您的实体关联定义相结合导致了此问题。

您应该会发现,如果将 multiple 更改为 false(因此只能为您的用户选择一个 UserType),一切都会正常进行。

如果您希望一个用户有多个 UserType,则您有一个多对多关联 - 一个用户可以有多个 UserType,一个 UserType 可以有多个用户。请参阅 Doctrine 的 ManyToMany 关联类型来实现这一点。 Documentation Here .

希望这对您有所帮助。

关于symfony - 可捕获的 fatal error : Argument 1 passed to ? Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9412961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com