[英]Symfony2 - Correct way to handle Objects
I have things working perfectly, but I am wondering if I should do it differently after reading this 我的工作正常,但是我想知道阅读本文后是否应该做其他的事情
Essentially, I have a Entity class called AvailabilityAlert. 本质上,我有一个称为AvailabilityAlert的实体类。 At the moment, this is pretty much a static Entity, the only fields it has in it relate to itself.
目前,这几乎是一个静态实体,它包含的唯一字段都与自身有关。
I then have a Entity class called FlightNumbers, and in it I have 然后,我有一个称为FlightNumbers的实体类,在其中有
/**
* @ORM\ManyToOne(targetEntity="Nick\AlertBundle\Entity\AvailabilityAlert", inversedBy="flightNumbers")
* @ORM\JoinColumn(name="availability_alert_id", referencedColumnName="id")
*/
private $availabilityAlert;
So $availabilityAlert is a link to the id field in the AvailabilityAlert Entity class. 因此$ availabilityAlert是指向AvailabilityAlert Entity类中id字段的链接。
So this all works great, an AvailabilityAlert has one or more FlightNumbers. 因此,这一切都很好,一个AvailabilityAlert具有一个或多个FlightNumber。 According to that article, I should show this in my AvailabilityAlert class.
根据那篇文章,我应该在我的AvailabilityAlert类中展示它。 So should this class have
所以这个班应该有
/**
* @ORM\OneToMany(targetEntity="Nick\AlertBundle\Entity\FlightNumbers", mappedBy="availabilityAlert")
*/
protected $flightNumbers;
And then a __construct which adds it to an ArrayCollection? 然后是将其添加到ArrayCollection的__construct吗? If so, what is the advantage of doing this?
如果是这样,这样做的好处是什么?
As I say, everything works perfectly the way I currently have it. 就像我说的,一切都按照我目前的方式完美运行。 However, I want to learn the correct way of doing things.
但是,我想学习正确的做事方法。
Adding an ArrayCollection on __construct allows you to use methods of Collection interface like add, remove, contains, ... 在__construct上添加ArrayCollection允许您使用Collection接口的方法,例如添加,删除,包含,...
This can come handy when using a newly created entity, eg 使用新创建的实体时,这可能会派上用场
(new FlightNumbers)->getAvailabilityAlert()->add(new AvailabilityAlert);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.