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

发布于 2025-01-08 12:25:16 字数 4112 浏览 3 评论 0原文

我有这个数据库模型 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>

但是那里出了问题,因为关联的数据从未被保存并且无法找到问题所在?有谁能帮我吗?

I've have this DB Model http://www.dropmocks.com/mBgqjs and I'm using CakePHP to build a simple application just for learning where I have this add() method:

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

and this is my add.ctp file:

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

but something is wrong there because the associated data never is saved and can't find what is wrong? Can any help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2025-01-15 12:25:16

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

Make sure your relations are correctly created in the models and use saveAll instead of saveAssociated, as the saveAssociated can't handle saving multiple entries at one time. What saveAll does is basically combining saveMany and saveAssociated into one single save method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文