Doctrine2自动生成的类中的自定义函数

时间:2015-04-02 10:38:59

标签: php codeigniter doctrine-orm

有没有办法通过Doctrine2扩展从数据库自动生成的类?

示例:我有这个由Doctrine生成的User类。

<?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;
    }

我想添加此功能:

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

是否有比将其直接添加到此课程更简洁的方式?

我尝试在库中创建另一个类(Test)并对其进行扩展,然后将其添加到autoload(这是有效的),但是当我尝试保存对象时出现错误:

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

消息:找不到名为&#39; Test.dcm.yml&#39;的映射文件。课程&#39;测试&#39;。

我在CodeIgniter3中使用Doctrine2。

感谢。

4 个答案:

答案 0 :(得分:6)

正如Doctrine 2 FAQ

中所述
  

EntityGenerator不是一个完整的代码生成器,可以解决所有任务。 [...] EntityGenerator应该启动你,但不是100%。

简单来说,这意味着您要求Doctrine仅生成一次实体文件。在那之后,你可以独立完成自己喜欢(或需要)的任何改变。

因为实体不仅仅是某些属性的容器,而且它是整个操作发生的地方,这就是流程的发生方式,Doctrine无法为您编写更多代码。

向存根添加功能的唯一方法Doctrine生成的实体是通过根据域模型中的角色编写实现每个实体功能的代码来完成生成的类。

关于另一个问题,在Test类上,错误消息是不言自明的:传递给EntityManager进行处理的任何类都需要映射。

请查看有关Inheritance Mapping的帮助页面。您可以将类User映射为Mapped Superclass(它类似于派生类的模板,并且其实例不会保留在数据库中),或者您可以使用Single Table Inheritance来存储实例在单个表中从User派生的所有类的类(当它们具有相同的属性但行为不同时很有用)。

或者,如果您因为害怕修改Doctrine生成的代码而创建了类Test,请在类User中放置所需的行为并删除类Test。< / p>

答案 1 :(得分:1)

似乎您在访问用户实体类时遇到了问题。你提到test是一个库类。为什么不尝试从控制器访问User实体类。如果可以这样做,那么测试文件的配置可能会出错。此外,您需要正确地映射您的教义实体类。您可以在这里查看使用yml:http://doctrine-orm.readthedocs.org/en/latest/reference/yaml-mapping.html

了解学说映射

答案 2 :(得分: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 - 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

答案 3 :(得分:-1)

Annotation允许您指定存储库类以向实体类添加更多方法。

/**
 * @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();

这是一个链接[http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html] 7.8.8。自定义存储库

相关问题