gpt4 book ai didi

javascript - 如何在 CakePHP 2.6 中为同一用户在多个表中保存数据

转载 作者:行者123 更新时间:2023-11-28 03:54:36 27 4
gpt4 key购买 nike

同一个用户表的多表谁保存数据有hasMany关系 如果我将 $this->CompanyContact->saveAll($this->request->data) CompanyContact 更改为 User 则仅保存用户数据如果我将 CompanyContact 更改为 CompanyAddress 则仅保存地址

this my contoller function 

public function editcompanyprofile($id = null) {
$this->layout = 'main';

if ($this->request->is(array('put', 'post'))) {

$this->request->data['CompanyAddress'] = $this->request->data['Company']['CompanyAddress'];
unset($this->request->data['Company']['CompanyAddress']);

if ($this->request->data['CompanyProfile']['profile_img']['name'] != "") {
$sFileName = time() . "_" . str_replace(" ", "_", $this->request->data['CompanyProfile']['profile_img']['name']);
$sPath = "profile";
$file = $this->Pk->uploadImage($this->request->data['CompanyProfile']['profile_img'], $sFileName, $sPath);

if ($file['status'] == 'success') {
unset($this->request->data['CompanyProfile']['profile_img']);
$this->request->data['CompanyProfile']['profile_img'] = $file['url'];
} else {
$this->request->data['CompanyProfile']['profile_img'] = "";
}
} else {
unset($this->request->data['CompanyProfile']['profile_img']);
}
if ($this->CompanyContact->saveAll($this->request->data)) {

return $this->redirect(array('controller' => 'users', 'action' => 'companyprofile', $id));
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
} else {

$options = array('conditions' => array('CompanyContact.company_id'), 'recursive' => 2);
$this->request->data = $this->CompanyContact->find('first', $options);
$this->set('options', $options);
// print_r($this->request->data);exit;
}
}





<?php echo $this->Form->create('CompanyProfile', array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->input('User.id'); ?>


<lable id="view-edit-lable">First Name</lable>
<?php echo $this->Form->input('User.fname', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?>
<br>
<lable id="view-edit-lable">Last Name</lable>
<?php echo $this->Form->input('User.lname', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?>
<br>
<lable id="view-edit-lable">Company Name</lable>
<?php echo $this->Form->input('Company.company_name', array('class' => 'form-control', 'placeholder' => 'Enter First Name Here..', 'label' => false, 'required')); ?>
<?php echo $this->Form->input('Company.id', array('type' => 'hidden')); ?>
<br>

<lable id="view-edit-lable">Website Url</lable>
<input type="email" class="form-control" id="Last_name" placeholder="Website" >
<br>
<lable id="view-edit-lable">Email Address</lable>
<?php echo $this->Form->input('User.email', array('class' => 'form-control', 'placeholder' => 'Enter Last Name Here..', 'label' => false, 'required')); ?>

<br>
<lable id="view-edit-lable">Contact no</lable>
<div class="phone-list">
<div class="input-group phone-input" style="margin-bottom: 10px;">
<span class="input-group-btn">

<button type="submit" style="height:45px;font-size: 15px;" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-earphone" data-toggle="dropdown" aria-expanded="false"></span></button>
</span>
<?php echo $this->Form->input('User.mobile_number', array('class' => 'form-control', 'placeholder' => '+1 (999) 999 9999', 'label' => false)); ?>

</div>

</div>
<br>

<lable id="view-edit-lable">Address</lable>
<?php echo $this->Form->input('Company.CompanyAddress.address1', array('class' => 'form-control', 'label' => false)); ?>
<?php echo $this->Form->input('Company.CompanyAddress.id', array('type' => 'hidden')); ?>
<br>
<lable id="view-edit-lable">Address 2</lable>
<?php echo $this->Form->input('Company.CompanyAddress.address2', array('class' => 'form-control', 'label' => false, 'placeholder' => 'Floor #')); ?>
<br>

<lable id="view-edit-lable">City</lable>
<?php echo $this->Form->input('Company.CompanyAddress.city', array('class' => 'form-control', 'label' => false)); ?>
<br>
<lable id="view-edit-lable">State</lable>
<?php echo $this->Form->input('Company.CompanyAddress.state', array('type' => 'select', 'options' => array('New York' => 'New York', 'Connecticut' => 'Connecticut', 'New Jersey' => 'New Jersey'), 'selected' => 'New York', 'label' => false, 'empty' => 'Select State', 'style' => 'width:100%;',
'class' => 'btn dropdown-toggle selectpicker btn-default'));
?>
<br>
<lable id="view-edit-lable">Zip Code</lable>
<?php echo $this->Form->input('Company.CompanyAddress.zipcode', array('type' => 'number', 'class' => 'form-control', 'label' => false)); ?>
<br>
<lable id="view-edit-lable">Country</lable>
<?php echo $this->Form->input('Company.CompanyAddress.country', array('type' => 'select', 'options' => array('USA' => 'USA'), 'selected' => 'USA', 'label' => false, 'empty' => 'Select State', 'style' => 'width:100%;',
'class' => 'btn dropdown-toggle selectpicker btn-default'));
?>


<?php echo $this->Form->end(); ?>

最佳答案

尝试替换这段代码

if ($this->CompanyContact->saveAll($this->request->data)) {

为此:

if ($this->CompanyContact->saveAll($this->request->data) && $this->CompanyAddress->saveAll($this->request->data)) {

关于javascript - 如何在 CakePHP 2.6 中为同一用户在多个表中保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com