如何附加到CakePHP3 ResultSet? [英] How to append to a CakePHP3 ResultSet?

查看:189
本文介绍了如何附加到CakePHP3 ResultSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询(一个查询对象):

I have a query (a Query object):

$joblineitems = $this->JobLineItems->find('all');

它有结果(一个ResultSet对象):

It has results (a ResultSet object):

$joblineitemsresults = $joblineitems->all();

ResultSet对象是一个Iterable,foreach()循环将返回每个JobLineItem对象。

The ResultSet object is an Iterable such that a foreach() loop will return each of the JobLineItem objects.

我只是希望的附加结果添加到$ joblineitemsresults - 具体欲一个$这个 - > JobLineItems-> newEntity()追加到的结果,从而结束时当我随后对$ joblineitemsresults进行分页并在视图中循环它时,最后一页循环中的最后一个对象将是一个空白对象,其属性为new = true。

I simply want to add an additional result to $joblineitemsresults - specifically I want to append a $this->JobLineItems->newEntity() to the end of the results so that when I subsequently paginate $joblineitemsresults and loop through it in the view, the last object in the loop on the last page will be a blank object with the property new=true.

如何手动将额外结果附加到ResultSet对象?

How can I manually append an extra result to a ResultSet object?

推荐答案

试试这个

$joblineitems = $this->JobLineItems->find('all');
$joblineitemsresults = $joblineitems->all();

$joblineitemsresults = $joblineitemsresults ->append([$this->JobLineItems->newEntity()]);
debug($joblineitemsresults ->toList());

你可以看到手册来自参考

特别注意最后一段


当从不同的来源追加时,你可以期望两个集合中
的一些键是相同的。例如,当附加两个
简单数组时。使用toArray()将集合
转换为数组时,这可能会出现问题。如果你不希望一个
集合中的值根据它们的键覆盖前一个中的其他值,
确保调用toList()以便删除键并且
保留所有值。

When appending from different sources, you can expect some keys from both collections to be the same. For example, when appending two simple arrays. This can present a problem when converting a collection to an array using toArray(). If you do not want values from one collection to override others in the previous one based on their key, make sure that you call toList() in order to drop the keys and preserve all values.

这就是为什么我用 toList()而不是指定者()。如果您使用 toArray(),您将看到新实体覆盖第一个。

That's why I used toList() instead of toArray(). If you use toArray() you'll see the new entity override the first one.

当然,您可以还变换您的记录阵列中的,然后将新实体添加到阵列

Of course you can also transform your recordset in an array and then append the new entity to the array

$joblineitemsresults = $joblineitems->toArray();
joblineitemsresults[] = $this->JobLineItems->newEntity();

这篇关于如何附加到CakePHP3 ResultSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆