Otacon
Otacon

Reputation: 347

Symfony 2 - Extending generated Entity class

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

}

Upvotes: 1

Views: 496

Answers (1)

Abdallah Arffak
Abdallah Arffak

Reputation: 1193

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

Upvotes: 2

Related Questions