无法识别的字段:facebookId

Unrecognized field: facebookId

我尝试将 symfony 3 与 HWIOAUTH Bundle 连接起来并遵循教程 https://gist.github.com/danvbe/4476697

文件config.yml

hwi_oauth:
connect:
    account_connector: my_user_provider
firewall_names: [main]
fosub:

    username_iterations: 30


    properties:
        google: googleId
        facebook: facebookId
resource_owners:
    facebook:
        type:                facebook
        client_id:           ***************
        client_secret:       ***************
        scope:               ""
    google:
        type:                google
        client_id:           ***************
        client_secret:      ***************
        scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"

security.yml

main:
        pattern:             .*
        context:             user
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path: list_items
            always_use_default_target_path: true
        oauth:
            resource_owners:
                facebook:           "/connect/check-facebook"
                google:             "/connect/check-google"
            login_path:        /connect
            failure_path:      /connect

            oauth_user_provider:
                #this is my custom user provider, created from FOSUBUserProvider - will manage the
                #automatic user registration on your site, with data from the provider (facebook. google, etc.)
                service: my_user_provider
        logout:             true
        anonymous:          true

service.yml

my_user_provider:
    class: AppBundle\Entity\FOSUBUserProvider
    arguments: ['@fos_user.user_manager',{facebook: facebookId, google: googleId}]

请帮我修复这个错误。我花了很多次,但无法修复它。

更新: 实体用户

 /**
 * @var string
 *
 * @ORM\Column(name="facebookId", type="string", nullable=true)
 */
private $facebookId;

首先更新您的架构并在数据库中查看,您是否在用户 table 中找到 facebookId 字段?

如果用户中没有facebookId这样的字段。然后更新您的实体注释如下:

/**
 * @var string
 *
 * @ORM\Column(name="facebookId", type="string", nullable=true)
 */
private $facebookId;

注意注释注释的变化。请参阅“/**”行后“*”之前的 space。我没有使用过 Symfony 3,但在使用 Symfony 2.8 时遇到了类似的问题,所以根据我的经验,我建议。可能有帮助!!

我使用 symfony 3.3.15,这是正确的配置:

//User.php
<?php

namespace BackendBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

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

    /**
     * @var string
     *
     * @ORM\Column(name="facebook_id", type="string", length=255, nullable=true)
     */
    private $facebook_id;

    /**
     * @var string
     *
     * @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true)
     */
    private $facebook_access_token;

    /**
     * @var string
     *
     * @ORM\Column(name="google_id", type="string", length=255, nullable=true)
     */
    private $google_id;

    /**
     * @var string
     *
     * @ORM\Column(name="google_access_token", type="string", length=255, nullable=true)
     */
    private $google_access_token;


    /**
     * Set facebook_id
     *
     * @param string $facebook_id
     *
     * @return User
     */
    public function setFacebookId($facebook_id)
    {
        $this->facebook_id = $facebook_id;

        return $this;
    }

    /**
     * Get facebook_id
     *
     * @return string
     */
    public function getFacebookId()
    {
        return $this->facebook_id;
    }

    /**
     * Set facebook_access_token
     *
     * @param string $facebook_access_token
     *
     * @return User
     */
    public function setFacebookAccessToken($facebook_access_token)
    {
        $this->facebook_access_token = $facebook_access_token;

        return $this;
    }

    /**
     * Get facebook_access_token
     *
     * @return string
     */
    public function getFacebookAccessToken()
    {
        return $this->facebook_access_token;
    }

    /**
     * Set google_id
     *
     * @param string $google_id
     *
     * @return User
     */
    public function setGoogleId($google_id)
    {
        $this->google_id = $google_id;

        return $this;
    }

    /**
     * Get google_id
     *
     * @return string
     */
    public function getGoogleId()
    {
        return $this->google_id;
    }

    /**
     * Set google_access_token
     *
     * @param string $google_access_token
     *
     * @return User
     */
    public function setGoogleAccessToken($google_access_token)
    {
        $this->google_access_token = $google_access_token;

        return $this;
    }

    /**
     * Get google_access_token
     *
     * @return string
     */
    public function getGoogleAccessToken()
    {
        return $this->google_access_token;
    }

}

其他文件services.yml:

//services.yml
services:
    my_user_provider:
        class: AppBundle\Security\Core\User\FOSUBUserProvider
        arguments: ['@fos_user.user_manager',{facebook: facebook_id,google: google_id}]

下一个文件 FOSUBUserProvider:

//Security/Core/User
<?php
namespace AppBundle\Security\Core\User;

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;

class FOSUBUserProvider extends BaseClass
{
    /**
     * {@inheritDoc}
     */
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
        $username = $response->getUsername();

        $service = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($service);
        $setterId = $setter . 'Id';
        $setterToken = $setter . 'AccessToken';

        //disconnect previously connected user
        if (null !== $previousUser = $this->userManager->findUserBy(array($this->getProperty($response) => $username))) {
            $previousUser->$setterId(null);
            $previousUser->$setterToken(null);
            $this->userManager->updateUser($previousUser);
        }

        //connect current user
        $user->$setterId($username);
        $user->$setterToken($response->getAccessToken());

        $this->userManager->updateUser($user);
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $email = $response->getEmail();

        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        if (null === $user) {
            $user = $this->userManager->findUserByEmail($email);

            $service = $response->getResourceOwner()->getName();
            $setter = 'set' . ucfirst($service);
            $setterId = $setter . 'Id';
            $setterToken = $setter . 'AccessToken';

            if (null === $user) {
                $user = $this->userManager->createUser();
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $user->setUsername($username);
                $user->setEmail($email);
                $user->setPassword($username);
                $user->setEnabled(true);
                $this->userManager->updateUser($user);

                return $user;
            } else {
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $this->userManager->updateUser($user);

                return $user;
            }
        }

        //if user exists - go with the HWIOAuth way
        $user = parent::loadUserByOAuthUserResponse($response);

        $serviceName = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

        //update access token
        $user->$setter($response->getAccessToken());

        return $user;
    }
}

文件config.yml

fosub:
        username_iterations: 30
        properties:
            facebook:    facebook_id
            google:      google_id
    resource_owners:
        facebook:
            type:                facebook
            client_id:           gkhgkjgkjgkjgkj
            client_secret:       kjhkjkjhkjhlkjhlkjh
            #scope:               "email"
            #scope:               ""
            scope:               "email,public_profile,user_friends,user_birthday,user_location"

        google:
            type:                google
            client_id:           kjhlkjhlkjhljhlkjhlk-gkhjgkjhgkjhgkgjhkjhgkjgkjgkjh.apps.googleusercontent.com
            client_secret:       gjgjgj-hkjhgkhgkjh
            #scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
            scope:               "email profile"

下一个文件:security.yml

oauth:
                resource_owners:
                    facebook:           "/login/check-facebook"
                    google:             "/login/check-google"
                login_path:        /login
                failure_path: /login
                oauth_user_provider:
                    service: my_user_provider

此致