dougong1031 2015-04-30 07:33
浏览 33
已采纳

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

I have these models : Country,State,City,User with relations : Country hasMany State, State hasMany City, City hasMany User,

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

Now I want to fetch all the users with not only their cities but also countries and states. How can I do this?

Controller users/index:

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

output :

    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 :

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

Model State :

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

Model City :

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

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

1条回答 默认 最新

  • duan0417 2015-05-01 06:38
    关注

    I have resolved. Thanks to the cakephp community. Controller users/index

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

    View users/index

     <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?