Symfony 2 - 扩展生成的实体类 [英] Symfony 2 - Extending generated Entity class
问题描述
TABLE:Timeslot
id整数主键
开始整数
结束整数
我想要通过使用一些附件方法
扩展实时类
通过使用
$ timeslots = $ this-> getDoctrine() - 的所有实例 > getRepository('AppBundle:Timeslot') - > find ...
但我不想修改自动生成的类文件,我想使用它:
$ timeslots = $ this-> getDoctrine() - > getRepository('AppBundle:TimeslotHelper') - > find ...
只有扩展Timeslot类,我有这个错误(因为实体没有正确映射):
找不到名为/home/otacon/PhpstormProjects/Web/src/AppBundle/Resources/config/doctrine/TimeslotHelper.orm.xml的映射文件,用于AppBundle\Entity\TimeslotHelper类。
任何提示?
解决方案< h1>
TimeslotHelper超类
抽象类TimeslotHelper {
抽象保护函数getStart();
抽象保护函数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子类
/ **
*时间段
*
* @ ORM\Entity
* /
类Timeslot扩展TimeslotHelper
{
/ **
* @var整数
*
* @ ORM\Column(name =start,type =integer ,nullable = true)
* /
private $ start;
/ **
* @var整数
*
* @ ORM\Column(name =end,type =integer,nullable = true )
* /
private $ end;
/ **
* @var整数
*
* @ ORM\Column(name =id,type =integer)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;
/ **
*设置起始
*
* @param integer $ start
* @return Timeslot
* /
public function setStart($ start)
{
$ this-> start = $ start;
return $ this;
}
/ **
*获取开始
*
* @return integer
* /
public function getStart )
{
return $ this-> start;
}
/ **
*设置结束
*
* @param integer $ end
* @return Timeslot
* /
public function setEnd($ end)
{
$ this-> end = $ end;
return $ this;
}
/ **
*获取结束
*
* @return integer
* /
public function getEnd )
{
return $ this-> end;
}
/ **
*获取id
*
* @return integer
* /
public function getId )
{
return $ this-> id;
}
}
你应该做相反的
public class Timeslot extends TimeslotHelper {
// the doctrine auto-generated 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()
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;
}
}
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()
这篇关于Symfony 2 - 扩展生成的实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!