简体   繁体   English

Doctrine / Symfony 一对多关系中 getSomethings 的返回值是什么?

[英]What is the return value of getSomethings in a Doctrine / Symfony One-To-Many Relationship?

I like to either type-hint or starting in PHP7 actually show the return value of a getter function.我喜欢使用类型提示或在 PHP7 中开始实际显示 getter 函数的返回值。 But with One-To-Many relationships in Doctrine / Symfony, I'm still stuck and am not sure what to add to the @var tag.但是在 Doctrine / Symfony 中的一对多关系中,我仍然被困住并且不确定要添加到@var标签中的内容。

[...]

/**
 * @var string
 * @ORM\Column(name="name", type="string")
 */
private $features;


/**
 * What goes into var here?
 *
 * One Product has Many Features.
 * @ORM\OneToMany(targetEntity="Feature", mappedBy="product")
 */
private $features;

public function __construct()
{
    $this->features = new ArrayCollection();
    $this->name = 'New Product Name';
}

/**
 * @return Collection
 */
public function getFeatures(): Collection
{
    return $this->features;
}


[...]

Currently I'm using @var Collection and can then use the Collection functions.目前我正在使用@var Collection ,然后可以使用Collection 函数。 But what would be the »proper« thing to return?但是返回什么是“正确”的东西? Is it indeed Collection ?真的是Collection吗? Or is it ArrayCollection ?或者它是ArrayCollection I'm tempted to use Features[] in order to use the functions of Feature, if I need to (instead of typehinting), but it doesn't feel right.如果需要(而不是打字提示),我很想使用Features[]来使用 Feature 的功能,但感觉不对。

What would be the »cleanest« / stable way to do this?什么是“最干净”/稳定的方法来做到这一点?

If you want to keep the docblock I would use the union type |如果你想保留文档块,我会使用联合类型| to both specify the Collection and the list of values it contains like:指定集合及其包含的值列表,例如:

/**
 * @var Collection|Feature[]
 */

With this your IDE should both find the methods from Collection as well as the Feature-type hints when you get a single object from the collection, eg in a foreach.有了这个,当您从集合中获取单个对象(例如在 foreach 中)时,您的 IDE 应该同时从集合中找到方法以及功能类型提示。

As to the question of ArrayCollection vs. Collection, it is usually recommended to type hint for the interface (Collection in this case).关于ArrayCollection vs.Collection的问题,通常建议接口(这里是Collection)输入hint。 ArrayCollection offers a few more methods, but unless you really need them I would not bother with the type hint just to get them. ArrayCollection 提供了更多的方法,但除非你真的需要它们,否则我不会为了获得它们而费心使用类型提示。

What I tend to do in projects is keep the Collection inside the entity and only pass out an array in the getter like this:我在项目中倾向于做的是将 Collection 保留在实体中,并且只像这样在 getter 中传递一个数组:

public function getFeatures(): array
{
    return $this->features->toArray();
}

public function setFeatures(array $features): void
{
    $this->features = new ArrayCollection($features);
}

Be careful, the void return type is not supported in PHP 7.0 yet.请注意,PHP 7.0 尚不支持void返回类型。 The benefit of returning an array is that in your code you don't have to worry about what kind of Collection Doctrine uses.返回数组的好处是在您的代码中您不必担心使用哪种 Collection Doctrine。 That class is mainly used to maintain reference between objects inside Doctrine's Unit Of Work, so it should not really be part of your concern.该类主要用于维护 Doctrine 的工作单元内的对象之间的引用,因此它不应该真正成为您关注的一部分。

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

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