Symfony 2 - Extended Entity Class

I have some entities created by Doctrine. One of them is Timeslot, here is a table definition

TABLE: Timeslot
    id integer primary key
    start integer
    end integer

I want to extend the Timeslot entity class with some helper methods such as

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

I get all instances of Timeslot using

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

But I do not want to modify the autonomous-generated doctrine file , and I would like to use it as:

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

Only extending the Timeslot class, I have this error (because the object is not displayed correctly):

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

Any clues?

DECISION

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

Temporary subclass

/**
 * 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
source share
1 answer

you have to do the opposite

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

All Articles