Managing a 2 way relationship in CakePHP without retrieving data twice - php

I am having troubling linking some of my models together. Users have one of three roles:
Student
Lecturer
Admin
Information that is shared between the 3 roles is stored in a User table. When a User wants to change their role they make a role request that must be accepted by Admin.
User has one Student
User has one Lecturer
User has one Admin
User has one RoleRequest
Student belongs to User
Lecturer belongs to User
Admin belongs to User
What I am struggling with is that I cannot find a way of retrieving the data of a user without retrieving their role or their user data twice. If I retrieve the User object with recursive set to 2 I get the user data twice as it is also inside the Lecturer object. If I do the same with the Lecturer object I get the lecturer data twice as it is also inside the User object.

Don't use recursive. Set public $recursive=-1; in your AppModel, then use CakePHP's AMAZING ContainableBehavior to retrieve whatever data you'd like.

If I understood your question correctly, I'm guessing you're not using Contaible.
With that behaviour, you can do something like
$this->User->find('all', array('contain'=>'Lecturer'));
and that will retrieve an array similar to
[User] => array(/*user data*/),
[Lecturer] => array(/*Lecturer data*/)
Just remember to define your models as containable.

Related

Showing multiple roles assigned to a user in Laravel using Spatie Permissions

does anyone know how to show multiple roles that are under a user? I have setup my permissions so a user can be under multiple roles, which is set easily enough using the following
$user->assignRole(['Root', 'IT', 'HR']); // assigning roles
I am however having trouble showing all roles a user relongs to in the same way?
Has anyone done this and knows how to? I simply want to show on a page which role a user belongs to.
Refer to this link
https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage
Get the names of the user's roles
$roles = $user->getRoleNames();
Returns a collection
if there are two tables (roles and user_roles) than you can check through inner join ('inner join' to check role must exist in 'roles' table that is assigned to a user) by passing user_id (user_id to get specific roles assigned by a user).
You can get a collection of roles assigned to the user via $user->roles

How to restructure relationship data?

I am new to Laravel 5.3 so bear with me ;)
I am using Laratrust for permission and roles, http://laratrust.readthedocs.io/en/3.0/configuration/models/user.html
I can do:
User::with('roles')->findOrFail( $id );
and get the user with the roles, but how can I get the user with a restructured variant of roles. Instead of the array with roles I want an array with a simplified variant of Role
Thank you in advance!
I think you're trying to find out what roles a user with a specific id has.
Given id is the id of the given user try this -
User::find($id)->roles()
This will give you a list of roles the user with the given id has.

How to retrieve data from additional columns in a pivot table and also be able to update it in laravel 5.2

I was working on making a group functionality for my website which uses a many to many relationship between groups and users.
My User model looks like this:
public function groups(){
return $this->belongsToMany('App\Group')->withPivot('role')->withTimestamps();
}
My Groups model looks like this:
public function users(){
return $this->belongsToMany('App\User')->withPivot('role')->withTimestamps();
}
So my third column has the name of role which is a string variable and is set to a default of "member" for members of my group and I set it to "admin" for the actual user who creates a new group. But I want the admin to have the option of making multiple members admins as well which would require me to check weather the current current user who sent the request is an admin or not. If he is, then I wanna be able to take his request of making a member an admin which would require me to update the role for that particular "member" to an "admin".
In the laravel documentation it only shows you how to attach and detach data in a pivot table and else where I have only seen methods of retrieving data from the first two columns but how can I do the same for additional columns and also be able to update it using the updateExistingPivot method?
You could access the column simply using pivot e.g :
$user->pivot->role
Take a look at Retrieving Intermediate Table Columns in documentation Eloquent Relationships.
Hope this helps.

add model relationship without save

I have the next relationship User hasMany Contacts.
In normal situation is use $user->contacts()->save($contact) to add and save a contact to the user, but I need to associate contacts to users without save the models (User and Contacts).
edit:
I need to build a plant that receives one of such methods and return an XML a collection of templates, only in some of these models will be stored at postiriori.
You can use the associate on your model with belongsTo.
$contact= Contact::find(10);
$user->account()->associate($contact);
$user->save(); // You do need to update your user
Source: http://laravel.com/docs/eloquent
If you know the UserID, just create a new Contact; and make sure to have the correct User ID in the user_id column. Don't even have to touch the User.

Create user roles and profiles in Codeigniter using Ion Auth

I'm using Codeigniter with Ion Auth for user management. I'd like to create two different user roles - one for volunteers and one for organizations. I know that I can use Ion Auth's groups for things like access control, but what would be a good way to add fields to the different user types (for instance - the volunteer user will have a 'languages spoken' field while the organization will have a 'mission' field)? Should I extend the Ion Auth class for each new user type and handle CRUD seperately for each type, or use the 'groups' field and the user id to reference the fields in another table? Any insight as to an approach to this common problem?
I would recommend just adding all the fields you need into the meta table and only updating the ones you need per user group. If you need multiple groups per user check out Ion Auth v2.
I had the same problem before, what I ended up doing was building relation tables to handle different groups with different fields. Then I modified the profile() code a bit, to join the additional relation table according to the user's group settings.
I would start off building a relational database.
example:
**volunteers table**
id
user_id
languages
**organizations table**
id
user_id
mission
Then depending on user group, join the table in profile() function.
Hope this helps!

Categories