Laravel/ui auth custom parameters - php

How can i set custom Auth::user()->***?
I have 2 tables, table 'users' and table 'roles',
When it executes the SQL it returns the parameters, but i want to add custom parameters with INNER JOIN from the table 'roles' so i can have Auth::user()->role, Auth::user()->prefix, etc...
So, more simply:
I have table 'users' and table 'roles'
Laravel auth only takes parameters from the users table
I want that it uses a INNER JOIN so it takes parameters from roles table too
I tried a SQL like this (SELECT users.*, roles.prefix, roles.color FROM users INNER JOIN roles ON users.roleId = roles.roleId WHERE users.id = 1;) and works, but how can i do the same thing with laravel auth?

Related

Simple doctrine query using symfony

I'm trying to build a basic query using symfony and doctrine. The query will return a User and all the jobs they are working on. From the two tables 'User' and UserDetails (Contains User_id and Job_id). User is mapped to userdetails correctly as one- many.
my query is
SELECT userdetails, u FROM TestBundle:User
join userdetails.u
As user is a field in userdetails, but userdetails isn't a member of users the following query doesn't work. Is there any way to write this so the result will look like User.userDetails.
Try something like
SELECT
u, ud
FROM
TestBundle:User u
JOIN //LEFT JOIN if you want also users without UserDetails
TestBundle:UserDetails ud
WITH
u.id = ud.user_id
of course your variables (like ud.user_id) could vary but we don't have enough informations to work on so we have to guess

Outputting all data in a table and showing which rows belong to a user

I have a roles table which contains various user roles.
A pivot tables joins this to my users table.
users
id | name
roles
id | title
user_role
user_id | role_id
I would like to display all roles that exist and pre tick the ones that belong to a specific user.
How would I go about this, I take it I cannot do this in mySQL?
I was considering getting all roles with one query, then with another query get all roles that belong to a specific user.
Then loop through all roles and if there is a match with the specific users roles, output a checked box instead of an unchecked one.
Is there a better way?
Something like
SELECT R.id as role_id,
R.title as role_title,
UR.user_id as user_id,
U.name as user_name
FROM Roles R
LEFT JOIN user_role UR
ON UR.role_id = R.id
AND UR.user_id = :myuserid
LEFT JOIN users U
ON U.id = UR.user_id
should return a complete list of roles, with either a NULL or the user id in the user column to indicate any roles that the user has been granted (user identified by id as :myuserid)
I think normal SQL Query can do most of the work: Checkout this SQLFIddle for sample of what your query might look like
SELECT r.* FROM roles r LEFT JOIN user_role ur ON r.id=ur.role_id WHERE ur.user_id=3;

Query in ManyToMany Relation Doctrine2

This is driving me crazy.
I have a normal Symfony2 Security System, with User and Role entities with a ManyToMany relation between them.
Lets suppose that i have 3 roles in the database, ROLE_1, ROLE_2 and ROLE_3.
How can i retrieve all Users that dont have ROLE_3 for example ?
I already tried something like:
$qb->innerJoin('u.roles', 'r , 'WITH', $qb->expr()->notIn('r.id', ':roles')))
->setParameter('roles', array(3));
My Question is, how can i Query in the JoinTable ?
This is more of a SQL question than it is a Symfony or Doctrine question. The first solution that comes to mind is a subquery, although that may or may not be the most efficient way to handle this problem. Something like this:
SELECT username
FROM users
WHERE users.id NOT IN (
SELECT users.id
FROM users
INNER JOIN user_roles
ON users.id = user_roles.user_id
INNER JOIN roles
ON user_roles.role_id = roles.id
WHERE roles.id NOT IN (1,2)
);
Where 1 and 2 are the IDs of the role you want to include in your results, and user_roles is the bridge table between users and roles.

Is there a smarter way to load many-to-many relationships with PHP and MySQL?

Say you have Users and Roles in a many-to-many relationship. Standard setup with following tables:
user role user_role
user_id role_id user_id
email name role_id
Preferably with as few queries as possible, what's a good way to load all users with all tags belonging to them? That is, I'd like to end up with users, each having an array of roles.
I'm currently thinking of using the following, which'll requires two queries.
Get all users with group concatenated role ids.
Get all role names in separate query
Loop through users, explode role ids and connect them with names from role query as we go.
Any good/better/more efficient/cleaner/quicker alternatives? Any good way to do this in a single query?
To return all roles for all users
<?php
$array=array();
$sql='SELECT user.email,role.name
FROM user
INNER JOIN user_role ON user_role.user_id=user.user_id
INNER JOIN role ON user_role.role_id=role.role_id';
$stmt = db::db()->query($sql);
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
if (!isset($array[$row['email']])){$array[$row['email']]=array();}
array[$row['email']][]=$row['name'];
}
?>
To return all roles for a single user
<?php
$sql='SELECT user.email,role.name
FROM user
INNER JOIN user_role ON user_role.user_id=user.user_id
INNER JOIN role ON user_role.role_id=role.role_id
WHERE user.email=?';
$stmt = db::db()->prepare($sql);
$stmt->execute(array('anEmail#domain.com'));
$roles=$stmt->fetchAll(PDO::FETCH_ASSOC);
?>
The above assumes that all users have at least one role. If not, use outer joins.

Getting users based on group, but users, user_groups and groups kept in separate tables

I'm assuming I need to use JOIN, but I'm not totally getting it. Here's my situation. I'm using ion_auth. It stores users in 'users', groups in 'groups' and stores users' group in 'users_groups'.
So if I have a group 'sales' with an id of '2', any user associated with that group appears in 'users_groups' as:
id user_id group_id
1 1 2
1 2 2
So how can I quickly return the results of a query like: get users from group 2. Would the JOIN command be what I'm looking for? I'm using CodeIgniter's Active Record but am open to any suggestions.
It's a very basic join between two tables
SELECT users.* FROM users JOIN users_groups ON (users.id=users_groups.user_id) WHERE users_groups.group_id = 2
SELECT list_of_fields FROM users JOIN users_group USING (group_id) WHERE users.group_id = 2
Assuming you used group_id as the field name on both tables

Categories