fdehanne
fdehanne

Reputation: 1718

Custom functions in Doctrine2 auto generated classes

Is there a way to extend classes auto-generated from database by Doctrine2 ?

Example: I have this User class generated by Doctrine.

<?php

namespace Entities;

/**
 * User
 */
class User
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $firstName;

    /**
     * @var string
     */
    private $lastName;


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

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

        return $this;
    }

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

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

        return $this;
    }

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

I would like to add this function :

public function getFullName()
{
    return $this->getFirstName().' '.$this->getLastname();
}

Is there a cleaner way than adding it directly into this class?

I tried to create another class (Test) in libraries and extends it, then add it in autoload (which is working), but i get an error when I try to save object :

class Test extends Entities\User {
    public function getFullName() {
        return $this->getFirstName().' '.$this->getLastname();
    }
}

Message: No mapping file found named 'Test.dcm.yml' for class 'Test'.

I'm using Doctrine2 in CodeIgniter3.

Thanks.

Upvotes: 4

Views: 360

Answers (4)

Raghu
Raghu

Reputation: 1

Annotation allows you to specify repository class to add more methods to entity class.

/**
 * @ORM\Entity(repositoryClass="App\Entity\UserRepository")
 */

class User
{

}

class UserRepository extends EntityRepository
{
    public function getFullName() {
        return $this->getFirstName().' '.$this->getLastname();
    }
}

// calling repository method
$entityManager->getRepository('User')->getFullName();

Here's a link [http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html] 7.8.8. Custom Repositories

Upvotes: -1

websky
websky

Reputation: 3162

you can do this:

<?php

namespace Entities;


/**
 * User
 */
class User extends Test
{
//... and extends Test
}

or

<?php

namespace Entities;

/**
 * User
 */
class User
{
    //...
    public function getFullName() {
        return $this->getFirstName().' '.$this->getLastname();
    }
}

view more

Symfony 2 - Extending generated Entity class

http://www.theodo.fr/blog/2013/11/dynamic-mapping-in-doctrine-and-symfony-how-to-extend-entities/

http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html

Upvotes: 1

Imran
Imran

Reputation: 4750

Seems you are having trouble while accessing the user entity class. You mentioned that test is a library class. Why not try to access the User entity class from a controller. If can do this then may be something is wrong with the configuration of test file. Besides, you need to map you doctrine entity class properly. You can have a look here to learn about doctrine mapping using yml: http://doctrine-orm.readthedocs.org/en/latest/reference/yaml-mapping.html

Upvotes: 1

axiac
axiac

Reputation: 72177

As explained in the Doctrine 2 FAQ:

The EntityGenerator is not a full fledged code-generator that solves all tasks. [...] The EntityGenerator is supposed to kick-start you, but not towards 100%.

In plain English this means you ask Doctrine to generate the Entity files only once. After that, you are on your own and do whatever changes you like (or it needs) to them.

Because an Entity is not just a container for some properties but it's where the entire action happens, this is how the flow should happen, Doctrine cannot write more code for you.

The only way to add functionality to the stub Entities generated by Doctrine is to complete the generated classes by writing the code that implements the functionality of each Entity according to its role in your Domain Model.

Regarding the other issue, on the Test class, the error message is self-explanatory: any class passed to the EntityManager for handling needs to be mapped.

Take a look at the help page about Inheritance Mapping. You can either map class User as a Mapped Superclass (it acts like a template for the derived classes and its instances are not persisted in the database) or you can use Single Table Inheritance to store the instances of all classes derived from User in a single table (useful when they have the same properties but different behaviour).

Or, in case you created class Test just because you were afraid to modify the code generated by Doctrine, put the behaviour you need in class User and drop class Test.

Upvotes: 6

Related Questions