1

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Jelle Ferwerda
  • 1,254
  • 1
  • 7
  • 13
  • Thank you Tere~sko. Would you recommend a different framework instead? – Jelle Ferwerda Oct 13 '13 at 18:06
  • 1
    I would recommend to avoid messing with frameworks until a solid grasp on OOP. [This](http://stackoverflow.com/a/16356866/727208) list might be to some help, but if, as you imply, you have just now moved away from PHP 4, then that will need to learn a lot more than what's in that list. – tereško Oct 13 '13 at 18:50
  • Thx Teresko. I have decided to back away, and am now building my own classes instead. On the long-run that will get me better understanding. Thx for the advice. – Jelle Ferwerda Dec 02 '13 at 11:56

1 Answers1

0

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'));
joshua.paling
  • 13,762
  • 4
  • 45
  • 60
  • THx Joshua, that helps. Especially the added info on using a $displayField parameter. Will read up and see what that does. – Jelle Ferwerda Oct 14 '13 at 07:38
  • @JelleFerwerda, the display field basically just sets the default field to be used when displaying records from that table. In a 'person' table, your display field might be 'first_name'. Read up on it here: http://book.cakephp.org/2.0/en/models/model-attributes.html#displayfield – joshua.paling Oct 14 '13 at 08:19