首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用cakephp 2.x中的可包含行为获取国家、城市、州的用户

如何使用cakephp 2.x中的可包含行为获取国家、城市、州的用户
EN

Stack Overflow用户
提问于 2015-04-30 07:33:54
回答 1查看 811关注 0票数 0

我有这样的模型:Country,State,City,User with relations:Country hasMany State,State hasMany City,City hasMany User,

State belongsTo Country,City belongsTo State,User belongsTo City,

现在我想把所有的用户,不仅是他们的城市,而且是国家和州。我该怎么做?

控制器用户/索引

代码语言:javascript
运行
复制
 public function index() {
$this->User->Behaviors->load('Containable');
        $this->paginate = array('contain'=>array('State'));
        debug($this->Paginator->paginate());exit;
        $this->set('users', $this->Paginator->paginate());
    }

输出

代码语言:javascript
运行
复制
    Warning (512): Model "User" is not associated with model "State" [CORE\Cake\Model\Behavior\ContainableBehavior.php, line 342]

\app\Controller\UsersController.php (line 27)

array(
    (int) 0 => array(
        'User' => array(
            'id' => '1',
            'name' => 'username',
            'city_id' => '1'
        )
    )
)

Model Country

代码语言:javascript
运行
复制
public $hasMany = array(
    'State' => array(
        'className' => 'State',
        'foreignKey' => 'country_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

Model State

代码语言:javascript
运行
复制
public $belongsTo = array(
        'Country' => array(
            'className' => 'Country',
            'foreignKey' => 'country_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public $hasMany = array(
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'state_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

模型City

代码语言:javascript
运行
复制
public $belongsTo = array(
    'State' => array(
        'className' => 'State',
        'foreignKey' => 'state_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);


public $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'city_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

Model 用户

代码语言:javascript
运行
复制
public $belongsTo = array(
    'City' => array(
        'className' => 'City',
        'foreignKey' => 'city_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
EN

Stack Overflow用户

回答已采纳

发布于 2015-05-01 06:38:23

我已经决定了。感谢cakephp社区。控制器用户/索引

代码语言:javascript
运行
复制
public function index() {
    //$this->User->recursive = 3;
$this->User->Behaviors->load('Containable');

/*  $user = $this->User->find('all', array(
'contain' => array(
'City.name' => array(
'State.name'=>array('Country.name')
))));
        debug($user);exit;
    */  

$this->paginate = array('contain'=>array('City'=>array('State'=>array('Country'))));
//debug($this->Paginator->paginate());exit;
    $this->set('users', $this->Paginator->paginate());
}

查看用户/索引

代码语言:javascript
运行
复制
 <table cellpadding="0" cellspacing="0">
    <thead>
    <tr>
            <th><?php echo $this->Paginator->sort('id'); ?></th>
            <th><?php echo $this->Paginator->sort('name'); ?></th>
            <th><?php echo $this->Paginator->sort('city_id'); ?></th>
            <th><?php echo $this->Paginator->sort('state_id'); ?></th>
            <th><?php echo $this->Paginator->sort('country_id'); ?></th>
            <th class="actions"><?php echo __('Actions'); ?></th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($users as $user): ?>
    <tr>
        <td><?php echo h($user['User']['id']); ?>&nbsp;</td>
        <td><?php echo h($user['User']['name']); ?>&nbsp;</td>
        <td>
            <?php echo $this->Html->link($user['City']['name'], array('controller' => 'cities', 'action' => 'view', $user['City']['id'])); ?>

        </td>

        <td>

            <?php echo $this->Html->link($user['City']['State']['name'], array('controller' => 'states', 'action' => 'view', $user['City']['State']['id'])); ?>
                </td>

        <td>

            <?php echo $this->Html->link($user['City']['State']['Country']['name'], array('controller' => 'countries', 'action' => 'view', $user['City']['State']['Country']['id'])); ?>
        </td>

    </tr>
<?php endforeach; ?>
    </tbody>
    </table>
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29961722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档