symfony3.4警告:spl_object_hash()期望参数1为对象,给定字符串

时间:2019-03-11 08:49:48

标签: symfony-3.4

当我尝试转到注册页面时,出现此错误

  

警告:spl_object_hash()期望参数1为对象,给定的字符串,HTTP 500内部服务器错误,未捕获的PHP异常Symfony \ Component \ Debug \ Exception \ ContextErrorException

我知道这类问题已经在堆栈溢出中发布了很多,但仍然无法解决我的问题。这就是为什么我在这里再次询问,希望有人可以帮助我。

这是我的注册控制器代码EmployeController

public function newAction(Request $request)
{
    $employe = new Employe();
    /*$user = new User();*/
    $form = $this->createForm('AppBundle\Form\EmployeType', $employe);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) { 
        $em = $this->getDoctrine()->getManager()->getRepository(Employe::class);
        /*$em2 = $this->getDoctrine()->getManager()->getRepository(User::class)->findBy(['roles']);*/
        $vars = explode('@', $employe->getUser()->getEmail());
        $employe->getUser()->setUsername($vars[0] . date('s'));
        /*$employe->setUser($user->setRoles($em2));*/
        $em->persist($employe);
        $em->flush();

        return $this->redirectToRoute('employe_show', array('id' => $employe->getId()));
    }

    return $this->render('employe/new.html.twig', array(
        'employe' => $employe,
        'form' => $form->createView(),
    ));
}

有关更多错误提示,请点击此处

$form = $this->createForm('AppBundle\Form\EmployeType', $employe);

这是我的注册formType(EmployeType.php)

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->setAction($options['action'])
        ->add('nom')
        ->add('prenom')
        ->add('tel')
        ->add('email')
        ->add('user', EntityType::class, [
            'class' => 'AppBundle\Entity\User',
            'choice_label' => function (User $user){
            return $user->getRoles();
            },
            'choices' => [
                'Employé' => 'ROLE_USER',
                'Responsable' => 'ROLE_ADMIN',
                'GRH' => 'ROLE_SUPER_ADMIN',
            ],
            'label' => 'Rôle',
            'multiple' => true,
            'expanded' => false
        ]);

我的实体Employe

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Employe
 *
 * @ORM\Table(name="employes")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\EmployeRepository")
 */
class Employe
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     */
    protected $nom;

    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=255)
     */
    protected $prenom;

    /**
     * @var string
     *
     * @ORM\Column(name="tel", type="string", length=255, unique=true)
     */
    protected $tel;

    /**
     * @ORM\OneToOne(targetEntity="User", mappedBy="employe")
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="Conge", mappedBy="employe")
     */
    private $conges;

    /**
     * @ORM\OneToMany(targetEntity="Affecter", mappedBy="employe")
     */
    private $affecters;

和实体用户

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var array
     */
    //protected $roles;


    /**
     * @ORM\OneToOne(targetEntity="Employe", inversedBy="user", cascade={"persist"})
     * @ORM\JoinColumn(name="employe_id", referencedColumnName="id", nullable=false)
     */
    private $employe;

    public function __construct()
    {
        parent::__construct();

        $this->setPlainPassword(substr(uniqid(), 0, 7));
    }

BN:我使用的是FOSUser

0 个答案:

没有答案