【问题标题】:Why the data isn't saved in the relationship tables?为什么数据没有保存在关系表中?
【发布时间】:2012-02-24 18:19:09
【问题描述】:

我有这个 DB 模型 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>

但是那里出了点问题,因为关联的数据从未被保存并且找不到问题所在?有什么可以帮助我的吗?

【问题讨论】:

    标签: php cakephp frameworks


    【解决方案1】:

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

    【讨论】:

    • 嗯,我的信息模型是 pastebin.com/UGTxPkuK,我认为这很好,请看一下,我也在使用 saveAll 但没有成功
    • 不知道它是否会解决它,但尝试改变它: public $hasMany = array( 'Educations' => array( ... ), 'Experiences' => array( . .. ), '附件' => array( ... ) ); to public $hasMany = array( 'Education' => array( ... ), 'Experience' => array( ... ), 'Attachment' => array( ... ) );
    • 哇...你救了我的一天 Caio,非常感谢。我还有其他相关问题可以在这里问吗?或者需要为此打开另一个帖子?
    • 打开另一个帖子。这很好,所以如果有人有同样的问题,他们可以轻松找到它。
    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 2012-02-10
    • 2016-10-21
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    相关资源
    最近更新 更多