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
Related
I have 5 tables:
user ( user id, user name, etc.. )
role ( role_id, role_name )
user_role ( user_id, roles_id )
form ( form_id, form_name, etc.. )
form_access ( form_id, role_id )
user contains all registered user data.
role contains all different types of roles.
user_role contains which user has which role (all assigned roles are stored)
form contains all form data.
form_access contains data like which user role has which form access(one form can be assigned to many user roles).
I wanted to write a SQL query in PHP to retrieve form name based on the user logged in and his role, e.g. if Admin logs in he should get all forms, if HR logs in he should get forms related to HR only.
I tried this query:
$query = "SELECT ur.role_id, f.form_name, f.form_desc
FROM user_role ur, froms f
WHERE users.id = '".$user."',
users.status ='A',
forms.form_id = form_access.form_id,
from_access.role_id = user_role.role_id,
user_role.role_id = '".$user."'";`
Some one help me out with the correct query?
Since you start from the user_id you'll want to select from user_role and JOIN form_access and form.
SELECT `form`.`form_name` AS `form_name`
FROM `user_role` AS `ur`
INNER JOIN `form_access` AS `fa` ON `fa`.`role_id` = `ur`.`role_id`
INNER JOIN `form` AS `f` ON `f`.`form_id` = `fa`.`form_id`
WHERE `ur`.`user_id` = '".$user."'
PS: Check the table and column names.
You have to use AND .
BUT this should be better with joins.
$query = "SELECT ur.role_id, f.form_name, f.form_desc
FROM from_access
INNER JOIN froms f ON forms.form_id = form_access.form_id
INNER JOIN user_role ur ON from_access.role_id = user_role.role_id
INNER JOIN users ON users.id = user_role.role_id
WHERE users.id = '".$user."'
AND users.status ='A' ";
Your query is entirely broken, the others may have provided you with solutions but I'm going to give you some advice.
You've written an entire query, tried it, and it failed. I write queries all day long but if I write a whole query in Notepad then execute it it's probably going to have some minor error in it somewhere too.
Start from the ground up. You're trying to get a list of forms the user has access to, so lets start with the forms_access table. So what's the most basic starting point? How about:
SELECT fa.role_id, fa.form_id
FROM forms_access fa
Ok, thats overly simplified but if that ran at least we know we're connected to the database.
So we can easily tell which form_ids each role has access to. Now we know our linking table to users is user_roles, so let's add that in:
SELECT ur.user_id, fa.form_id, fa.role_id
FROM forms_access fa
INNER JOIN user_roles ur ON fa.role_id = ur.role_id
So we've joined forms_access to user_roles on the foreign key role_id. Now we can see for every user_id, which role_id they have and which form_ids they can access.
So we're pretty much there, we just need the information from the forms table, so lets JOIN to that too:
SELECT ur.user_id, f.form_name, f.form_desc, fa.form_id, fa.role_id
FROM forms_access fa
INNER JOIN user_roles ur ON ur.role_id = fa.role_id
INNER JOIN forms f ON f.form_id = ur.form_id
Great! Now we have a list of each form_name/form_desc that each user_id can access.
Try the above step by step, if you skip to the end there could well be an error since I have not tested the code, and I don't know for sure that your table definitions match the question. If you do it step by step you only need to check the most recently added line to find the error.
I have just noticed in the question that you also need users.status = 'A', so in the same way as above you'll need to join to the users table on an appropriate foreign key, give it a go.
Now, once you've done all that you need to filter the results to a specific user_id - notice up till this point we haven't bothered with the WHERE clause.
Now don't go adding some variant of WHERE user_id = '$user' right away because then you've introduced 2 potential errors. Instead try adding WHERE user_id = 0 (or some known user_id). Does the query run and the results look correct? Great, now finally try adding in your php variable.
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.
I have a form set up where the user can enter in the username or lastname of a person. I then take either the username or lastname depending on what they entered and query the database and displaying all the userinformation and whether they have access to certain areas. There are two tables I need to query and get all the information for the user entered. I'm assuming you use a JOIN but I'm not quite sure how to do it. Right now I have the query to grab all the data from the users table I just need help adding in the second table. The two tables look like this
Users {
uid
username
password
firstname
lastname
email
}
userAccess {
uid
assessment
development
learningmodule
project
}
The SQL I have so far is
SELECT *
FROM Users
WHERE username = '$username' OR lastname = '$lastname'
You are right you'll need to use a join. This type of join is called an Inner Join. You can achieve it like this:
SELECT * from Users, userAccess WHERE Users.uid = userAccess.uid AND Users.uid = #;
I'm guessing you'll have an id column that is the Primary Key for your userAccess table as well. The above query will provide the data from both tables in 1 record for every condition where the above statement is true.
If you are using MySQL you can read more about joins here:
http://dev.mysql.com/doc/refman/5.0/en/join.html
You donĀ“t mention which database you are using, although I'm guessing MySql.
What you need to do is a simple inner join:
SELECT u.uid
u.username,
u.password,
u.firstname,
u.lastname,
u.email,
ua.assessment,
ua.development,
ua.learningmodule,
ua.project
FROM Users u
INNER JOIN UserAccess ua
ON u.uid = ua.uid
WHERE u.firstname LIKE '%$username%' OR u.lastname LIKE '%$lastname%';
Do note that I'm not selecting the uid again in the UserAccess table as it is redundant since you already selected it from the Users table and since you are joining by that key you don't need it again.
Also note that instead of using the equal operator (=) I use the LIKE operator since there are more chances that you want to filter information based on partial info such as keywords and not full words.
I'd recommend you to have a look at a SQL tutorial for beginners. This one seems to be good enough.
I'd also recommend you to read the guidelines on how to post questions here or else you won't get much attention from anybody, plus they will downvote you :)
I have two SQL tables. The first one structure is simple.
ID Name
----------
The second one is simple too.
CommentID Comment CreatorID
----------------------------------
I want to show in my site all the comments that correspond with the "ID of user"
The problem is we have here a lot of ID and two different tables.
Something like this:
$1= $bdd->query("SELECT * FROM comments WHERE id_user=220281");
$2=$1->fetch();
But its impossible because id user is not on the comments table.
The most simple way to do this is to join the tables like this:
select
users.name,
comms.commentID,
comms.comment
from
userTable users
join commentTable comms
on users.ID=comms.ownersID
where
users.id=1
This will return the users name in each row of data, but you don't need to use it in the output more than once.
It also sounds like you could use a few pointers on SQL queries. Do yourself a favour and have a read of this article I put together a while back.
SELECT c.*
FROM comments c
INNER JOIN users u ON c.id_creator = u.id_user AND
u.id_user = 220281
A simple join will do the trick like this :
SELECT c.comment, u.user_name FROM
Users u
JOIN
Comments c ON
c.creator_id = u.user_id
WHERE
u.user_id=220281
fiddle:http://sqlfiddle.com/#!6/3b28a/1
for the sake of example:
i have the following schema:
users(id, name)
groups(id,name)
user_groups(user_id, group_id)
i want to fetch users that belong to a certain group. problem is, i know the group id, so i don't want to join the groups table - user_groups is sufficient.
the result should be
SELECT * FROM `users` AS u
LEFT JOIN `user_groups` AS ug ON (ug.`user_id` = u.`id)
WHERE ug.`group_id` = X
// i know the value of X
my attempts in doctrine2 resulted in another join of the groups table.
the closest thing i got is:
SELECT u FROM models\User u WHERE ?1 MEMBER OF u.groups
but it will also LEFT JOIN it inside the "WHERE EXISTS(...)"
can this be done without native query (using DQL/query builder)?
One of the advantages of ORM over ActiveRecord pattern is that, you don't need to create a new entity for join tables.
So here when you use Doctrine, the user_groups table will be mapped on both sides.
That means you cannot access the user_groups table directly. Unless you use native sql, which i strongly advice against.
So the best way to tackle your situation is to use findOneBy($group_id) on group repo and get the users from it. That the theoretically correct way to implement it.
-- EDIT --
For your comment:
yes, i agree that from the pure db POV its more efficient.
But you won't be able to do it without native query as DQL, query builder and everything related to doctrine uses Entities to perform action and since you are using an ORM by design, you won't have an entity for Join tables.
Else the only way is to change your mapping from a many to many relation from user to groups using join table to a one-to-many from users to user_groups and a many-to-one from user_groups to groups. That way you will have an entity for user_groups.
Then you can directly use that entity to get the users.
Even though its a hack, its not technically & theoretically correct to use like that. Unless of course the join table has other responsibilities also.