Question

I am trying to create a web service using FOSOAuthServer bundle in symfony2.

I am using yml configuration; so I create entity for AcccessToken like this:

yml:

My\ApiBundle\Entity\AccessToken:
    type:  entity
    table: access_token
    id:
        id:
            type: integer
            generator:
                strategy: AUTO

    manyToOne:
        client:
            targetEntity: Client
            inversedBy: access_tokens
            joinColumn:
                name: client_id
                referencedColumnName: id
                nullable: false

        user:
            targetEntity: User
            inversedBy: access_tokens
            joinColumn:
                name: user_id
                referencedColumnName: id

Entity:

<?php

namespace My\ApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\OAuthServerBundle\Entity\AccessToken as BaseAccessToken;

/**
 * AccessToken
 */
class AccessToken extends BaseAccessToken
{
    /**
     * @var integer
     */
    protected $id;


    /**
     * @var \My\ApiBundle\Entity\Client
     */
    protected $client;


    /**
     * @var \My\ApiBundle\Entity\User
     */
    protected $user;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set client
     *
     * @param \My\ApiBundle\Entity\Client $client
     * @return AccessToken
     */
    public function setClient(\My\ApiBundle\Entity\Client $client)
    {
        $this->client = $client;

        return $this;
    }

    /**
     * Get client
     *
     * @return \My\ApiBundle\Entity\Client 
     */
    public function getClient()
    {
        return $this->client;
    }

    /**
     * Set user
     *
     * @param \My\ApiBundle\Entity\User $user
     * @return AccessToken
     */
    public function setUser(\My\ApiBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \My\ApiBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

Now when I run generate entities command again, it throws this error.

Fatal error: Declaration of My\ApiBundle\Entity\AccessToken::setUser() must be compatible with FOS\OAuthServerBundle\Model\TokenInterface::setUser(Symfony\Component\Security\Core\User\UserInterface $user) in D:\xampp\htdocs\oauth\src\My\ApiBundle\Entity\AccessToken.php on line 12

What am I doing wrong? I am following the docs from here.

Was it helpful?

Solution

You can't break setUser method definition (defined in interface). It must be exactly the same as in TokenInterface.

Change:

public function setUser(\My\ApiBundle\Entity\User $user = null)

To:

public function setUser(\Symfony\Component\Security\Core\User\UserInterface $user = null)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top