Php Symfony2-调用未定义的方法FOSUserBundle
我能够集成FOSUserBundle并更新db模式。但是,当我转到我在路由文件中指定的注册路径(/registration)时,它会显示以下内容:Php Symfony2-调用未定义的方法FOSUserBundle,php,symfony,fosuserbundle,symfony-forms,Php,Symfony,Fosuserbundle,Symfony Forms,我能够集成FOSUserBundle并更新db模式。但是,当我转到我在路由文件中指定的注册路径(/registration)时,它会显示以下内容: FatalErrorException: Error: Call to undefined method MyProject\UserBundle\Entity\User::setEnabled() in /var/www/project-symfony/projectSymfony/vendor/friendsofsymfony/user-bund
FatalErrorException: Error: Call to undefined method MyProject\UserBundle\Entity\User::setEnabled() in /var/www/project-symfony/projectSymfony/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Controller/RegistrationController.php line 44.
这是我的主要用户类(User.php),位于MyProject/UserBundle/Entity下
<?php
namespace MyProject\UserBundle\Entity;
use FOS\UserBundle\Model\User as AbstractUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* User
*
* @ORM\Table(name="mydb_users")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @UniqueEntity("username")
*/
class User implements AdvancedUserInterface, EquatableInterface, \Serializable
{
/**
* Constructor
*/
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->groups = new ArrayCollection();
$this->hearingTestUsers = new ArrayCollection();
$this->hearingTestBalances = new ArrayCollection();
$this->actualDeviceUsers = new ArrayCollection();
$this->hearingAidUsers = new ArrayCollection();
$this->questionnaireUsers = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->audiograms = new ArrayCollection();
}
/**
* To string
*
* @return string
*/
public function __toString()
{
return $this->username;
}
/**
* @var integer
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=25, unique=true)
*/
protected $username;
/**
* @var string
*
* @ORM\Column(type="string", length=32)
*/
protected $salt;
/**
* @var string
*
* @ORM\Column(type="string", length=40)
*/
protected $password;
这是我的user.php,位于FOSBundle/Model/user.php下
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Model;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Storage agnostic user object
*
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class User implements UserInterface, GroupableInterface
{
protected $id;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $usernameCanonical;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $emailCanonical;
/**
* @var boolean
*/
protected $enabled;
/**
* The salt to use for hashing
*
* @var string
*/
protected $salt;
/**
* Encrypted password. Must be persisted.
*
* @var string
*/
protected $password;
/**
* Plain password. Used for model validation. Must not be persisted.
*
* @var string
*/
protected $plainPassword;
/**
* @var \DateTime
*/
protected $lastLogin;
/**
* Random string sent to the user email address in order to verify it
*
* @var string
*/
protected $confirmationToken;
/**
* @var \DateTime
*/
protected $passwordRequestedAt;
/**
* @var Collection
*/
protected $groups;
/**
* @var boolean
*/
protected $locked;
/**
* @var boolean
*/
protected $expired;
/**
* @var \DateTime
*/
protected $expiresAt;
/**
* @var array
*/
protected $roles;
您应该从FOSUserBundle扩展AbstractUser类。只需在类中添加“extensedabstractuser”,就可以了
您可以使用自己的类(不扩展),但必须实现一些其他字段,因为RegistrationController依赖于这些字段
您的类可能被重写如下
<?php
namespace MyProject\UserBundle\Entity;
use FOS\UserBundle\Model\User as AbstractUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* User
*
* @ORM\Table(name="mydb_users")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @UniqueEntity("username")
*/
class User extends AbstractUser implements AdvancedUserInterface, EquatableInterface, \Serializable
{
/**
* Constructor
*/
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->groups = new ArrayCollection();
$this->hearingTestUsers = new ArrayCollection();
$this->hearingTestBalances = new ArrayCollection();
$this->actualDeviceUsers = new ArrayCollection();
$this->hearingAidUsers = new ArrayCollection();
$this->questionnaireUsers = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->audiograms = new ArrayCollection();
}
/**
* To string
*
* @return string
*/
public function __toString()
{
return $this->username;
}
/**
* @var integer
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=25, unique=true)
*/
protected $username;
/**
* @var string
*
* @ORM\Column(type="string", length=32)
*/
protected $salt;
/**
* @var string
*
* @ORM\Column(type="string", length=40)
*/
protected $password;
非常感谢。我延长了时间,现在似乎在工作。我现在面临的问题是因为我有两个用户类。用户包上的一个和FOSUserBundle附带的user.php文件。我确实有一些重复项,例如$username$plainpassword$Password。当我从自己的user.php文件中删除它们时,它可以工作,但会给我SQL错误。我怎样才能避免这种情况?谢谢
<?php
namespace MyProject\UserBundle\Entity;
use FOS\UserBundle\Model\User as AbstractUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* User
*
* @ORM\Table(name="mydb_users")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @UniqueEntity("username")
*/
class User extends AbstractUser implements AdvancedUserInterface, EquatableInterface, \Serializable
{
/**
* Constructor
*/
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->groups = new ArrayCollection();
$this->hearingTestUsers = new ArrayCollection();
$this->hearingTestBalances = new ArrayCollection();
$this->actualDeviceUsers = new ArrayCollection();
$this->hearingAidUsers = new ArrayCollection();
$this->questionnaireUsers = new ArrayCollection();
$this->sessions = new ArrayCollection();
$this->audiograms = new ArrayCollection();
}
/**
* To string
*
* @return string
*/
public function __toString()
{
return $this->username;
}
/**
* @var integer
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(type="string", length=25, unique=true)
*/
protected $username;
/**
* @var string
*
* @ORM\Column(type="string", length=32)
*/
protected $salt;
/**
* @var string
*
* @ORM\Column(type="string", length=40)
*/
protected $password;