Hello everyone I'm getting confused because when I'm running a code with some condition only 1 column getting updated and the others column stay null, I'm sure there is no typo with the column name in table nor in code here is my code :
public function solusi_line($id)
{
$idasli = base64_decode($id);
$solusi = solusi::where('request_id', $idasli)->orderBy('created_at', 'asc')->get();
$count = solusi::where('request_id', $idasli)->orderBy('created_at', 'desc')->count();
$cekakun = Session::get('id');
$adminkah = Admin::where('user_id', $cekakun)->count();
// dd($solusi);
//jika admin ke bagian sini
if ($adminkah != 0) {
# code...
return view('admin.detail_feedback', compact('solusi', 'count'));
} else {
return view('user.detailfeedback_user', compact('solusi', 'count'));
}
//klo user kebagian user
}
public function FirstFeedback($id)
{
$datas = Kirim::where('id', $id)->first();
return view('admin.detailprogress', compact('datas'));
}
//solusi dari admin
public function solusiPost(Request $request)
{
$itungsolusi = solusi::where('request_id', $request->idRequest)->count();
$data = new solusi();
$data->request_id = $request->idRequest;
$data->pengirim_id = Session::get('id');
$data->penerima_id = $request->idPenerima;
$data->solusi = $request->solusi;
$file = $request->file('lampiran_solusi');
if (!empty($file)) {
$ext = $file->getClientOriginalExtension();
$name = time() . '.' . $ext;
$file->move('upload/lampiran/', $name);
$data->lampiran = $name;
$data->url_lampiran = url('upload/lampiran') . "/" . $name;
} else {
$data->lampiran = null;
$data->url_lampiran = null;
}
if ($data->save()) {
//matiin sementara
$check = DB::table('kirims')->where('id', $data->request_id)->first();
$user = Kirim::find($check->id);
// dd($user);
if (!is_null($user)) {
// $user->update(['status' => 'CheckByUser', 'status_feedback' => 'Ya']);
$user->update(['remarks' => 'Ya', 'status' => 'CheckByUser', 'status_feedback' => 'Ya']);
}
//just status column is success to update but not the remarks and status_feedback column
$id_solusi = $data->id;
$thisUser = DB::table('kirims')
->join('simpan_users', 'kirims.pengirim_id', '=', 'simpan_users.user_id')
->join('solusis', 'kirims.id', '=', 'solusis.request_id')
->where('kirims.id', $data->request_id)
->where('solusis.id', $id_solusi)
->select('kirims.email', 'kirims.ticket_id', 'solusis.solusi', 'solusis.lampiran', 'solusis.url_lampiran')
->first();
// dd($thisUser);
here the problem
$check = DB::table('kirims')->where('id', $data->request_id)->first();
$user = Kirim::find($check->id);
// dd($user);
if (!is_null($user)) {
// $user->update(['status' => 'CheckByUser', 'status_feedback' => 'Ya']);
$user->update(['remarks' => 'Ya', 'status' => 'CheckByUser', 'status_feedback' => 'Ya']);
}
//just status column is success to update but not the remarks and status_feedback column
I really appreciate for any help, I getting stuck because laravel not showing error too, thank you.
You should check your Kirim model, where you need to define $fillable property. Something like below.
class Kirim extends Model {
protected $fillable = ['remarks', 'status', 'status_feedback',''];
// All fields inside $fillable array can be mass-assigned
}
use update statement like this.
$user->remarks = 'Ya',
$user->status = 'CheckByUser',
$user->status_feedback = 'Ya'
$user->update();
few days ago i faced the same issue and fixed it as i told you .
Related
I am a beginner for Codeigniter. I'm currently creating a registration page. I want to enter data into 4 tables at once using CodeIgniter. the table is tbl_asesi, tbl_pdd_non_formal, tbl_work, tbl_organization, but new data comes in 1 table, namely tbl_asesi while the other tables are still empty. can you help me fix the code?
The controller that I use is as follows
my controller :
<?PHP
defined('BASEPATH') OR exit('No direct script access allowed');
class Pendaftaran extends CI_Controller {
public function index()
{
$data = array(
'bidang' => $this->m_pendaftaran->get_bidang(),
'subbidang' => $this->m_pendaftaran->get_subbidang(),
'jenis_bidang' => $this->m_pendaftaran->get_jenis_bidang(),
'level' => $this->m_pendaftaran->get_level(),
'okupasi' => $this->m_pendaftaran->get_okupasi(),
'unitinti' => $this->m_pendaftaran->get_unitinti(),
'unitpilihan1' => $this->m_pendaftaran->get_unitpilihan1(),
'unitpilihan2' => $this->m_pendaftaran->get_unitpilihan2(),
'unitpilihan3' => $this->m_pendaftaran->get_unitpilihan3(),
'bidang_selected' => '',
'subbidang_selected' => '',
'jenis_bidang_selected' => '',
'level_selected' => '',
'okupasi_selected' => '',
'unitinti_selected' => '',
'unitpilihan1_selected' => '',
'unitpilihan2_selected' => '',
'unitpilihan3_selected' => '',
);
$this->template->load('static','pendaftaran',$data);
}
function __construct(){
parent::__construct();
$this->load->library(array('form_validation'));
$this->load->model('m_pendaftaran');
$this->load->helper(array('form', 'url'));
}
public function daftarasesi() {
$this->form_validation->set_rules('nama', 'NAMA','required');
$this->form_validation->set_rules('nik', 'NIK','required');
$this->form_validation->set_rules('jenis_kelamin', 'JENIS_KELAMIN','required');
$this->form_validation->set_rules('alamat', 'ALAMAT','required');
$this->form_validation->set_rules('jabatan', 'JABATAN','');
$this->form_validation->set_rules('asal_institusi', 'ASAL_INSTITUSI','required');
$this->form_validation->set_rules('alamat_institusi', 'ALAMAT_INSTITUSI','required');
$this->form_validation->set_rules('no_hp', 'NO_HP','required');
$this->form_validation->set_rules('email','EMAIL','required|valid_email');
$this->form_validation->set_rules('sd', 'SD','required');
$this->form_validation->set_rules('kota_sd', 'KOTA_SD','required');
$this->form_validation->set_rules('tahun_sd', 'TAHUN_SD','required');
$this->form_validation->set_rules('smp', 'SMP','required');
$this->form_validation->set_rules('kota_smp', 'KOTA_SMP','required');
$this->form_validation->set_rules('tahun_smp', 'TAHUN_SMP','required');
$this->form_validation->set_rules('sma', 'SMA','required');
$this->form_validation->set_rules('kota_sma', 'KOTA_SMA','required');
$this->form_validation->set_rules('tahun_sma', 'TAHUN_SMA','required');
$this->form_validation->set_rules('d', 'D');
$this->form_validation->set_rules('kota_d', 'KOTA_D');
$this->form_validation->set_rules('tahun_d', 'TAHUN_D');
$this->form_validation->set_rules('s1', 'S1');
$this->form_validation->set_rules('kota_s1', 'KOTA_S1');
$this->form_validation->set_rules('tahun_s1', 'TAHUN_S1');
$this->form_validation->set_rules('s2', 'S2');
$this->form_validation->set_rules('kota_s2', 'KOTA_S2');
$this->form_validation->set_rules('tahun_s2', 'TAHUN_S2');
$this->form_validation->set_rules('s3', 'S3');
$this->form_validation->set_rules('kota_s3', 'KOTA_S3');
$this->form_validation->set_rules('tahun_s3', 'TAHUN_S3');
$this->form_validation->set_rules('pelatihan', 'PELATIHAN');
$this->form_validation->set_rules('lembaga', 'LEMBAGA');
$this->form_validation->set_rules('kota_pelatihan', 'KOTA_PELATIHAN');
$this->form_validation->set_rules('tahun_pelatihan', 'TAHUN_PELATIHAN');
$this->form_validation->set_rules('kegiatan', 'KEGIATAN');
$this->form_validation->set_rules('institusi', 'INSTITUSI');
$this->form_validation->set_rules('tahun_kerja', 'TAHUN_KERJA');
$this->form_validation->set_rules('nama_organisasi', 'NAMA ORGANISASI');
$this->form_validation->set_rules('status_anggota', 'STATUS ANGGOTA');
$this->form_validation->set_rules('surat_organisasi', 'SURAT ORGANISASI');
$this->form_validation->set_rules('tahun_organisasi', 'TAHUN_ORGANISASI');
$this->form_validation->set_rules('bidang', 'BIDANG','required');
$this->form_validation->set_rules('subbidang', 'SUBBIDANG','required');
$this->form_validation->set_rules('level', 'LEVEL','required');
$this->form_validation->set_rules('jenis_bidang', 'JENIS_BIDANG','required');
$this->form_validation->set_rules('okupasi', 'OKUPASI','required');
$this->form_validation->set_rules('unitinti', 'UNITINTI','required');
$this->form_validation->set_rules('unitpilihan1', 'UNITPILIHAN1','required');
$this->form_validation->set_rules('unitpilihan2', 'UNITPILIHAN2','');
$this->form_validation->set_rules('unitpilihan3', 'UNITPILIHAN3','');
if($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('gagaldaftar', 'Maaf, Pendaftaran Gagal Dilakukan.');
$this->template->load('static','pendaftaran');
}else{
$data_asesi['nama'] = $this->input->post('nama');
$data_asesi['nik'] = $this->input->post('nik');
$data_asesi['jenis_kelamin'] = $this->input->post('jenis_kelamin');
$data_asesi['alamat'] = $this->input->post('alamat');
$data_asesi['jabatan'] = $this->input->post('jabatan');
$data_asesi['asal_institusi'] = $this->input->post('asal_institusi');
$data_asesi['alamat_institusi'] = $this->input->post('alamat_institusi');
$data_asesi['no_hp'] = $this->input->post('no_hp');
$data_asesi['email'] = $this->input->post('email');
$data_asesi['sd'] = $this->input->post('sd');
$data_asesi['kota_sd'] = $this->input->post('kota_sd');
$data_asesi['tahun_sd'] = $this->input->post('tahun_sd');
$data_asesi['smp'] = $this->input->post('smp');
$data_asesi['kota_smp'] = $this->input->post('kota_smp');
$data_asesi['tahun_smp'] = $this->input->post('tahun_smp');
$data_asesi['sma'] = $this->input->post('sma');
$data_asesi['kota_sma'] = $this->input->post('kota_sma');
$data_asesi['tahun_sma'] = $this->input->post('tahun_sma');
$data_asesi['d'] = $this->input->post('d');
$data_asesi['kota_d'] = $this->input->post('kota_d');
$data_asesi['tahun_d'] = $this->input->post('tahun_d');
$data_asesi['s1'] = $this->input->post('s1');
$data_asesi['kota_s1'] = $this->input->post('kota_s1');
$data_asesi['tahun_s1'] = $this->input->post('tahun_s1');
$data_asesi['s2'] = $this->input->post('s2');
$data_asesi['kota_s2'] = $this->input->post('kota_s2');
$data_asesi['tahun_s2'] = $this->input->post('tahun_s2');
$data_asesi['s3'] = $this->input->post('s3');
$data_asesi['kota_s3'] = $this->input->post('kota_s3');
$data_asesi['tahun_s3'] = $this->input->post('tahun_s3');
//tabel tbl_pdd_non_formal
$data_non_formal = array();
$data_non_formal['pelatihan'] = $this->input->post('pelatihan');
$data_non_formal['lembaga'] = $this->input->post('lembaga');
$data_non_formal['kota_pelatihan'] = $this->input->post('kota_pelatihan');
$data_non_formal['tahun_pelatihan'] = $this->input->post('tahun_pelatihan');
//tabel tbl_pekerjaan
$data_pekerjaan = array();
$data_pekerjaan['kegiatan'] = $this->input->post('kegiatan');
$data_pekerjaan['institusi'] = $this->input->post('institusi');
$data_pekerjaan['surat_pendukung'] = $this->input->post('surat_pendukung');
$data_pekerjaan['tahun_kerja'] = $this->input->post('tahun_kerja');
//tbl_organisasi
$data_organisasi = array();
$data_organisasi['nama_organisasi'] = $this->input->post('nama_organisasi');
$data_organisasi['status_anggota'] = $this->input->post('status_anggota');
$data_organisasi['surat_organisasi'] = $this->input->post('surat_organisasi');
$data_organisasi['tahun_organisasi'] = $this->input->post('tahun_organisasi');
//tbl_asesi
$data_asesi['bidang'] = $this->input->post('bidang');
$data_asesi['subbidang'] = $this->input->post('subbidang');
$data_asesi['level'] = $this->input->post('level');
$data_asesi['jenis_bidang'] = $this->input->post('jenis_bidang');
$data_asesi['okupasi'] = $this->input->post('okupasi');
$data_asesi['unitinti'] = $this->input->post('unitinti');
$data_asesi['unitpilihan1'] = $this->input->post('unitpilihan1');
$data_asesi['unitpilihan2'] = $this->input->post('unitpilihan2');
$data_asesi['unitpilihan3'] = $this->input->post('unitpilihan3');
// var_dump($data_asesi);
// var_dump($data_non_formal);
// var_dump($data_pekerjaan);
// var_dump($data_organisasi);
$this->m_pendaftaran->daftarasesi($data_asesi, $data_non_formal, $data_pekerjaan, $data_organisasi);
// $this->m_pendaftaran->daftarasesi($data);
$this->session->set_flashdata('berhasil', 'Pendaftaran Berhasil Dilakukan.');
$this->template->load('static','pendaftaran');
}
}
public function lakukan_download_asesi(){
force_download('berkas/berkas_asesi.rar',NULL);
}
}
the model that I use is as follows
my model :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_pendaftaran extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{
// insert data asesi //
$this->db->insert('tbl_asesi',$asesi);
$nik_fk = $this->db->insert_nik();
// insert data non formal //
$data_non_formal['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pdd_non_formal',$non_formal);
return $insert_nik = $this->db->insert_nik();
// insert data pekerjaan //
$data_pekerjaan['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pekerjaan',$pekerjaan);
return $insert_nik = $this->db->insert_nik();
// insert data organisasi //
$data_organisasi['nik_fk'] = $nik_fk;
$this->db->insert('tbl_organisasi',$organisasi);
return $insert_nik = $this->db->insert_nik();
}
I think you're doing wrong and in Codeigniter there is no query builder function exists called $this->db->insert_nik() and you were returning this, so it was not working. So you shouldn't use this.
You can insert data into multiple tables from one form as following way and Use this in your model.
public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{
$this->db->trans_start();
// insert data asesi //
$this->db->insert('tbl_asesi',$asesi);
// insert data non formal //
$this->db->insert('tbl_pdd_non_formal',$non_formal);
// insert data pekerjaan //
$this->db->insert('tbl_pekerjaan',$pekerjaan);
// insert data pekerjaan //
$this->db->insert('tbl_pekerjaan',$pekerjaan);
// insert data organisasi //
$this->db->insert('tbl_organisasi',$organisasi);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
You can run as many queries as you want between the start/complete
functions and they will all be committed or rolled back based on
success or failure of any given query.
N.B: Always use transaction when you're dealing with multiple insertion/updation.
I need to reorder the column sort_order.
It is, select the position where to allocate the category and
recalculate the table.
For example, if I select category 3, insert at next and reorder sort_order.
I tried this code but something is wrong:
public function actionUp($id) {
$model = $this->loadModel($id);
$prev = Category::find()
->andWhere(['<', 'sort_order', $model->sort_order])
->orderBy(['sort_order' => SORT_DESC])
->limit(1)->one();
$current = $model->sort_order;
$model->sort_order = $prev->sort_order;
$prev->sort_order = $current->sort_order;
$model->save();
$prev->save();
return $
this->redirect(['index']);
}
How I can reorder this column?
Thank you for any help.
public function actionUp($id, $newPosition=null) {
$model = Category::findOne($id);
if(!$model)
throw new UserException('We were unable to find a matching record');
$prev = Category::find()
->andWhere(['<', 'sort_order', $model->sort_order])
->orderBy(['sort_order' => SORT_DESC])
->limit(1)->one();
if($prev)
$prevOrder = $prev->sort_order;
else
$prevOrder = 0;
$currentOrder = $model->sort_order;
If($newPosition)
$model->sortOrder = $newPosition;
else
$model->sort_order = $prevOrder;
$prev->sort_order = $currentOrder;
$model->save();
$prev->save();
return $this->redirect(['index']);
}
actionUp and Down work well, thank you very much
And when I'm going to add a new category, how can I indicate the position, based o parent_id?
public function create(CategoryCreateForm $form): Category
{
$channel = Category::create(
$form->parentId,
$form->name,
$form->code,
$form->description
);
$category = $this->categories->get($form->parentId);
$next = Category::find()
->andWhere(['<', 'sort_order', $category->sort_order])
->orderBy(['sort_order' => SORT_DESC])
->limit(1)->one();
if ($next)
$nexOrder = $next->sort_order;
else
$nexOrder = 0;
$currentOrder = $category->parent_id;
if ($newPosition)
$category->parent_id =$newPosition;
else
$category->parent_id = $nexOrder;
$next->sort_order = $currentOrder;
$prev->save();
$this->categories->save($category);
return $category;
}
It does not work this way, what is wrong.
I have this, in the Categoryservice
public function moveUp($id){
$model = $this->categories->get($id);
$prev = Category::find()
->andWhere(['<', 'sort_order', $model->sort_order])
->orderBy(['sort_order' => SORT_DESC])
->limit(1)->one();
if($prev)
$prevOrder = $prev->sort_order;
else
$prevOrder = 0;
$currentOrder = $model->sort_order;
$model->sort_order = $prevOrder;
$prev->sort_order = $currentOrder;
$prev->save();
$this->categories->save($model);
}
You were right, one of the categories did not have sort_order, it was null, and it gave the error.
I have one more question, how can it do the same but, when creating to choose where to position it?
Am trying to update multiple records in the database but my case I have a column total, which I want to update to different values. I haven't tried much here but hope I could get a clue on how to go about this.
My controller
public function update_record()
{
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
if ($this->some_model->batch_data('records', $total, $id) == true) {
echo "yes";
} else {
echo "no";
}
}
The Model
public function batch_data($table, $data, $where)
{
$this->db->where_in('id',$where);
$this->db->set('total',$data);
$this->db->update($table);
return true;
}
I have not tested this yet but currently looking for a more and efficient way of doing this.
If you still want to update multiple records using the update_batch method, you could first assign the id and total as key-value arrays, then use the update_batch method.
Controller :
public function update_record()
{
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$update_data = [];
foreach ($id as $key => $value) {
$update_data[] = [
'id' => $value,
'total' => $total[$key]
];
}
if ($this->some_model->batch_data('records', $update_data) == true) {
echo "yes";
} else {
echo "no";
}
}
Model :
public function batch_data($table, $data)
{
$this->db->update_batch($table, $data, 'id'); // this will set the id column as the condition field
return true;
}
Output :
// preview query output :
// UPDATE `records`
// SET
// `total` =
// CASE
// WHEN `id` = '19821' THEN 8
// WHEN `id` = '19923' THEN 118
// WHEN `id` = '19966' THEN 90
// WHEN `id` = '19967' THEN 100
// ELSE `total`
// END
// WHERE `id` IN ('19821', '19923', '19966', '19967')
As per my comment. This is a method that combines the array in to key/value pairs and updates them 1x1 wrapped in a transaction (so if one query fails nothing changes).
This is the method I would personally use as I don't like update_batchs internal workings (cases).
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$combined = array_combine($id, $total);
if (count($combined) > 0) {
$this->db->trans_start();
foreach ($combined as $id => $total) {
$this->db->set('total', $total);
$this->db->where('id', $id);
$this->db->update('sometable');
}
$this->db->trans_complete();
return $this->db->trans_status();
}
return true;
Try this, Only for single where condition
Controller:
public function update_record()
{
$tableName = "records";
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$update_data = [];
foreach ($id as $key => $value) {
$update_data[] = [
'id' => $value,
'total' => $total[$key]
];
}
$whereKey = 'id';
$this->$this->some_model->batch_data($tableName, $updateData, $whereKey);
}
Model:
public function batch_data($tableName, $updateData, $whereKey)
{
$this->db->update_batch($tableName, $updateData, $whereKey);
}
I'm trying to edit and save some articles, but I receive this error : Creating default object from empty value. What's wrong in my code?Because, at edit I have my subject, but and submit I get this error.
My error : https://imgur.com/a/eWGqc5B
Controller
public function update($type, $id)
{
/* print_r(Input::all()); die; */
if($type == "News")
{
$article = \App\News::find($id);
$article->subject = Request::input('subject');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->information = Request::input('information');
$article->update();
}
if($type == "Event")
{
$article = \App\Event::find($id);
$article->subject = Request::input('subject');
$comm->comments = Request::input('comments');
$article->public = Request::input('public');
$article->category_id = Request::input('category_id');
$article->event_type_id = Request::input('event_type_id');
$article->country = Request::input('country');
$article->starts = Request::input('starts');
$article->ends = Request::input('ends');
$article->organizer = Request::input('organizer');
$article->address = Request::input('address');
$article->city = Request::input('city');
$article->website = Request::input('website');
$article->email = Request::input('email');
$article->telephone = Request::input('telephone');
$article->information = Request::input('information');
$article->update();
}
return redirect(URL::previous());
Line 669 in ArticleController.php
$article->subject = Request::input('subject');
Here is my editEvent code : https://codepen.io/anon/pen/YodwKO
my articles.blade code : https://codepen.io/anon/pen/jjXWOb
My route:
`
Route::post('admin/article/update/{type?}/{id?}', [ 'as' => 'update.article', 'uses' => 'ArticleController#update']);
`
The variable $comm is not defined. It looks like a typo.
I am able to save associated records in cakephp3 but if you look at the code , the records are not saved all at once with 1 save call.
I did have a problem trying to save all the records at once. If you look at how its done you see the Guardian and Users table are save separately.
The code works and saves the records but saving all at once in 1 save call has been an issue as I got an error on the guardian table.
Guardians have a 1 to many relationship with students
Students and Users have a 1 to 1
Students to subjects and availabilityFotStudents both have a many to many
Code
public function add($gId=0) {
$this->loadModel("Guardians");
$this->loadModel('PaymentTypes');
$this->set("title","Add Student");
$guardians=null;
if ($gId>0){
$guardians =$this->Guardians->get($gId);
}
if ($this->request->is('post')) {
if ( $this->request->data['studenttype']==0){ //enquiry
$this->request->data['students']['address_billing'] = 0;
$this->request->data['students']['student_enq'] = 1;
}
else if ( $this->request->data['studenttype']==1){ //waitlist
$this->request->data['students']['address_billing'] = 1;
$this->request->data['students']['student_enq'] = 0;
}
else if ( $this->request->data['studenttype']==2){ //skip waitlist
$this->request->data['students']['address_billing'] = 4;
$this->request->data['students']['student_enq'] = 0;
}
if ( $this->request->data['students']['tutoring_typest_id']==1){
$this->request->data['students']['group_status']=0;
}
else if ( $this->request->data['students']['tutoring_typest_id']>1){
$this->request->data['students']['group_status']=1;
}
if ($this->request->data['students']['tutoring_typest_id']==3 ){//group only
$this->request->data['students']['address_billing'] = 4;
}
$data = $this->request->data;
// debug($data);
$uname= $this->request->data['Users']['username'];
if ($this->Students->Users->findByUsername($uname)->count()) {
$this->Flash->error(__('Username exists. Please, try again.'));
return $this->redirect(["action"=>"add",$gId]);
}
$userId=0;
$entity = $this->Students->Users->newEntity($this->request->data['Users'],['validate'=>false]);
$entity->role = "student";
$entity['role_id'] = 4;
$entity = $this->Students->Users->save($entity);
// debug( $entity);
$studentUserId = $entity->id;
if($guardians==null) {
$guardians = $this->Guardians->newEntity($this->request->data['guardians'], ['validate' => false]);
}
$guardianEntity = $this->Guardians->save($guardians);
$guardians = $this->Students->newEntity();
$studentData = $this->request->data['students'];
$studentData['subjects'] = $this->request->data['subjects'];
$studentData['availability_for_students'] = $this->request->data['availability_for_students'];
$studentEntity = $this->Students->patchEntity($guardians,$studentData,
[
"validate"=>false,
'associated' => [
"AvailabilityForStudents"=>["validate"=>false],
"Subjects"=>["validate"=>false]
]
]
);
$studentEntity->guardian_id = $guardianEntity->id;
$studentEntity->user_id = $studentUserId;
$studentEntity = $this->Students->save($studentEntity,
[
"validate"=>false,
'associated' => [
"AvailabilityForStudents"=>["validate"=>false],
"Subjects"=>["validate"=>false]
]
]
);
if ($studentEntity) {
$this->Flash->success(__('The student has been saved'));
return $this->redirect(["action"=>"index2"]);
} else {
$this->Flash->error(__('The student could not be saved. Please, try again.'),'flash_alert');
}
}//post
$subjects = $this->Students->Subjects->find('list', array( 'order' => array('Subjects.name' => 'asc') ));
$weekdays = $this->Students->weekDays;
$tutoringTypes = $this->Students->TutoringTypeStudents->find('list');
//$payments = $this->Student->paymentOptions;
$this->PaymentTypes->primaryKey("name");
$payments = $this->PaymentTypes->find( 'list', array(
'fields' => array('PaymentTypes.name','PaymentTypes.name')) );
$referrals = $this->Students->Referrals->find('list');
$tutorGender = $this->Students->Lessons->Tutors->tutorGender;
$this->set('guardians', $guardians);
$this->set(compact('referrals','subjects', 'tutoringTypes', 'weekdays','payments','tutorGender'));
}
you can use Tranactional data save. Where you can run multiple save method. if any save method failed then no data will be save. so it works like "1 save call" which you mentioned