Reputation: 487
I have two Models, News and Projects. News has a referenced Model, Section. Project has a referenced Model, SubGroup. Both News and Project have a "created" field. I'd like to display on my home page last 5 News or Project items.
So I found this post, and did what it said, namely:
$newsAndProjects = array();
$news = $this->News->find( ... );
foreach ($news as $k => $v) {
$newsAndProjects[] = $v['News'];
}
$projects = $this->Project->find( ... );
foreach ($projects as $k => $v) {
$newsAndProjects[] = $v['Project'];
}
if (sizeof($newsAndProjects) > 1) {
$newsAndProjects = Set::sort($newsAndProjects, '/created', 'DESC');
}
And I thought it worked fine. But when I try to access the referenced Models of News or Project, I get an error.
What I've noticed is, whereas I used to be able to do this:
$news['News']['title']
$news['News']['Section']
or
$projects['Project']['title']
$projects['Project']['SubGroup']
... once in the $newsAndProjects array, I have to access the Models directly, namely:
$newsAndProjects['title']
And I no longer even have 'Section' or 'SubGroup' Models, but rather:
$newsAndProjects['section_id']
$newsAndProjects['sub_group_id']
... which means I can no longer, for example, access the Section or SubGroup name, id, etc.
How can I get this info back?
Upvotes: 1
Views: 36
Reputation: 487
What a dummy. I figured it out. My unfamiliarity with Cake and arrays in php in general was the problem.
My referenced Models weren't showing up because I was doing this
$newsAndProjects[] = $v['News'];
$newsAndProjects[] = $v['Project'];
instead of this
$newsAndProjects[] = $v;
$newsAndProjects[] = $v;
ie. I was only putting the 'root' Model (News or Project) in the $newsAndProjects array, instead of $v, the entire Model object with its corresponding referenced Models.
Hopefully this post will save someone some time someday.
Upvotes: 1