0

我有一个User模型belongsToProfile

现在,每当我想创建一个用户时,我也想创建我的个人资料。

为此,我创建了以下表格:

 <div class="span9" id="lejer">
    <?php echo $this->Form->create('User', array('class' => 'form-horizontal')); ?>
    <?php echo $this->Form->input('group_id', array('type' => 'hidden', 'value' => 3));?>
    <h3 class="heading3">Dine Personlige Informationer:</h3>

    <div class="registerbox">
        <fieldset>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Fornavn:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Fornavn', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Efternavn:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Efternavn', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Telefon:
                </label>

                <div class="controls">
                    <?php echo$this->Form->input('Telefon', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    E-mail:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('E-mail', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>

        </fieldset>

    </div>

    <h3 class="heading3">Your Address</h3>
    <div class="registerbox">
        <fieldset>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Addresse:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Addresse', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Postnr:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Postnr', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    By:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('By', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">
                    <span class="red">*</span>
                    Område:
                </label>
                <div class="controls">
                    <?php echo$this->Form->input('Område', array('class'=> 'input-xlarge', 'label' => '')); ?>
                </div>
            </div>
        </fieldset>

    </div>

现在,当我调试时,我可以看到所有数据都在以下范围内array

$this->request->data['User'];

现在在我的 UsersController 中,我有以下create操作:

    public function create() {
    if ($this->request->is('post')) {
        $this->User->create();
        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.'));
        }
    }
}

但是当我运行它时,只User创建 a 而不是 a Profile

谁能告诉我我做错了什么?我认为saveAll我会保存到它的关系模型中。

4

1 回答 1

1

您的代码中存在问题,您需要使用带有点分隔符的字段名称的用户模型名称,例如

$this->Form->input('User.email'); $sthis->Form->input('Profile.Telefon');

在此之后,您可以在控制器中使用以下代码来保存记录。

$this->Model->saveAssociated($this->request->data);

有关更多详细信息,请阅读此链接:-

> http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
于 2013-10-13T16:07:45.730 回答