0
我已經有這個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>
但是有什麼不對,因爲關聯的數據永遠不會被保存並且找不到錯在哪裏?任何幫助我嗎?
那麼我的信息模型是這個http://pastebin.com/UGTxPkuK,我認爲這很好,請看看,我也用saveAll但沒有成功 – ReynierPM 2012-02-24 19:26:34
不知道它是否會解決它,但嘗試改變這一點: 公共$的hasMany =陣列( '的教育'=>陣列( ... ), '經驗'=>陣列( ... ), '附件'=>陣列( ... ) ); 到 公共$的hasMany =陣列( '教育'=>陣列( ... ), '體驗'=>陣列( ... ), '附件'=>陣列( 。 .. ) ); – 2012-02-24 19:58:53
哇...你救了我一天Caio,非常感謝。我可以在這裏問另一個相關問題,或需要爲此目的打開另一個帖子嗎? – ReynierPM 2012-02-24 20:12:32