Cakephp:带有Country / State dropdowm的用户模型 [英] Cakephp: User Model with Country / State dropdowm

查看:68
本文介绍了Cakephp:带有Country / State dropdowm的用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Auth组件进行用户登录和注册。我要添加国家和州下拉列表到我的用户模型。有人可以帮助我在模型文件中所需的关联吗?

I am using Auth component for user login and registration. I want to add Country and State dropdowns to my User Model. Can someone help me with the required association in the Model files?

class User extends AppModel {

    var $name = 'User';

    var  $belongsTo  ="Country" ;
}


<?php
class Country extends AppModel {

    var $name = 'Country';

    var $hasMany = array('User');

}?>

我的观点:

echo $form->input('country_id');

但我得到一个空dropdowm!

But I am getting an empty dropdowm!

推荐答案

您的 belongsTo 定义不正确。 belongsTo属性必须是一个数组:

Your belongsTo definition is incorrect. The belongsTo attribute must be an array:

public $belongsTo = array('Country');

然后在用户控制器中, 国家::

Then in your Users controller, ensure you're getting the list of countries:

$countries = $this->User->Country->find('list');
$this->set(compact('countries'));

您的视图将如下所示:

<?php echo $this->Form->create('User');?>
    <fieldset>
      <legend><?php echo __('Edit User'); ?></legend>
      <?php
        echo $this->Form->input('id');
        // more fields
        echo $this->Form->input('country_id');
      ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit'));?>

这篇关于Cakephp:带有Country / State dropdowm的用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆