Noodles
Noodles

Reputation: 928

Using/extending Doctrine Entities

I'm new to doctrine ORM and I'm trying to understand where to add code etc.

Say I have a User entity:

src/Entity/User.php

class User
{
    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="integer", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $userId;

    /**
     * @var int
     *
     * @ORM\Column(name="group_id", type="smallint", nullable=false, options={"unsigned"=true})
     */
    private $groupId = '0';

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=20, nullable=false)
     */
    private $username;
    ...

This is fine if I want to get a user based on id etc. I can use the entity manager to do $em->find('User', 1);

I want to add some logic to check whether the user is an admin user (e.g. $user->isAdmin()). Where would I add this? To a repository, a proxy or just by extending the User entity class (e.g. make the entity class UserBase and have User extend that)?

Upvotes: 0

Views: 44

Answers (1)

Magnesium
Magnesium

Reputation: 617

Your user entity should implement Symfony's UserInterface and already be able to do things such as check roles, password etc.

For other informations: - Use the entity to do some operations on the datas that is already loaded by the entity. For example you could add a "getFullName()" method - Use the repository for database operations, for example if you want to retrieve a list of user with some sorting rules, conditions etc.

So to answer your question, a method such as "isAdmin()" would be in the entity.

Upvotes: 1

Related Questions