简体   繁体   English

Symfony2和Doctrine - ManyToOne

[英]Symfony2 and Doctrine - ManyToOne

I am trying to understand Symfony2, but there is something that is not making sense to me. 我想了解Symfony2,但有些东西对我来说没有意义。 I reversed engineered an existing database to produce my entities, so maybe thats the problem. 我对现有数据库进行了反向工程以生产我的实体,所以也许就是问题所在。

I have a table called availability_alert, nothing special, a few fields including an id (which is the primary key). 我有一个名为availability_alert的表,没什么特别的,一些字段包括一个id(这是主键)。 This table has no link to anything else. 该表没有其他链接。

I then have a second table called booking_class, once again nothing special, but it does have the field $availabilityAlert which links to the availability_alerts tables id. 然后我有一个名为booking_class的第二个表,再一点没什么特别的,但它确实有$ availabilityAlert字段链接到availability_alerts表id。

In essence, an Availability Alert can have one or many Booking Class. 从本质上讲,可用性警报可以有一个或多个预订类。

Now in my booking class entity, I have the link 现在在我的预订类实体中,我有链接

/**
 * @var \AlertBundle\Entity\AvailabilityAlert
 *
 * @ORM\ManyToOne(targetEntity="AlertBundle\Entity\AvailabilityAlert")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="availability_alert_id", referencedColumnName="id")
 * })
 */
private $availabilityAlert;

So this all looks ok. 所以这一切看起来都不错。 The setter for it though came out like so 它的制定者虽然这样出来了

public function setAvailabilityAlert(\AlertBundle\Entity\AvailabilityAlert $availabilityAlert = null)
{
    $this->availabilityAlert = $availabilityAlert;

    return $this;
}

So that appears to take an AvailabilityAlert Object as a parameter, not the AvailabilityAlert id? 那么似乎将AvailabilityAlert对象作为参数,而不是AvailabilityAlert id?

So with the above, I am presuming doing something like this in my controller will not work? 所以有了上述内容,我假设在我的控制器中做这样的事情是行不通的?

$alert = new AvailabilityAlert();

$bookingClass = new BookingClass();
$bookingClass->setAvailabilityAlert($alert->getId());

Could someone give me some advice on whether things are correct here, or if I should be doing something else? 有人可以给我一些关于这里是否正确的建议,或者我是否应该做其他事情? Essentially AvailabilityAlert should be a static table, other tables link to this. 本质上,AvailabilityAlert应该是静态表,其他表链接到此表。

Any advice appreciated. 任何建议表示赞赏

Thanks 谢谢

Yes this is correct. 是的,这是正确的。

In the Doctrine world, you are working with objects, not with integers or strings, when it comes to relationships between Entities. 在Doctrine世界中,当涉及实体之间的关系时,您正在处理对象,而不是整数或字符串。

You can read more about Doctrine 2 relationships here: http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations 您可以在此处阅读有关Doctrine 2关系的更多信息: http//symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

That's correct. 那是对的。 You don't use integers, strings. 您不使用整数,字符串。 It's because the relationships are given in entity annotations and Doctrine uses them to figure out what is used exactly to reference the one object from the other. 这是因为关系是在实体注释中给出的,而Doctrine使用它们来确定用于引用另一个对象的确切用途。 This even let you change how objects reference themselves - for example you change the id to a compound primary key in AvailabilityAlert class and your code wouldn't change much except for the annotations. 这甚至可以让您更改对象引用自己的方式-例如,将ID更改为AvailabilityAlert类中的复合主键,除了注释之外,您的代码不会有太大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM