可捕获的致命错误:参数 1 传递给 ?Symfony2 [英] Catchable Fatal Error: Argument 1 passed to ? Symfony2

查看:23
本文介绍了可捕获的致命错误:参数 1 传递给 ?Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

可捕获的致命错误:参数 1 传递给 Medicine\UserBundle\Entity\User::setUsertype() 必须是 Medicine\UserBundle\Entity\Usertype 的一个实例,给定的 Doctrine\Common\Collections\ArrayCollection 实例,调用/opt/lampp/htdocs/drugs/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php 在第 347 行并在/opt/lampp/htdocs/drugs/src/中定义Medicine/UserBundle/Entity/User.php 第 224 行

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

我有一个用户实体和一个用户类型实体,usertype_id 是用户表中的一个 manytoone 字段.这是两个实体的代码:-

用户

命名空间 Medicine\UserBundle\Entity;使用 Doctrine\ORM\Mapping 作为 ORM;使用 Doctrine\Common\Collections\ArrayCollection;/*** @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UserRepository")* @ORM\Table(name="user")* @ORM\HasLifecycleCallbacks()*/类用户{/*** @ORM\Id* @ORM\Column(type="整数")* @ORM\GeneratedValue(strategy="AUTO")* @ORM\HasLifecycleCallbacks()*/受保护的 $id;/*** @ORM\Column(type="string")*/受保护的 $ 用户名;/*** @ORM\ManyToOne(targetEntity="Usertype", inversedBy="users")* @ORM\JoinColumn(name="usertype_id", referencedColumnName="id")*/受保护的 $usertype;/*** @ORM\Column(type="string")*/受保护的 $image;/*** 设置用户类型** @param Medicine\UserBundle\Entity\Usertype $usertype*/公共函数 setUsertype(\Medicine\UserBundle\Entity\Usertype $usertype){$this->usertype = $usertype;}/*** 获取用户类型** @return Medicine\UserBundle\Entity\Usertype*/公共函数 getUsertype(){返回 $this->usertype;}}

我只是展示相关代码,我有上述代码的所有 getter 和 setter 方法.

用户类型

命名空间 Medicine\UserBundle\Entity;使用 Doctrine\ORM\Mapping 作为 ORM;使用 Doctrine\Common\Collections\ArrayCollection;/*** @ORM\Entity(repositoryClass="Medicine\UserBundle\Repository\UsertypeRepository")* @ORM\Table(name="usertype")* @ORM\HasLifecycleCallbacks()*/类用户类型{/*** @ORM\Id* @ORM\Column(type="整数")* @ORM\GeneratedValue(strategy="AUTO")* @ORM\HasLifecycleCallbacks()*/受保护的 $id;/*** @ORM\Column(type="string")*/受保护的 $name;/*** @ORM\OneToMany(targetEntity="用户",mappedBy="用户类型")*/受保护的 $users;公共函数 __construct(){$this->users = new \Doctrine\Common\Collections\ArrayCollection();}/*** 添加用户** @param Medicine\UserBundle\Entity\User $users*/公共函数 addUser(\Medicine\UserBundle\Entity\User $users){$this->users[] = $users;}/*** 获取用户** @return Doctrine\Common\Collections\Collection*/公共函数 getUsers(){返回 $this->users;}}

控制器

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

公共函数 indexAction(){$entity = new User();$form = $this->createForm(new LoginForm(), $entity);$request = $this->getRequest();if ($request->getMethod() == 'POST') {$form->bindRequest($request);如果 ($form->isValid()) {echo "

";print_r($entity->getUsertype());出口;$em = $this->getDoctrine()->getEntityManager();$em->persist($entity);$userrepository = $em->getRepository('MedicineUserBundle:User');echo "

";print_r($entity->getUsertype());出口;$all = $userrepository->findOneBy(array('login' => $entity->getLogin(), 'password' => $entity->getPassword()));如果($全部){返回 $this->redirect($this->generateUrl('MedicineUserBundle_login'));}}}return $this->render('MedicineUserBundle:User:loginpage.html.twig',array('形式' =>$form->createView()));}

登录表单

 公共函数 buildForm(FormBuilder $builder, array $options){$builder->add('login', 'text', array('label' => 'Username',))->add('密码','密码')->add('usertype', 'entity', array('class' => 'MedicineUserBundle:Usertype', 'property'=>'name', 'multiple' => true, ));}

解决方案

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

您应该会发现,如果您将 multiple 更改为 false(因此只能为您的 User 选择一种 UserType),则一切正常.

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

希望这会有所帮助.

I am stuck and frustrated with the bellow error message:

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

What I think this error is due to use of manytoone field in my entity, I even tried with keeping onetomany in another entity.

I have a user entity and a usertype entity, the usertype_id is a manytoone field in user table. here is the code for both the entities:-

User

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;
}
}

I am just showing the concerned code, i have all the getter and setter methods for the above code.

UserType

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

This Executes when a user wants to login. He will fill in the username password and a 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()
    ));
}

Login Form

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

    ;
}

解决方案

The 'multiple' => true in combination with your entity association definition is causing this problem.

You should find that if you change multiple to false (and as such can only select one UserType for your User), things work properly.

If you want multiple UserTypes for one User, you have a Many-to-Many association - one user can have many UserTypes, and one UserType can have many Users. See Doctrine's ManyToMany association type to implement this. Documentation Here.

Hope this helps.

这篇关于可捕获的致命错误:参数 1 传递给 ?Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆