CakePHP编辑不加载外表数据

时间:2014-03-01 13:29:18

标签: cakephp cakephp-appmodel cakephp-2.4

我想让我的编辑工作我需要在加载用户数据时加载联系人详细信息数据。我以与检索角色列表的方式类似的方式设置数据。我也不知道如何根据目前的模型检索我的硬编码来检索28.非常感谢提供的任何帮助。

public function edit($id = null) {
    //Populate roles dropdownlist          
    $data = $this->User->Role->find('list', array('fields' => array('id', 'name')));
    $this->set('roles', $data);

    $data2 = $this->User->ContactDetail->find('first', array(

        'conditions' => array('ContactDetail.id' =>'28')));        
    $this->set('contactdetails', $data2);

    if (!$this->User->exists($id)) {
        throw new NotFoundException(__('Invalid user'));
    }
    if ($this->request->is(array('post', 'put'))) {
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    } else {
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->request->data = $this->User->find('first', $options);
    }
}

我的观点以下列方式设置

<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend><?php echo __('Edit User'); ?></legend>
    <?php
        echo $this->Form->input('id');
                echo $this->Form->input('username');
        echo $this->Form->input('password');
        echo $this->Form->input('role_id');

                echo $this->Form->input('ContactDetail.name');
                echo $this->Form->input('ContactDetail.surname');
                echo $this->Form->input('ContactDetail.address1');
                echo $this->Form->input('ContactDetail.address2');
                echo $this->Form->input('ContactDetail.country');
                echo $this->Form->input('ContactDetail.email');
                echo $this->Form->input('ContactDetail.fax');


          ?>      
                <label>Are you interested in buying property in Malta?</label>
          <?php  
                $interest_buy = array('0'=>'no','1' => 'yes');
                echo $this->Form->input('ContactDetail.interest_buy_property',array('type'=>'radio','options'=>$interest_buy,'value'=>'0','legend'=>FALSE));
          ?>      
                <label>Are you interested in renting property in Malta?</label>
          <?php  
                $interest_rent = array('0'=>'no','1' => 'yes');
                echo $this->Form->input('ContactDetail.interest_rent_property',array('type'=>'radio','options'=>$interest_rent,'value'=>'0','legend'=>FALSE));
                echo $this->Form->input('ContactDetail.mobile');
                echo $this->Form->input('ContactDetail.phone');
                echo $this->Form->input('ContactDetail.postcode');
                echo $this->Form->input('ContactDetail.town');

                echo $this->Form->input('ContactDetail.newsletter',array('type'=>'checkbox','label'=>'Would you like to register for the newsletter?' ,'checked'=>'1','legend'=>FALSE,));
         ?>       
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

用户模型

public $primaryKey = 'id';

public $displayField = 'username';

public function bindNode($user) {
    return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}

public function beforeSave($options = array()) {
    $this->data['User']['password'] = AuthComponent::password(
                    $this->data['User']['password']
    );
    return true;
}

public $belongsTo = array(
    'Role' => array('className' => 'Role'));

public $hasOne = array(
    'ContactDetail' => array(
        'foreignKey' => 'id'));

public $actsAs = array('Acl' => array('type' => 'requester', 'enabled' => false));

public function parentNode() {
    if (!$this->id && empty($this->data)) {
        return null;
    }
    if (isset($this->data['User']['role_id'])) {
        $roleId = $this->data['User']['role_id'];
    } else {
        $roleId = $this->field('role_id');
    }
    if (!$roleId) {
        return null;
    } else {
        return array('Role' => array('id' => $roleId));
    }
}

}

ContactDetail模型

public $primaryKey = 'id';
public $displayField = 'name';  

3 个答案:

答案 0 :(得分:0)

一旦您的用户模型hasOne ContactDetail ,您就不需要两次检索信息了,因为您已经完成了{{ 1}}行,所有关联模型也将检索。

所以,你的$this->request->data看起来像这样:

Controller

您的public function edit($id = null) { if (!$this->User->exists($id)) { throw new NotFoundException(__('Invalid user')); } // Roles $roles = $this->User->Role->find('list', array('fields' => array('id', 'name'))); $this->set(compact('roles'); if ($this->request->is(array('post', 'put'))) { if ($this->User->saveAll($this->request->data)) { $this->Session->setFlash(__('The user has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The user could not be saved. Please, try again.')); } } else { $this->request->data = $this->User->read(null, $id); } } ContactDetail&#39; 字段如下所示:

View

所有与模型echo $this->Form->input('ContactDetail.name'); 相关的字段都是如此。您可以在此处找到更多详细信息: Saving Your Data

用户型号:

ContactDetail

答案 1 :(得分:0)

如果我说得对,那么每个用户都有1个联系人详细信息行。在这种情况下,您需要在用户模型中定义:

public $hasOne = array(
    'ContactDetail' => array(
        'foreignKey' => 'id',
    ),
);

像这样,你的id将在两个表中同步。如果你不想把id作为外键,你不必这么做,这只是一个建议。

接下来,当您在控制器中检索数据时,您可以执行以下操作:

$this->User->Behaviors->load('Containable');
$user = $this->User->find('first', array(
    'conditions' => array('User.id' => $id),
    'contain' => array('ContactDetail'),
));

现在我不知道是否有自动方式来执行此操作,但我手动对数据进行排序以填充输入。我猜你会得到像 array('User'=&gt; array(),'ContactDetail'=&gt; array())这样的结构。

$user['User']['ContactDetail'] = $user['ContactDetail'];
unset($user['ContactDetail']);
$this->request->data = $user;

然后在您的视图中将字段设置为输入数组:

$this->Form->create('User');
$this->Form->input('User.some_user_field');
$this->Form->input('User.ContactDetail.some_contact_detail_field');

这应该填写你的字段。当你保存数据时,如果你的数组是这样的结构,你可以使用 saveAssociated():

$this->User->saveAssociated($this->request->data, array('deep' => true));

修改

如果您的关系被定义为 User hasMany ContactDetail ,那么您需要构建您的数据,如下所示:

$this->request->data = array(
    'User' => array(
        // user data
        'ContactDetail' => array(
            [0] => array(
                //contact data
            ),
        ),
    ),
);

在你看来:

$this->Form->input('User.ContactData.0.field')

这仅适用于1行,如果在子表上需要更多行且输入为1,请相应地执行逻辑。

答案 2 :(得分:0)

我找到的最佳解决方案是设置

public $belongsTo = array(
        'Role' => array('className' => 'Role')
    ,      'ContactDetail' => array('className' => 'ContactDetail'));

这样加载了联系人详细信息数据。虽然保存的数据不会更新contactdetails。

我保存用过

$this->User->saveAll($this->request->data)

这完全有效