cakePHP:具有国家/地区下拉列表的用户关联

时间:2011-12-15 06:24:18

标签: cakephp

扩展我的其他问题here

我在执行ajax调用以填充状态下拉列表时看到以下错误 -

2011-12-15 01:15:51调试:填充国家/地区ID#99的状态 2011-12-15 01:15:51调试:注意事项(8):未定义属性:User :: $ State in [C:\ xampp \ htdocs \ dearmemoir \ app \ controllers \ users_controller.php,第236行]

这是我的控制器代码 -

function get_states($country_id = null){
        CakeLog::write('debug', 'Populating state for country id #' . $country_id ) ;
        $this->layout = 'ajax';
        $this->set('states',$this->User->State->find('list',array('conditions'=>array('State.country_id' => $country_id))));
}

我看到正在进行ajax调用,并且我能够在控制器中显示所选的值。

这是模特 -

state.php

<?php

class State extends AppModel {

    var $displayField = 'state_name';

    var $name = 'State';

    var $hasMany = array('User');

    var $hasOne = array ('Country');

}?>

country.php

<?php
class Country extends AppModel {

    var $displayField = 'country_name';

    var $name = 'Country';

    var $hasMany = array('User','State');

}?>

user.php的

<?php
class User extends AppModel {

    var $name = 'User';

    var  $belongsTo  = array('Country');

------

}

我无法弄清楚可能出现的问题!

1 个答案:

答案 0 :(得分:0)

urg ...我想通了 -

我的用户模型缺少与State的关联

var  $belongsTo  = array('Country');

应该是

var  $belongsTo  = array('Country','State');