Friday, June 7, 2013

Models indirect association, how to retrieve data?

How's it going?

I have a problem with cakePHP, I'm new to it and struggling with it. Let me pour onto you guys my situation:

I'm developing a system for a University here in Brazil. In this sytem I've got the following models: - Country; - State; - City; - University; - Unit; - Class;

Ok. and the relations are: - State belongs to Country; - City belongs to State; - University belongs to Country (because, in my database, not every country has states registered to it and not every state has citys registered to it, so I could not demand from the user to register a University to a City); - Unit belongsTo university; - Class belongs to University (because not every University has Units registered to it);

Ok. I guess now I've set a good background for you guys to understand my situation. Now onto the question itself:

In the Views of my Model Class I wanna display the Country and (IF there are State City). And I may have the need to display such content in other Model views such as Unit and University.

But when I try to do that in my Class model I can only display the country_id. The foreign key in my university database table. And why is that, that is because my Class model belongs to University, so it's pretty easy to access my University's properties. However I do not wish to access the id of the Country, I want it's Name. And maybe in the future I might want other properties, who knows?

HOW DO I DO THAT? How do I access the properties of the Model Country when my Model Class has NO direct relation to it?

many thx hugs and kisses. Hope someone can help me with this one.

p.S: I've managed a kinda loser solution: In my Class Controller I've done the following (so I can access variables of the Models Country, State and City):

In the View function I've loaded the Models for State and City (the country Model I can access by the relation Class->University->Country);
Then I used a find method to find the respective country, state and city for the Class in question I wanna display in view.ctp. The code follows:

CODE
public function view($id = null) {
$this->Class->id = $id; $this->set('class', $this->Class->read());
$this->loadModel('State');
$this->loadModel('City');
$country = $this->Class->Universsity->Country->find('first', array('conditions' => array('Country.id' => $this->Class->data['University']['country_id']),));
$state = $this->State->find('first', array('conditions' => array('State.id' => $this->Class->data['University']['state_id']),));
$city = $this->City->find('first', array('conditions' => array('City.id' => $this->Class->data['University']['city_id']),));
$this->set('country',$country['Country']);$this->set('city',$city);
$this->set('state',$state);
}


And it kinda works...In my Wiew.ctp I get an array for Country data, and array for State data and an array for City data. And in the view I check to see if the state and city arrays are not length = 0 to display them and all...but I think there HAS to be a better way to do This.

CAN ANYONE HELP ME???

P.P.S:

The other question is...what about the Index.ctp? How will I do that if I do not know which Class I'm working with? Will I have to have logic and find methods in the View? Isin't that just messing up the code?

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: