php 如何从链接实体中获取第一个实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11751383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:59:47  来源:igfitidea点击:

How to get the first entity from linked entity

phpsymfonydoctrine-orm

提问by Mirage

I have the

我有

Class User  {

@OneToMany
    private $profiles
}

Now I have the $user entity in my controller but I also want the first or last profile based on date. How can I achieve this?

现在我的控制器中有 $user 实体,但我还想要基于日期的第一个或最后一个配置文件。我怎样才能做到这一点?

I have the method getProfiles()but I think that will return the array collection.

我有方法,getProfiles()但我认为这将返回数组集合。

回答by Lusitanian

$user->getProfiles()->first()

will do it, as long as you are declaring the $profilesproperty as a

会这样做,只要您将$profiles财产声明为

\Doctrine\Common\Collections\ArrayCollection

in the constructor of your class.

在你的类的构造函数中。

回答by user1097795

You can easily get the first one like that :

您可以轻松获得第一个这样的:

$first = $this->get('doctrine.manager')->getRepository(MyEntity::class)->findOneBy([]);