Why won't CakePHP 3.2 Paginator sort on my query results? - sorting

CakePHP query and such from the controller:
$ratings = $this->Ratings->find('all')->group('manufacturer');
$ratings->select(['manufacturer',
'count' => $ratings->func()->count('*'),
'max' => $ratings->func()->max('psi_score'),
'min' => $ratings->func()->min('psi_score')]);
$ratings = $this->paginate($ratings);
$this->set(compact('ratings'));
$this->set('_serialize', ['ratings']);
Here is what debug($ratings) produces:
'items' => [
(int) 0 => object(Cake\ORM\Entity) {
'manufacturer' => 'A10Green Technology',
'count' => (int) 8,
'max' => '7.41',
'min' => '6.57',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Ratings'
},
In the view, sorting on manufacturer works fine. That is an original db field. The count, max and min values are not getting sorted.
Here are the sort calls for the view:
<th><?= $this->Paginator->sort('manufacturer')?></th>
<th><?= $this->Paginator->sort('count')?></th>
<th><?= $this->Paginator->sort('max')?></th>
<th><?= $this->Paginator->sort('max')?></th>

You need to whitelist the fields that you want to allow to be sorted like in: Pagination
public $paginate = [
'sortWhitelist' => [
'id', 'title', 'Users.username', 'created'
]
];
Otherwise by default cake 3 blocks it

Related

Best way to update a lot of data with difference value?

I have a lot of data and each record have difference value. I try to loop insert/update data but it take time. Is it have better way to improve my code?
I don't want to delete all data before update.
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
]
foreach ($data as $item) {
DB::table('my_table')->updateOrInsert(['id' => $item->id], $item);
}
Thank.
You can use upsert in Laravel 8.x or use this package: https://github.com/staudenmeir/laravel-upsert to use INSERT ON DUPLICATE
$data = [
[ 'id' => 1, 'name' => 'A' ],
[ 'id' => 2, 'name' => 'B' ],
[ 'id' => 3, 'name' => 'C' ],
[ 'id' => 4, 'name' => 'D' ],
.
.
.
[ 'id' => 10000, 'name' => 'ZYX' ],
];
DB::table('my_table')->upsert($data, 'id');
use laravel upsert method here
for example you want to update if exists or insert record if not exists
$data = [
[
'id' => 1
'name' => 'J.K. Rowling',
'author' => 'Harry Potter',
'quantity' => 15
],
[
'id' => 2
'name' => 'Cal Newport',
'author' => 'Deep Work',
'quantity' => 20
]
];
DB::table('books')->upsert($data, ['id'], ['quantity']);
1.The first argument consists of the values to insert or update.
2.The second argument lists the column(s) that uniquely identify records (id in our case) within the associated table.
3.The third and final argument is an array of columns that should be updated if a matching record already exists in the database. We want the quantity to be updated in our case.
for more detail read official docs upsert

If condition with array in Laravel

I'm trying to create an array to convert JSON. My data is queried from database.
My problem is I have to check condition for array. If $item->verified == 1, my 'isVerified'
will be true, my email will be in verified and opposite.
Here is what I did, I check condition and create 2 array for it. Can I just use 1 array
for condition:
if( ($item->verified) == 1)
{
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => true,
'email' => [
'verified' => $item->email,
'unverified' => []
]
];
}
else
{
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => false,
'email' => [
'verified' => [],
'unverified' => $item->email
]
];
}
You can use ternary operator.
$data[] = [
'name' => $item->fullname,
'address' => $item->address,
'isVerified' => $item->verified == 1,
'email' => [
'verified' => $item->verified == 1 ? $item->email : [],
'unverified' => $item->verified == 0 ? $item->email : [],
]
];

Search in Laravel Datatables

In my datatables, the search function works only on the name column. The name column ist different that the others. It means, the name column gives the value of the database back, while the others gives a value back with the function add column
In the column array there are the following values:
ยดยดยด
protected function getColumns()
{
return [
'status',
'name' => [
'title' => 'Name',
'orderable' => true,
'searchable' => true
],
'location' => [
'title' => 'Standort',
],
'department' => [
'title' => 'Abteilung',
'orderable' => true,
'searchable' => true
],
'division' => [
'title' => 'Bereich',
'orderable' => true,
'searchable' => true
],
'leader' => [
'title' => 'Verantwortlicher',
'orderable' => true,
'searchable' => true
],
'start_date' => [
'title' => 'Startdatum',
'searchable'=> true,
],
'end_date' => [
'title' => 'Enddatum',
'searchable'=> true
]
];
}
```
Why it doesn't search on all columns? What i have to do?
Try to search by using this code.
return Datatables::of($tasks)
->filter(function ($query) use ($request) {
$title = $request->title;
if (isset($title) && !empty($title)) {
$query->where('title', 'like', '%'.$title.'%');
}
})->make(true);
OR
return Datatables::of($tasks)
->filterColumn('title', function($query, $value) {
$query->whereRaw("title like ?", ["%{$value}%"]);
})->make(true);

How to Combine Aggregate + Lookup and Search in Laravel

I Have the following Aggregate + Lookup Query
Aggregate + lookup Query
$collection = Dapil_Kotakab::raw(function ($collection) use ($page, $perPage) {
return $collection->aggregate([
[
'$lookup' => [
'as' => 'KecDetails',
'from' => 'src_kecamatan',
'foreignField' => 'id',
'localField' => 'idKecamatan'
]
],
[
'$lookup' => [
'as' => 'KotDetails',
'from' => 'src_kota_kabupaten',
'foreignField' => 'code',
'localField' => 'idKota'
]
],
[
'$lookup' => [
'as' => 'ProvDetails',
'from' => 'src_provinsi',
'foreignField' => 'idProv',
'localField' => 'idProvinsi'
]
],
['$skip' => ($page - 1) * $perPage],
['$limit' => $perPage],
]);
});
And I have the following $Search code.
$collection = Dapil_Kotakab::whereRaw(array('$text'=>array('$search'=> "\"".$word."\"" )))->skip(($page - 1)*$perPage)->limit($perPage)->get();
I have tried several times, But I keep getting error.
Any suggestions in this regard will be appreciated.

How to remove element from multidimensional cakephp session

I am working on eCommerce website where is stored all cart product in session that is working perfectly.
Here are debug of cart session.
debug($this->request->getsession()->read('cart'));
[
(int) 1 => [
(int) 0 => [
'id' => (int) 1,
'picture' => '1_1.webp',
'sku' => 'TH447WA38OUMINDFAS',
'name' => 'The Vanca Multicoloured Printed Strappy Top',
'size' => 'S',
'price' => '480'
]
],
(int) 2 => [
(int) 0 => [
'id' => (int) 2,
'picture' => '2_1.webp',
'sku' => 'AL384WA86QOSINDFAS',
'name' => 'All About You Pink Embroidered Blouse',
'size' => 'S',
'price' => '1330'
]
],
(int) 3 => [
(int) 0 => [
'id' => (int) 3,
'picture' => '3_1.webp',
'sku' => 'RE367WA35NDKINDFAS',
'name' => 'Renka Comfortable Black Color Seamless Summer Tops For Women',
'size' => 'S',
'price' => '495'
]
]
]
Now i want to remove any row from cart but that is not working for me.
unset($this->request->getsession()->read('cart')[1]);
should be simply
$this->request->getSession()->delete('cart.1');
you can use dot notation when accessing session arrays
you could also read and delete the data in one command
$cart = $this->request->getSession()->consume('cart');
see the manual here and the API here and here

Resources