Symfony2 - 调用未定义的方法FOSUserBundle

时间:2013-12-30 07:01:01

标签: php symfony fosuserbundle symfony-forms

我能够集成FOSUserBundle并更新数据库架构。但是,当我按照我在路由文件中指定的那样进入注册路径(/ registration)时,它会向我显示:

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; 

这是我的config.yml文件:

   imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    #translator:      { fallback: %locale% }
    secret:          %secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: %kernel.debug%
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_proxies: ~
    session:
        handler_id:  session.handler.pdo
    fragments:       ~

services:
    pdo:
        class: PDO
        arguments:
            dsn:      "mysql:dbname=%database_name%"
            user:     %database_user%
            password: %database_password%

    session.handler.pdo:
        class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
        arguments: [@pdo, %pdo.db_options%]

# Twig Configuration
twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: %kernel.root_dir%/Resources/java/compiler.jar
        #yui_css:
        #    jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        mapping_types:
            enum: string
            set: string
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: %kernel.root_dir%/data/data.db3
        # path:     %database_path%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    spool:     { type: memory }

# SncRedis Configuration
snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: redis://localhost
        commands:
            type: predis
            alias: commands
            dsn: redis://localhost/1
        responses:
            type: predis
            alias: responses
            dsn: redis://localhost/2
        sides:
            type: predis
            alias: sides
            dsn: redis://localhost/3
        programs:
            type: predis
            alias: programs
            dsn: redis://localhost/4
        fitting_commands:
            type: predis
            alias: fitting_commands
            dsn: redis://localhost/5
        fitting_responses:
            type: predis
            alias: fitting_responses
            dsn: redis://localhost/6

monolog:
    handlers:
        main:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: MyProject\UserBundle\Entity\User

这是我的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;

有人可以帮我解决这个问题吗?感谢

1 个答案:

答案 0 :(得分:0)

您应该从FOSUserBundle扩展AbstractUser类。只需将“extends AbstractUser”添加到您的班级,您就可以了。

您可以使用自己的类(不扩展),但是您必须实现其他一些字段,因为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;