I use a search plugin in my app that includes an option to export the search results to Excel. I’ve been asked to update the export with some values that come from a joined table, so I modified the contain statement to include DocRevisions
:
$query = $this->Docs->find('search', [
'search' => $this->request->getQueryParams()]
)
->contain(
['Categories',
'Projects',
'Discrepancies',
'Systems',
'Author',
'Initiator',
'DocRevisions' => [
'RevStatus',
'RevAuthor',
'EngApprov',
'ModifiedUser',
'AppTech',
],
]
)
->order('Docs.doc_no ASC');
// Call to exportSearch() at end of search() method
$this->exportSearch($query);
In my app, Docs
hasMany DocRevisions
. Some docs have none; others have more.
In my exportSearch
method, I have an $extract
variable set up like this. It works on the $query
array I passed it from search()
:
$extract = [
'doc_no',
'current_rev_no',
function (array $row) {
return $row['project']['location'];
},
function (array $row) {
return $row['discrepancy']['description'];
},
function (array $row) {
return $row['author']['full_name'];
},
function (array $row) {
return $row['doc_revisions']['rev_status']['description'];
},
];
My problem is with the last function, the one that tries to grab the description of the revision status. Every time I try to export it, I get Undefined index: rev_status
. Yet, when I debug, I can see the values I want:
debug($row['doc_revisions']);
'rev_status' => [
'id' => (int) 1,
'description' => 'Engineering Review',
'display_order' => (int) 1,
],
This is also true of the other contains I defined (modified_user, eng_approv, rev_author
). They’re all there when I debug $row
. But each one produces the same Undefined index
result when I try to export them. How can I get these values to appear in my export?