cakephp 3 paginator doesn't work with a specific query [duplicate] - cakephp

Products belongsToMany Categories and Categories hasMany Products, inside my Product view I'm showing a list of all it's categories but I want to paginate or limit these results.
My current code on ProductsController is:
$product = $this->Products
->findBySlug($slug_prod)
->contain(['Metas', 'Attachments', 'Categories'])
->first();
$this->set(compact('product'));
I know I need to set $this->paginate() to paginate something but I can't get it working to paginate the categories inside the product. I hope you guys can understand me.
UPDATE: Currently I have this going on:
$product = $this->Products->findBySlug($slug_prod)->contain([
'Metas',
'Attachments',
'Categories' => [
'sort' => ['Categories.title' => 'ASC'],
'queryBuilder' => function ($q) {
return $q->order(['Categories.title' => 'ASC'])->limit(6);
}
]
])->first();
The limit works but I don't know how to paginate yet

The paginator doesn't support paginating associations, you'll have to read the associated records manually in a separate query, and paginate that one, something along the lines of this:
$product = $this->Products
->findBySlug($slug_prod)
->contain(['Metas', 'Attachments'])
->first();
$categoriesQuery = $this->Products->Categories
->find()
->innerJoinWith('Products', function (\Cake\ORM\Query $query) use ($product) {
return $query->where([
'Products.id' => $product->id,
]);
})
->group('Categories.id');
$paginationOptions = [
'limit' => 6,
'order' => [
'Categories.title' => 'ASC'
]
];
$categories = $this->paginate($categoriesQuery, $paginationOptions);
$this->set(compact('product', 'categories'));
Then in your view template you can display your $product and separately paginate $categories as usual.
See also
Cookbook > Controllers > Components > Pagination
Cookbook > Views > Helper> Paginator
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data

Related

How to paginate related articles in cakephp 4 [duplicate]

Products belongsToMany Categories and Categories hasMany Products, inside my Product view I'm showing a list of all it's categories but I want to paginate or limit these results.
My current code on ProductsController is:
$product = $this->Products
->findBySlug($slug_prod)
->contain(['Metas', 'Attachments', 'Categories'])
->first();
$this->set(compact('product'));
I know I need to set $this->paginate() to paginate something but I can't get it working to paginate the categories inside the product. I hope you guys can understand me.
UPDATE: Currently I have this going on:
$product = $this->Products->findBySlug($slug_prod)->contain([
'Metas',
'Attachments',
'Categories' => [
'sort' => ['Categories.title' => 'ASC'],
'queryBuilder' => function ($q) {
return $q->order(['Categories.title' => 'ASC'])->limit(6);
}
]
])->first();
The limit works but I don't know how to paginate yet
The paginator doesn't support paginating associations, you'll have to read the associated records manually in a separate query, and paginate that one, something along the lines of this:
$product = $this->Products
->findBySlug($slug_prod)
->contain(['Metas', 'Attachments'])
->first();
$categoriesQuery = $this->Products->Categories
->find()
->innerJoinWith('Products', function (\Cake\ORM\Query $query) use ($product) {
return $query->where([
'Products.id' => $product->id,
]);
})
->group('Categories.id');
$paginationOptions = [
'limit' => 6,
'order' => [
'Categories.title' => 'ASC'
]
];
$categories = $this->paginate($categoriesQuery, $paginationOptions);
$this->set(compact('product', 'categories'));
Then in your view template you can display your $product and separately paginate $categories as usual.
See also
Cookbook > Controllers > Components > Pagination
Cookbook > Views > Helper> Paginator
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data

Retrieve data from associated database in cakephp

I have an Entries table with users_id as a foreign key to the Users table.
I have set the belongsTo association in the EntriesTable like so:
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
After that I wrote this in the EntriesController:
$entries = TableRegistry::get('Entries');
$query = $entries->findByTitle($title)
->contain(['Users']);
$entry = $query->first();
$this->set('entry', $entry);
In the VIEW template of the Entries I have to show the username field of the user that wrote the Entry.
I am aware that in previous versions of CakePHP 1, I could just write $entry['User']['username'] to retrieve the username of the User who wrote the Entry. Now that I am using CakePHP 3.6, it doesn't seem to work. How do I perform the same task in CakePHP 3?
You can write like below
In controller
public function view($title)
{
$entry = $this->Entries->findByTitle($title)->contain(['Users']);
$entry = $entry->first();
$this->set('entry', $entry);
}
OR
public function view($title)
{
$query = $this->Entries->find('all', [
'where' => ['Entries.title' => $title],
'contain' => ['Users']
]);
$entry = $query->first();
$this->set('entry', $entry);
}
In view
<?= $entry->user->username ?>
So in cakephp you were loading Model of its controller, which is done as default,you can directly create an find() method in same controller,if you want to find from other table/ model you can do is-
a. $this->Entries->otherTableName()->find()->all();
b. $tableName= $this->loadModel('tableName');
$tableName->find();

CakePHP 3 Pagination

I am trying to make a pagination for my threads.
The code works, but I want to limit the posts per page.
How can I achieve this?
And how to draw the pagination to my threads view?
My code:
$this->paginate = [
'contain' => 'posts'
];
$query = $this->Threads->find('all')->where(['id' => $id]);
$this->set('test', $this->paginate($query));
Thanks.
Have you tried passing a limit option to $this->paginate?
$this->paginate = [
'contain' => ['Posts'],
'limit' => 10 // limits the rows per page
];
https://book.cakephp.org/3.0/en/controllers/components/pagination.html

CakePHP 3.4 group by foreach loop the result

I am struggling with foreaching grouped records.
It is a collection of hours saved with the same date, and I want to group them in my view/output.
Any suggestions? I have tried a ton of different approaches. Here is how I get them from DB. (date = type date in mysql "Y-m-d"). And outputting them normal. All my trials and errors.. I could write all day, and I am sure it is a simple solution for someone that know how cake libs work.
$hours = $this->UserHours->find('all', [
'conditions' => [
'UserHours.user_id' => $this->Auth->user('id'),
'month(UserHours.date)' => $this->request->data('date.month')
],
'order' => ['UserHours.date ASC'],
'group' => ['UserHours.date']
]);
And the foreach loop:
foreach ($projecthours as $hour):
debug($hour);
?>
<tr>
<td> <?php echo $hour->date;?> </td>
<td> <?php echo $hour->start; ?> </td>
<td> <?php echo $hour->end; ?> </td>
</tr>
My problem is that I have no idea how to loop trough the records correctly when grouped from controller. I cant find them when using the "group" condition in controller. I only get the first record of each date when using "group".
:) I guess they are somewhere inside there, but I cant figure this one out. I am quite new to cake 3.4..
After you get the grouped dates, you get all dates matching each grouped date.
// get grouped dates
$dates = $this->UserHours->find('all', [
'conditions' => [
'UserHours.user_id' => $this->Auth->user('id'),
'month(UserHours.date)' => $this->request->data('date.month')
],
'order' => ['UserHours.date ASC'],
'group' => ['UserHours.date']
]);
$hours = [];
// find all dates for each grouped date
foreach ($dates as $date) {
$hours[] = $this->UserHours->find('all', [
'conditions' => [
'UserHours.user_id' => $this->Auth->user('id'),
'month(UserHours.date)' => $this->request->data('date.month')
],
'order' => ['UserHours.date ASC']
])
->where(['UserHours.date' => $date->date]);
}
foreach ($hours as $values) {
foreach ($values as $hour) {
dd($hour->date);
}
}
That's the expected result, as that is how SQL grouping works, you get one value per group, there simply will be no further values in the results.
If you just want to output things in a grouped manner, then that's something different, for that you have to select all data, and then you could simply group the results on PHP level, for example using the collection/resultset method groupBy():
$query = $this->UserHours->find('all', [
'conditions' => [
'UserHours.user_id' => $this->Auth->user('id'),
'month(UserHours.date)' => $this->request->data('date.month')
],
'order' => ['UserHours.date ASC']
]);
// callback usage required as `date` is most likely an object
$groupedByDate = $query->all()->groupBy(function ($row) {
return $row['date']->format('Y-m-d');
});
// if it weren't an object, then passing the fields name would already do it
// $groupedByDate = $query->all()->groupBy('date');
foreach ($groupedByDate as $date => $group)
{
// $date = the value of UserHours.date shared by all entities in the group
// $group = an array of UserHour entities that share the same UserHours.date value
foreach ($group as $hour)
{
// ...
}
}
See also
Cookbook > Database Access & ORM > Query Builder > Queries Are Collection Objects
Cookbook > Collections > Collection::groupBy()

CakePHP 3 Sort by associated model with HasOne relationship

Order HasOne Suborder
Suborder BelongsTo Order
I need to sort Orders by a field in Suborders, but sorting by virtual fields appears to have been removed in Cake 3.x
In OrdersTable.php, I have
$this->hasOne('Suborder', [
'className' => 'Suborders',
'foreignKey' => 'order_id',
'strategy' => 'select',
'conditions' => function ($exp, $query) {
return $exp->add(['Suborder.id' => $query
->connection()
->newQuery()
->select(['SSO.id'])
->from(['SSO' => 'suborders'])
->where([
'Suborder.order_id = SSO.order_id',
'SSO.suborder_type_id in' => [1, 2, 3]
])
->order(['SSO.id' => 'DESC'])
->limit(1)]);
}
]);
In OrdersController.php, I have
$this->paginate = [
'limit' => 20,
'order' => ['id' => 'desc'],
'sortWhitelist' => [
'id',
'title',
'client_order',
'substatus',
'Workflows.order_status_id',
'Clients.name',
'ProductTypes.type',
'Suborder.due_date',
'Suborder.created',
],
];
$orders = $this->paginate($collection);
In index.ctp, I have
$this->Paginator->sort('Suborder.created', 'Order Placed'),
$this->Paginator->sort('Suborder.due_date'),
and the error I'm getting is Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Suborder.created' in 'order clause'. How do I get Cake to include the Suborder in the initial query for sorting and pagination?
Edit:
$collection = $this->Orders->find()
->contain([
'Clients',
'CurrentAssignment.Users',
'Workflows.OrderStatuses.Category',
'Workflows.OrderStatuses.Departments' => function ($q) use ($uID) {
return $this->Departments->find()->matching('Users', function ($q) use ($uID) {
return $q->where(['Users.id' => $uID]);
});
},
'ClientProducts.ProductTypes',
'Reviews' => function ($q) {
return $q->where(['review_type_id is not' => 6]);
},
'Reviews.ReviewTypes',
'PublicNotes',
'ActiveReview',
'Suborder',
'Suborder.SuborderTypes',
'Suborders.SuborderTypes',
]);
and $collection is modified with 150 lines of wheres, orWheres, and joins based on a number of conditions.
You have configured the assocaition to use the select strategy, which will use a separate query to retrieve the data (currently wrongly documented), hence you cannot reference it in the main query used for pagination.
So you have to use the default join strategy instead if you want to sort on it.
See also
Cookbook > Database Access & ORM > Associations - Linking Tables Together > HasOne Associations

Resources