为什么数据没有保存在关系表中?

时间:2012-02-24 18:19:10

标签: php cakephp frameworks

我有这个数据库模型http://www.dropmocks.com/mBgqjs,我正在使用CakePHP来构建一个简单的应用程序,只是为了学习我有这个add()方法的地方:

public function add() {
    if ($this->request->is('post') && is_uploaded_file($this->request->data['Information']['picture']['tmp_name'])) {
        // Handling file uploads
        $upload_avatar_dir = WWW_ROOT . "uploads/avatar/";
        $new_file_name = $this->createRandomString() . substr($this->request->data['Information']['picture']['name'], -4);
        move_uploaded_file($this->request->data['Information']['picture']['tmp_name'], $upload_avatar_dir . $new_file_name);

        $this->request->data['Information']['picture'] = $upload_avatar_dir . $new_file_name;

        $this->Information->create();

        if ($this->Information->saveAssociated($this->request->data)) {
            $this->Session->setFlash(__('The information has been saved'), 'flash_success');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The information could not be saved. Please, try again.'), 'flash_error');
        }
    }
    $countries = $this->Information->Country->find('list');
    $this->set(compact('countries'));
}

这是我的add.ctp文件:

<div class="information form">
<?php echo $this->Form->create('Information', array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data'));?>
<fieldset>
    <legend><?php echo __('Add Information'); ?></legend>

    <ul class="nav nav-tabs">
        <li class="active"><a href="#personal" data-toggle="tab"><?php echo __('Personal') ?></a></li>
        <li><a href="#extra" data-toggle="tab"><?php echo __('Other Information') ?></a></li>
    </ul>

    <div class="tab-content">
        <div class="tab-pane active" id="personal">
            <div class="row">
                <div class="span4">
                    <?php
                        echo $this->Form->input('name', array('label' => __('Name')));
                        echo $this->Form->input('lastname');
                        echo $this->Form->input('email');
                        echo $this->Form->input('countries_id');
                        echo $this->Form->input('mobile_phone');
                        echo $this->Form->input('home_phone');
                    ?>
                </div><!--./span4-->
                <div class="span4">
                    <?php
                        echo $this->Form->input('address', array('cols' => 50, 'rows' => 5));
                        echo $this->Form->input('picture', array('type' => 'file'));
                        echo $this->Form->input('recruitment_status', array('label' => __('Status'),'options'=>array('1'=>__('Call for Interview'),'2'=>__('Rejected'),'3'=>__('Pending for Upcoming Oportunities'))));
                    ?>
                </div><!--./span4-->
            </div><!--./row-->
        </div>
        <div class="tab-pane" id="extra">
            <?php
                echo $this->Form->input('Education.0.education_content');
                echo $this->Form->input('Experience.0.experience_content');
                //echo $this->Form->input('Attachment.attachment_route', array('type' => 'file', 'multiple'));
                echo $this->Form->input('other_information');
            ?>
        </div>
    </div>
</fieldset>
<?php 
 $options = array(
'value' => __('Save!'),
'class' => 'btn btn-inverse'
 );
?>  
<div class="paginator-footer"> <?php echo $this->Form->end($options);?> </div>
</div>

但是那里出了点问题,因为关联的数据永远不会被保存,找不到有什么问题?可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

确保在模型中正确创建关系并使用saveAll而不是saveAssociated,因为saveAssociated无法同时处理多个条目。 saveAll的作用基本上是将saveMany和saveAssociated组合成一个保存方法。