Magnanimity
Magnanimity

Reputation: 1313

New Symfony2 User Class gives Fatal Error

I am trying to move from manually specified users in security.yml to database users. I have read http://symfony.com/doc/current/book/security.html#loading-users-from-the-database and implemented their example, then expanded on it using a friend's user class. When I try to run php app/console doctrine:generate:entities I get the following error: Class MyFreelancer\PortalBundle\Entity\User contains 7 abstract methods and must therefor be declared abstract or implement the remaining methods and then a list of methods that goes off the page.

My code is as follows:

<?php
namespace MyFreelancer\PortalBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Ser;
use Gedmo\Mapping\Annotation as Gedmo;

use Symfony\Component\Security\Core\User\Role;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * User
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @Ser\ExclusionPolicy("all")
 */
class User implements UserInterface, \Serializable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Ser\Expose
     */
    private $id;

/**
     * @var string
     *
     * @ORM\Column(name="role_id", type="integer")
     * @Assert\Range(min=1)
     */
    private $roleId;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=100)
     * @Assert\Length(min=3, max=100)
     * @Ser\Expose()
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=2048)
     * @Assert\Length(min=8, max=2048)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     * @Assert\Length(min=3, max=100)
     * @Ser\Expose()
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="string", length=100)
     * @Assert\Length(min=3, max=100)
     * @Ser\Expose()
     */
    private $surname;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=200)
     * @Assert\Email(checkMX=true)
     * @Ser\Expose()
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="cell", type="string", length=20, nullable=true)
     * @Assert\Length(min=10, max=20)
     * @Ser\Expose()
     */
    private $cell;

}
?>

I have looked at Symfony2 Fatal error in Users Entity Provider, but I do not know what has to go into those methods if I have to implement them manually. Also, isn't doctine:generate:entities supposed to do that for you? If declaring the class as abstract is easier, how do I do that? What does that entail?

While we are on the topic, how do I save/retrieve the save user roles to the database, such as (from security.yml)

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

This is actually a second question and if it cannot be answered here then I will open a new topic. Thank you to everyone who has read this question.

Upvotes: 0

Views: 951

Answers (1)

Magnanimity
Magnanimity

Reputation: 1313

The cookbook article about the entity provider at http://symfony.com/doc/current/cookbook/security/entity_provider.html shows how to set this up correctly.

Upvotes: 1

Related Questions