Question

I am new to MVC proramming (And even object based). So coming from PhP 4.* I moved into OOP, MVC and Cake..

I am building a site which Insitutes from difference COUNTRIES can use to store their data (And more). I am now building the basic per-institute registration, and would like to include a drop-down of countries.

I see two ways to approach this; Either retreive the country table information for a dropdown using the Country model: $this->set('countries', ClassRegistry::init('Country')->getAllCountries()); (Followed by a function in \Model\Country.php)

or use the InstitutesController:
$this->set('countries', $this->Institute->Country->find('list', $params = array('fields' => array('id', 'country'))));

Which is the recommended route to take, as both seem to work?

Was it helpful?

Solution

Use the second one:

$this->set('countries', $this->Institute->Country->find('list', $params = array('fields' => array('id', 'country'))));

Both work, but, you've already got an instance of your country model (accessible via $this->Institute->Country). So, why create another instance of it? There's just no need.

There shouldn't actually be a need to specify the fields for your call to the find method. 'id' will be automatically selected as the first field, and if you set the display field of your Country model to 'country', then that will be the default uses in find('list') calls. Do it like this:

// just after class Country extends AppModel {
public $displayField = 'country';

Then, you'll just need to use this code:

$this->set('countries', $this->Institute->Country->find('list'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top