Custom Functions in Doctrine2 Automatically Created Classes

Is there a way to extend classes automatically created from the Doctrine2 database?

Example: I have this custom class created 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 to this class?

I tried to create another class (Test) in the libraries and extended it, and then add it to autoload (which works), but I get an error when I try to save the object:

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

Message: The mapping file was not found with the name 'Test.dcm.yml' for the class 'Test'.

I am using Doctrine2 in CodeIgniter3.

Thank.

+4
source share
4

Doctrine 2:

EntityGenerator , . [...] EntityGenerator , 100%.

, Doctrine Entity . , ( ).

Entity , , , , Doctrine .

. , Doctrine, - , , .

, Test : , EntityManager , .

. User Mapped Superclass ( , ), , User (, , ).

, Test , , Doctrine, User drop class Test.

+6
+1

:

<?php

namespace Entities;


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

<?php

namespace Entities;

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

Symfony 2 - Entity

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

+1

Annotations let you specify a repository class to add additional methods to an 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 the link [ http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html] 7.8.8. Custom repositories

-1
source

All Articles