Organize a groupBy within another groupBy using Collection in CakePHP - cakephp

I have the following array and I would like to be able to organize it through collection
$collection = Collection($total);
$filter = $collection->groupBy('date_general')->toArray();
[2017-11-14] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
[1] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
Up to this point I already have the array organized by dates. Now within each date I need to organize it by user, that is to say that it is:
[2017-11-14] => Array
(
[164] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 164
)
)
[112] => Array
(
[0] => Array
(
[status] => pending
[date_general] => 2017-11-14
[user_id] => 112
)
)
)

You'll have to process each date group and individually group their contents.
$collection = collection($total)
->groupBy('date_general')
->map(function ($data) {
return collection($data)->groupBy('user_id')->toArray();
});
See also
Cookbook > Collections

Related

Get data from 2 foreach array from 1st array i want to get name and from second Indexes

I have two Arrays 1st Array give me a role form Roles table and second array give me the data using this role_id key.
When i pass the array to view how can i get Role that is from first foreach().
This is my
SettingController.php
public function index()
{
$users = User::with('roles')->get()->all();
$access_names = AccessName::all();
$modules = Module::all();
$entries = Entry::all();
$general_settings = Setting::all()->first();
$module_permissions = ModulePermission::get()->toArray();
$roles = Role::get();
$num_roles = count($roles);
$data = 'roles';
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
}
}
}
return view('backend.setting.index', compact(['general_settings', 'roles', 'users', 'access_names', 'roles_data','modules','entries']));
}
My output of this code is like this
Array
(
[Admin] => Array
(
[1] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
If i can access data from this then very well other wise i make a new array like this from i can access this by a new array that is data so it is easy to check that
I also tried this but this is not working properly
foreach ($roles as $role){
foreach ($module_permissions as $permissions){
if($role['id'] == $permissions['role_id']){
$roles_data[$role['name']][] = $permissions;
$array['data'] = $roles_data;
}
}
}
But If output come like this that is also very good
Array
(
[data]=>array
(
[Admin] => Array
(
[0] => Array
(
[id] => 179
[user_id] => 1
[role_id] => 1
[access_name_id] => 2
[module_permission_id] => 1
[entry_type] => own
[status] => 0
[created_at] => 2019-08-22 04:45:14
[updated_at] => 2019-08-22 04:45:14
)
)
[User] => Array
(
[0] => Array
(
[id] => 206
[user_id] => 2
[role_id] => 2
[access_name_id] => 1
[module_permission_id] => 1
[entry_type] => own
[status] => 1
[created_at] => 2019-08-22 05:09:40
[updated_at] => 2019-08-22 05:09:40
)
)
)
)
In view i want to do like this.
view.blade.php
if($role->name == 'data['Admin']'){
//then do this
}
elseif($role->name == 'data['User']'){
// do this
}
else{
// do this
}
And i also want to this dynamic what can i do to make dynamic i not use 'admin' and 'user' because if i have many roles it is difficult to handle.
Thanks.................

Not able to access data from array retrieved in controller to display in view files

In my cakephp Controller when I retrieve my data by find clause I get this array
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 1
[Category-name] => Arts
)
[Course] => Array
(
[0] => Array
(
[id] => 1
[category_id] => 1
[degree] => UG
[course-name] => BSc-Maths
)
[1] => Array
(
[id] => 5
[category_id] => 1
[degree] => PG
[course-name] => MSc Math
)
[2] => Array
(
[id] => 6
[category_id] => 1
[degree] => UG
[course-name] => Bsc Stats
)
[3] => Array
(
[id] => 7
[category_id] => 1
[degree] => PG
[course-name] => Msc-Stats
)
)
)
[1] => Array
(
[Category] => Array
(
[id] => 2
[Category-name] => Science and technology
)
[Course] => Array
(
[0] => Array
(
[id] => 2
[category_id] => 2
[degree] => UG
[course-name] => BSc-CS
)
)
)
[2] => Array
(
[Category] => Array
(
[id] => 3
[Category-name] => Commerce
)
[Course] => Array
(
[0] => Array
(
[id] => 3
[category_id] => 3
[degree] => PG
[course-name] => Msc-Finance
)
)
)
[3] => Array
(
[Category] => Array
(
[id] => 4
[Category-name] => Law
)
)
)
I want to show All Courses for a particular Category in ctp file as in form. As For Category Arts there are 4 Courses.
I want to display these 4 Courses for Arts Category.
I am able to Display Categories using the same array.
But not able to display Courses using same array in y ctp file as dropdown in a form.
Is there a way to Access this Courses data? Or Do I have to use different query for to access Courses??
Please I need your help with this.
Thanks in advance.
I would solve it like this:
An improved retrieving Query which only outputs the course names.
$cats = $this->Category->find('all',array(
'conditions'=>array('Category.Category-name'=>'Arts')
'recursive'=>1,
'contain'=>array('Course'),
'fields'=>'Course.course-name'))[0]['Course'];
Now the $cats variable should contain an array with 4 entries which can be used in a dropdown select

cakephp 2 threaded not showing children

Hi i don't know what is wrong with the threaded of cakephp 2 please help
when i use threaded without condition it will show its children
$category = $this->Category->find('threaded');
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
[0] => Array
(
[Category] => Array
(
[id] => 113
[name] => Core samples
[parent_id] => 11
)
)
)
but when i use this with condition it won't show its parent
$this->Category->find('threaded',array('conditions'=>array('Category.id'=>11)));
output:
Array
(
[0] => Array
(
[Category] => Array
(
[id] => 11
[name] => Affinity Collection
[parent_id] => 0
[lft] => 25
[rght] => 30
[alias] => affinity-collection
[status] => 0
)
[children] => Array
(
)
)
)
please help why it is not showing the children when use with condition
I think the problem is because you are asking for all categories with an id of 11. Insted you should ask for all categories that are children of categorie 11.
You could use your left and right columns to get a nested array (keyword Tree behaviour). Try this:
$parentCategorie = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.id' => 11
)
);
$children = $this->Categorie->find('first', array(
'conditions' => array(
'Categorie.lft BETWEEN ? AND ?' => array($parentCategorie['Categorie']['lft'], $parentCategorie['Categorie'])['rght']
)
);
Probably the best chance you have to achive what you want is to use the children method of the Tree behavior.
You coul use it as follows
$parent=$this->Category->find('first',
array('conditions'=>array('Category.id'=>11)));
$this->Category->children($parent['Category']['id']);

cakephp multiple records data array fit in request data

I need to get multiple records from model, then put them into request->data for a view to render into a form with multiple input fieldsets for example name='data[Applicant][0][display_name]'. name='data[Applicant][1][display_name]'...data value goes in for each applicant.
Actually I've already done what i want, but i do not think it is a good method to do so.
Appreciate if anyone can guide me
foreach ($this->Applicant->data['Applicant'] as $key=>$item){
$data['Applicant'][] = $item['Applicant'];
}
$this->request->data = $data;//set Model to data
$this->set('data' , $this->Applicant->data);
$this->Applicant->data is the following:
Array
(
[Applicant] => Array
(
[0] => Array
(
[Applicant] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
)
[1] => Array
(
[Applicant] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
the following is the desired output (less one level):
Array
(
[Applicant] => Array
(
[0] => Array
(
[id] => 1
[application_id] => 17
[name] => User
[first_name] =>
...
)
[1] => Array
(
[id] => 3
[application_id] => 17
[name] =>
[first_name] =>
thanks
This should suffice:
$this->request->data['Applicant'] = Hash::extract( $this->Applicant->data, 'Applicant.{n}.Applicant' );

saveAll() on nested data

When I try to create a new entry of Template with the $data array, the models of Template, Group and Product are saved correctly. But the nested Calcfield (which is a hasMany of Group) is not saved. :( Group and Product are hasMany of Template.
Is this possible at all?
$this->Template->create();
$this->Template->saveAll($data, array('validate' => false));
// $data looks like this:
Array
(
[Template] => Array
(
[title] =>
[shorttitle] => Wie auch immer
[place] => Hannover
[size] =>
)
[Group] => Array
(
[0] => Array
(
[title] => Hosting
[order] =>
[Calcfield] => Array
(
[0] => Array
(
[title] => Hosting
[value] => 0
[description] =>
)
[1] => Array
(
[title] => Strom
[value] => 0
[description] =>
)
)
)
)
[Product] => Array
(
[0] => Array
(
[share] => 10
[businessunit] => Marketing
)
[1] => Array
(
[share] => 30
[businessunit] => intl. CRM
)
)
)
No, recursive saves are not possible, as far as I know. You would need to stick those in a separate array and then save them after the initial save.
Since CakePHP 2.1 it's possible to save deeply-nested models using the deep parameter in saveAll (since CakePHP 2.1)
ref. http://book.cakephp.org/2.0/en/models/saving-your-data.html?highlight=saveall#model-saveassociated

Resources