gpt4 book ai didi

php - 如何通过在 cakephp 2.x 中使用可包含行为来获取国家、城市、州的用户

转载 作者:行者123 更新时间:2023-11-29 22:29:12 24 4
gpt4 key购买 nike

我有这些型号:国家、州、城市、用户与关系:国家有很多州,该州有许多城市,城市有很多用户,

州属于国家/地区,城市属于州,用户所属城市,

现在我想获取所有用户,不仅包括他们的城市,还包括国家和州。 我怎样才能做到这一点?

Controller 用户/索引:

 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());
}

输出:

    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'
)
)
)

型号国家:

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

模型状态:

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' => ''
)
);

型号城市:

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' => ''
)
);

型号用户:

public $belongsTo = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

最佳答案

我已经解决了。感谢 cakephp 社区。 Controller 用户/索引

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());
}

查看用户/索引

 <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>

关于php - 如何通过在 cakephp 2.x 中使用可包含行为来获取国家、城市、州的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29961722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com