Symfony 2 - 扩展生成的Entity类

时间:2015-01-08 16:17:04

标签: php symfony doctrine-orm

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

TABLE: Timeslot
    id integer primary key
    start integer
    end integer

我希望扩展 Timeslot 实体类,方法是使用一些辅助方法,例如

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...

我不想修改doctrine自动生成的类文件,我想用它来像:

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

通过仅扩展Timeslot类,我有这个错误(因为实体未正确映射):

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

任何提示?

TimeslotHelper超类

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

Timeslot子类

/**
 * 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;
    }

}

1 个答案:

答案 0 :(得分:2)

你应该做相反的事情

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

你的助手课程:

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()