Friday, 15 March 2013

php - Combine Two Different Models in Array for Sorting, Referenced Models Lost -


I have two models, news and projects. I is a reference model of news, section. A referenced model of the project is a sub group, both the news and the project has a "created" field. I want to display the last 5 news or project items on my home page.

So I found the post, and what he did, that is:

  $ newsAndProjects = array (); $ News = $ this- & gt; News-> Search (...); Forex Currency ($ news as $ = k = $> $ v) {$ newsAndProjects [] = $ v ['news']; } $ Projects = $ this- & gt; Project-> Search (...); Foreign Currency ($ as $ $ projects => $ v) {$ newsAndProjects [] = $ v ['project']; } If (size ($ newsAndProjects)> 1) {$ newsAndProjects = Set :: sort ($ News and Project, '/ created', 'DESC'); }  

And I thought it worked fine. But when I try to use the referenced model of the news or project, I get an error

$ news ['news'] ['title'] $ news ['news' ] ['Section']

or

  $ projects ['project'] ['title'] $ projects ['project'] ['sub -Group ']  

... Once in the $ news and project array, I must use the model directly, that is:

  $ newsAndProjects ['Title']  

And I now have a 'section' or 'sub group' The file is not, but:

  $ newsAndProjects ['section_id'] $ newsAndProjects ['Sub_group_id']  

... which means that I now Can not live, for example, use section or sub group name, id, etc.

How can I get this information back?

What a dummy I understood this Generally speaking, there was my unfamiliar problem with Cake and Array in PHP.

My referenced models were not appearing because I was doing this

  $ newsAndProjects [] = $ v ['news']; $ News and project [] = $ v ['project'];  

Instead

  $ news and projects [] = $ v; $ News and projects [] = $ v; I.e. only $ v,  whole  with the associated referenced models, instead of the model object, put the 'root' model (news or project) in the $ newsAndProjects array Was there. 

Hope this post will save some time somebody someday.


No comments:

Post a Comment