Symfony 2 - 扩展生成的实体 class

Symfony 2 - Extending generated Entity class

我有一些由 Doctrine 生成的实体。其中之一是 Timeslot,这里是 table 定义

TABLE: Timeslot
    id integer primary key
    start integer
    end integer

我想扩展 Timeslot 实体class 通过使用像

这样的辅助方法
public class TimeslotHelper extends Timeslot
{
    public function getStartDay(){
        return $this->$start_time / (24*60);
    }

    public function getStartHour(){
        return $this->$end_time % (24*60);
    }

    public function getStartMinutes(){
        return $this->$start_time % 60;
    }
    ...
}

我正在使用

获取Timeslot的所有实例
$timeslots = $this->getDoctrine()->getRepository('AppBundle:Timeslot')->find...

但是 我不想修改学说自动生成的 class 文件,我想像这样使用它:

$timeslots = $this->getDoctrine()->getRepository('AppBundle:TimeslotHelper')->find...

通过仅扩展时间段 class 我遇到了这个错误(因为实体没有正确映射):

No mapping file found named '/home/otacon/PhpstormProjects/Web/src/AppBundle/Resources/config/doctrine/TimeslotHelper.orm.xml' for class 'AppBundle\Entity\TimeslotHelper'.

有什么提示吗?

解决方案

超级TimeslotHelperclass

abstract class TimeslotHelper {

    abstract protected function getStart();
    abstract protected function getEnd();

    public function getStartDay(){
        return floor($this->getStart() / (24*60));
    }

    public function getEndDay(){
        return floor($this->getEnd() / (24*60));
    }

    public function getStartHour(){
        return floor(($this->getStart() % (24*60)) / 60);
    }

    public function getEndHour(){
        return floor(($this->getEnd() % (24*60)) / 60);
    }

    public function getStartMinute(){
        return $this->getStart() % 60;
    }

    public function getEndMinute(){
        return $this->getEnd() % 60;
    }
}

时隙子class

/**
 * Timeslot
 *
 * @ORM\Entity
 */
class Timeslot extends TimeslotHelper
{
    /**
     * @var integer
     *
     * @ORM\Column(name="start", type="integer", nullable=true)
     */
    private $start;

    /**
     * @var integer
     *
     * @ORM\Column(name="end", type="integer", nullable=true)
     */
    private $end;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * Set start
     *
     * @param integer $start
     * @return Timeslot
     */
    public function setStart($start)
    {
        $this->start = $start;

        return $this;
    }

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

    /**
     * Set end
     *
     * @param integer $end
     * @return Timeslot
     */
    public function setEnd($end)
    {
        $this->end = $end;

        return $this;
    }

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

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

}

你应该反其道而行之

public class Timeslot extends TimeslotHelper {
//the doctrine auto-generated class file 
}

你的帮手class:

public class TimeslotHelper 
{
    public function getStartDay(){
        return $this->$start_time / (24*60);
    }

    public function getStartHour(){
        return $this->$end_time % (24*60);
    }

    public function getStartMinutes(){
        return $this->$start_time % 60;
    }
    ...
}

您的实体

$timeslots = $this->getDoctrine()->getRepository('AppBundle:Timeslot')->find...

$timeslots-> getStartDay()