Adnan
Adnan

Reputation: 2996

how to auto populate dropdown without having 'id' field in cakePHP?

I have two tables:

  1. countries with fields [objid(PK), name]
  2. cities with fields [objid(PK), name, country_id(FK)]

Using cakePHP, what would be the correct syntax of using $belongsTo in city model to auto populate the countries drop down?

Upvotes: 0

Views: 726

Answers (1)

RSK
RSK

Reputation: 17516

City Model

  class City extends AppModel{
       var name = "City";
        var $primaryKey = "objid";
       var $belongsTo = array('Country' =>  array('className' => 'Country','foreignKey' =>'country_id'));
    }

County Model

class Country extends AppModel{
    var name = "Country";
    var $primaryKey = "objid";
    var $hasMany = array('City');
}

Upvotes: 1

Related Questions