Why multiple records is not updating using cakephp? - cakephp-2.0

I am giving my controller code below :
for ($i = 0; $i < count($club_open); $i++) {
$infomation["ClubOpenDay"]["club_id"] = $club_id;
$infomation["ClubOpenDay"]["days"] = $club_open[$alpha[$i]];
$infomation["ClubOpenDay"]["open_time"] = $club_open_time[$alpha[$i]];
$infomation["ClubOpenDay"]["close_time"] = $club_close_time[$alpha[$i]];
$infomation["ClubOpenDay"]["status"] = $club_status[$alpha[$i]];
$this->ClubOpenDay->updateAll(
array('ClubOpenDay.status' => "'".$infomation["ClubOpenDay"]["status"]."'"),
array('ClubOpenDay.club_id' => "'".$club_id."'")
);
}
using this code, I am not able to update multiple records. Any idea?

instead of this:
$this->ClubOpenDay->updateAll(
array('ClubOpenDay.status' => "'".$infomation["ClubOpenDay"]["status"]."'"),
array('ClubOpenDay.club_id' => "'".$club_id."'")
);
write the following:
$this->ClubOpenDay->updateAll(
array('ClubOpenDay.status' => $infomation["ClubOpenDay"]["status"]),
array('ClubOpenDay.club_id' => $club_id)
);

Related

controller store function array laravel 5.5

I need some help. I have a problem with store function with array in laravel. When I submit my form, I got error massage that "ksort() expects parameter 1 to be array, string given".
This is my store function :
public function store(request $request) {
$input=$request->all();
$images=array();
$total = count($request->path_scan_ijazah);
// return $total;
if($files=$request->file('path_scan_ijazah')){
for ($i=0; $i < $total; $i++) {
$nip[] = $request->nip;
$instansi[] = $request['nama_instansi_pendidikan'][$i];
$jurusan[] = $request['nama_jurusan'][$i];
$jenjang[] = $request['jenjang_pendidikan'][$i];
$gelar[] = $request['gelar'][$i];
$thn_masuk = $request['tahun_masuk'][$i];
$thn_lulus = $request['tahun_lulus'][$i];
$path = $request['path_scan_ijazah'][$i]->store('public/upload/ijazah');
$images[] = $path;
$data[] = Education::insert([
'nip_employee' => $nip,
'nama_instansi_pendidikan' => $instansi,
'nama_jurusan' => $jurusan,
'jenjang_pendidikan' => $jenjang,
'gelar' => $gelar,
'tahun_masuk' => $thn_masuk,
'tahun_lulus' => $thn_lulus,
'path_scan_ijazah' => $images
]);
}
// dd($data);
}
Please help me, what should I do?

Laravel - Save multiple field values in json/array in a column

I have 3 form fields name, address, profile_photo. I want to save there 3 fields in json/array format in a single column so that I can retrieve it later in view blade.
My form looks like this
I tried
$customer_details= new Customer;
{
$profile_photo = $request->file('profile_photo');
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$cust_details=json_encode(request(['name', 'address', $profile_ph]));
$customer_details->details = $cust_details;
$customer_details->save();
}
With this profile_photo is not saved but other data are saved as:
{"name":["john","Sam"],"address":["CA","FL"]}
How can I save all there fields including profile_photo and show each details distinctly later on view blade?
Hope this will help you.
$customer_details = new Customer;
$profile_photo = $request->file('profile_photo');
$profile_ph = [];
for ($i = 0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph[$i] = '/images/' . $pro_fileNameToStore;
}
$data = [
'name' => $request->name,
'address' => $request->address,
'profile_ph' => $profile_ph
];
$customer_details->details = json_encode($data);
$customer_details->save();
The details column should be text if you are using mysql.
Try this:
$names = $request->input('name');
$addresses = $request->input('address');
$profile_photos = $request->file('profile_photo');
for ($i = 0; $i < count($names); $i++) {
$customer_details = new Customer;
$pro_fileExt = $profile_photos[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time() . '.' . $pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photos[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR . $pro_fileNameToStore);
$profile_ph = '/images/' . $pro_fileNameToStore;
$cust_details = json_encode([
'name' => $names[$i],
'address' => $addresses[$i],
'profile_photo' => $profile_ph,
]);
$customer_details->details = $cust_details;
$customer_details->save();
}
Try this with single database query
$profile_photo = $request->file('profile_photo');
$finalData = [];
$names = [];
$addresses = [];
$images = [];
for ($i=0; $i < count(request('profile_photo')); $i++) {
$pro_fileExt = $profile_photo[$i]->getClientOriginalExtension();
$pro_fileNameToStore = time().'.'.$pro_fileExt;
$pro_pathToStore = public_path('images');
Image::make($profile_photo[$i])->save($pro_pathToStore . DIRECTORY_SEPARATOR. $pro_fileNameToStore);
$profile_ph = '/images/'.$pro_fileNameToStore; }
$names[] = $request->input('name');
$addresses[] = $request->input('address');
$images[] = $profile_ph;
}
$cust_details=json_encode(['name' => $names, 'address' => $addresses, 'images' => $images]);
$customer_details->insert($cust_details);

update multiple array values with multiple ids in laravel 5.1

My query as follows
$dueid = array('1','2');
for($n = 0; $n< count($post['percentage']); $n++) {
$due=invoiceduedates::whereIn('id',$dueid)
->update(array(
'percentage' => $post['percentage'][$n],
'amount'=>$post['pamount'][$n],
'date' => $post['date'][$n]
)
);
}
But in table,at 1st and 2nd ids the 2nd array data is getting updated.Please help me to sort it out.
I don't know what you what to get... but in this way it's normal that you get what you get. I can only sugest you to try like this:
$dueid = array('1','2');
$dues = invoiceduedates::whereIn('id',$dueid)->get();
for($n = 0; $n< count($post['percentage']); $n++) {
$due = $dues->find($dueid[$n+1]);
$due->update(array(
'percentage' => $post['percentage'][$n],
'amount'=>$post['pamount'][$n],
'date' => $post['date'][$n]
)
);
}

Using read() in cakephp to retrieve row with array of data

I want to know if it is possible to retrieve a row from the database using something similar to the following:
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
$this->Place->set($place);
$place_found = $this->Place->read();
}
Is there some way I can preset the data in the Place model using the array and then use Place read. I'm looking for something simple like the usual:
$this->Place->id = 7;
$place_found = $this->Place->Read();
I have also tried doing this:
$this->Place->city = blah;
$this->Place->area = foo; etc....
$place_found = $this->Place->read();
However, that also does not work.
Haven't you ever used find()?! read() only fetches a row with the ID passed.
$place_found = $this->Place->find('first', array(
'conditions' => array(
'Place.city' => $city,
'Place.area' => $area
// etc
)
));
If you need to build the conditions manually you can create a conditions array to pass like so:
$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
$placeConditions['area'] = $area;
}
$places = $this->Place->find('first', array('conditions' => $placeConditions));
I suggest you read the page I linked, you will soon find out there is never a reason to use the read() method.
In model you could do:
$this->id = 3; //place id
$this->Place->read();
Hope it helps
I think this approach is what you're looking for(with your code):
if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];
//$this->Place->set($place); //don't need it here I think
$place_found = $this->Place->find('all',array('conditions'=>$place));
}
You'll have to use "find()", not "read()", but - it's almost as simple as your example code and should work. (also, there's a shortcut for the array_push() I believe, but - I like to use this for readability - personal preference):
if (!empty($this->params['form'])) {
$conditions = array();
array_push($conditions, array('Place.city' => $this->params['form']['city']);
array_push($conditions, array('Place.state' => $this->params['form']['state']);
array_push($conditions, array('Place.country' => $this->params['form']['country']);
array_push($conditions, array('Place.iso' => $this->params['form']['iso']);
$this->Place->set($place);
$place_found = $this->Place->find('all', array('conditions'=>$conditions));
}

Perl: Create a hash from an array

If I have the following array
my #header_line = ('id', 'name', 'age');
How do I create a hash from it equivalent to the line below?
my %fields = { id => 0, name => 1, age => 2};
The reason I want to do this is so that I can use meaningful names rather than magic numbers for indexes. For example:
$row->[$fields{age}]; # rather than $row->[2]
my %fields;
#fields{#header_line} = (0 .. $#header_line);
my %fields = map { $header_line[$_] => $_ } 0..$#header_line;
You said in reply to a comment that this is coming from Text::CSV. This module has a way to import this into a hash for you.
$csv->column_names( #header_line );
$row = $csv->getline_hr( $FH );
print $row->{ 'id' };
my %fields = ();
for (my $i = 0; $i < scalar(#header_line); $i++) {
$fields{$header_line[$i]} = $i;
}
TIMTOWTDI
my %fields = ();
foreach my $field(#header_line)
{
$fields{$field} = scalar(keys(%fields));
}

Resources