I would like to ask kindly someone to help me with this issue in my CakePHP 4.2.4 web app.
I have public function display() in my MenuCell class, which fetches data from database Menus (id, link_name, controlle, action, date, date_modified). Based on the database Table, I want to generate side navigation links in ascending order, i.e.
I paginate data from the database as follows:
public function display()
// Create a paginator
$paginator = new Paginator();
$results = $paginator->paginate($this->Menus, ['order' => ['Menus.name' => 'asc']]);
Nevertheless, result is that variable $menuitems is sorted according to the id key in database table, i.e. my Menu is ordered as follows:
I tried to add parameters into paginator,
$results = $paginator->paginate($this->Menus, [‘order’ => [‘Menus.name’ => ‘asc’]]);
I am sure that solution is trivial but I have almost no experience with OOP and can not find the solution.
Thank you in advance!
Not tested, but something like this should work:
public $paginate = [
‘Menus’ => [
‘order’ => [
‘Menus.name’ => ‘asc’],
You want to create menus that contain only a subset of the menu options?
Thank you dirk for your reply and thank you Zuluru for asking for additional information. Maybe my question will be clearer after reading my attempt to fix the issue below.
I tested to set $public variable according to dirk answer but this approach works only when I call the view using menus controller, i.e. /myapp/menus/sidemenuview/ When I try to sort items of the list in a Cell, it does not work. Here is the output as well as source code for comparison:
Here are Cell definition and MenusController
View file /templates/Menus/sidemenuview.php and /templates/cell/Menu/display.php are identical:
Maybe, its a bug or maybe parameters of the $public variable are transfered via URL somehow and this is the cause, why the sorting does not work for Cell. Please could someone chek this?
I’ll ask this again. You want to create menus that contain only a subset of the menu options that are in the database?
Thank you for your interest, Zuluru. Answer is no, the database with menu items is made manually and I want to fetch all data from second, third and fourth column. Nevertheless, I found solution, its in the cakephp Cookbook.
For everyone with similar problem, the solution is in images below (i slightly modified font in sidemenuview.php, so it looks differently, view files are attached):
MenuController and MenuCell files:
Where I was going with my question is that, if you want all the menu options to be loaded, then the paginator is not something you should be using at all. You should just do a
find and get all the results.
OK, I will try to use find() method and if I success, I will paste solution here.