1
votes

I have some entities generated by Doctrine. One of those is Timeslot, here is the table definition

TABLE: Timeslot
    id integer primary key
    start integer
    end integer

I want to extend the Timeslot entity class by using some accessory methods like

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'm getting all the instances of Timeslot by using

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

But I don't want to modify the doctrine auto-generated class file and i Would like to use it like:

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

By only extending the Timeslot class I have this error (because the entity is not correctly mapped):

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

Any hints?

SOLUTION

TimeslotHelper superclass

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 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
Why extend at all? Just use a standalone TimeslotHelper class and pass the Timeslot object as a method parameter.Jez
It's only a comfort because I need to use always TimeslotHelper so i want to make a call like: $this->getDoctrine()->getRepository('AppBundle:TimeslotHelper')->...Otacon
If you always want to use it then modify the generated class - that's what it's for.Jez
On the off-chance that you're only doing that calculation in order to display the values in your view layer, a Twig extension might be nicer - see symfony.com/doc/current/cookbook/templating/twig_extension.htmlJez
Just put it in the main entity, that's what it's there for.Jason Roman

1 Answers

2
votes

you should do the opposite

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

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

Your Entity

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

$timeslots-> getStartDay()