简体   繁体   English

如何从CakePHP 3结果对象中获取第一项

[英]How do I get first item from CakePHP 3 results object

TLDR: How do I use the first item in a results object? TLDR:如何使用结果对象中的第一项?


After getting data from the database, I want to pull the first one out and use it separately before repeating through the remaining videos. 从数据库中获取数据后,我想先拉出第一个并单独使用它,然后重复其余的视频。 (All in my template/view file). (全部在我的模板/视图文件中)。 If it were an array, I could use something like array_shift , but so far with the object, I'm coming up empty. 如果它是一个数组,我可以使用像array_shift这样的东西,但到目前为止,这个对象,我是空的。

First, I get the data in my Controller, and pass it to the View: 首先,我在Controller中获取数据,并将其传递给View:

$videos = $this->Videos->find();
$this->set(compact('videos'));

Then, in the View (template file), I've tried this: 然后,在视图(模板文件)中, 我试过这个:

$featuredVideo = $videos{0};

Error Cannot use object of type Cake\\ORM\\Query as array 错误无法使用Cake \\ ORM \\ Query类型的对象作为数组

And this: 和这个:

$featuredVideo = $videos->0;

Error: syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' 错误:语法错误,意外的'0'(T_LNUMBER),期待标识符(T_STRING)或变量(T_VARIABLE)或'{'或'$'

And this: 和这个:

$featuredVideo = $videos->toArray()[0];

This works (kind of), but then I have to use $featuredVideo as an Array, which is different to everywhere else, and seems non-elegant. 这种工作(有点),但后来我必须使用$featuredVideo作为一个数组,这与其他地方不同,并且看起来不优雅。

And this: 和这个:

$featuredVideo = $videos->first();

This is awesome, but it limits the entire object to only the first, so I no longer have the rest of the videos. 这很棒,但是它将整个对象限制为只有第一个,所以我不再拥有其余的视频。

And this: 和这个:

$allVideos = $videos;
$featuredVideo = $videos->first();

But, apparently since it's an object, the $allVideos is just a reference to the $videos , because they both get reduced to the first video. 但是,显然因为它是一个对象, $allVideos只是对$videos的引用,因为它们都被缩减为第一个视频。

I realize this seems like a simple question, and it's likely a simple answer, but I have looked around, and tried the suggestions on how to get the first item from an object with the above results. 我意识到这似乎是一个简单的问题,它可能是一个简单的答案,但我已经环顾四周,并尝试了如何从具有上述结果的对象中获取第一个项目的建议。

What I failed to understand was that ->find() just returns the query object until you tell it to execute via an ->all() or a ->first() or by running it through a foreach() loop. 我无法理解的是->find()只返回查询对象,直到你通过->all()->first()或通过foreach()循环运行它来告诉它。

So, I couldn't just access the first child object, since the query hadn't yet executed to show the results. 所以,我不能只访问第一个子对象,因为查询尚未执行以显示结果。

And when I was trying to do ->first() after setting the variable above it, it was actually executing the query on the query object after setting limit to 1, hence making both only have 1. 当我在设置上面的变量之后尝试做->first()时,它实际上是在将limit设置为1后对查询对象执行查询,因此两者都只有1。

More details here . 更多细节在这里

The answer (in code): 答案(代码中):

// Controller
$videos = $this->Videos->find()->all();
$this->set(compact('videos'));

// View
$featuredVideo = $videos->first();

More details here about when query objects are lazily evaluated . 有关何时延迟评估查询对象的详细信息。


Thanks jose_zap and marc_ for helping clarify this one for me. 感谢jose_zap和marc_帮助我澄清这个。

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

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