Using nested queries and many to many relationships in Doctrine's QueryBuilder - php

So I'm having a bit of trouble thinking of how to approach this using a query builder. Currently, I have three objects that are the following:
HelpRequest
id
...
status
Filter
id
name
statuses -> ManyToMany(targetEntity="Status")
Status
id
name
A filter can have multiple statuses so there is a table that is keeping track what statuses are part of a specific filter.
Sample Data
help_requests
---
| id | content | status |
| 1 | hello | 3 |
filters
---
| id | name |
| 1 | Active |
| 1 | Inactive |
statuses
---
| id | name |
| 1 | Open |
| 2 | Closed |
| 3 | Waiting User Response |
status_filter
---
| status_id | filter_id |
| 1 | 1 |
| 3 | 1 |
| 2 | 2 |
The status_filter table is automatically generated from a ManyToMany relationship in doctrine between a Status object and a Filter object.
Based on the given information, I've written this SQL query but now I'm having troubles writing this with QueryBuilder.
SELECT * FROM help_requests WHERE status IN (SELECT status_id FROM status_filter WHERE filter_id = 1)
If there's any more information I can give, let me know. I've read multiple questions on SO and have tried a number of things but I can't seem to get it right. I'm aware I could just hard coded that query but I'd like the experience using QueryBuilder
Thanks for the help!
Update
In the end, since I couldn't get it to work with QueryBuilder and I didn't want to create a new entity solely to map two other entities together, I decided to use createQuery() instead and this is what I came up with:
SELECT
hr
FROM
HelpRequest hr
WHERE
hr.status
IN (
SELECT
s.id
FROM
Filter f
JOIN
f.statuses s
WHERE
f.name = :name
)
Thank you everyone for the help.

Try this query, and put is in your HelpRequestsRepository class:
$subquery = $this->->select('st.status_id')
->from('/path/to/StatusFilter', 'st')
->where('st.filter_id = 1');
$query = $this->createQueryBuilder('hr')
->select('*')
->where('hr.status IN (' . $subquery->getDQL() . ')')
->getQuery();

Try this approach in the HelpRequestsRepository class:
$qb = $this->createQueryBuilder('hr');
$qb->select("hr");
$qb->join("::Status","s",Expr\Join::INNER_JOIN, "hr.status=s" );
$qb->join("::Filter","f",Expr\Join::INNER_JOIN, "s.filters=f" );
$qb->where("f.name = :name");
$qb->setParameter('name', $nameOfTheFilterToBeFound)
Hope this help

Related

show all employees from specific set of stores

My job is to cleanse the database from broken store records(records with missing data like a store name or streetname, database example below). However it turns out some of those records have employees linked to them so we can't just delete them.
Question: what kind of query do I need to make sure only the employees of those stores are shown?
I have three tables in which i need to work: user, department and store_users (store_users is a table in which all ID's are packed together to see which user_id belongs to which store_id.)
Example of code
its most likely wrong but its to give some insight in what I am trying to accomplish.(its purely mysql at the moment)
SELECT
*
FROM
gebruiker
WHERE
(SELECT
*
FROM
winkel_adresgegevens
WHERE
winkel_id IN (SELECT
winkel_gebruikers_winkel_id
FROM
winkel_gebruikers)
AND (winkel_naam = '' OR winkel_straat = ''
OR winkel_huisnummer = ''
OR winkel_postcode = ''
OR winkel_plaats = ''));
database example
+--------+---------------+----------+------------+------------------+
|store_id|store_branch_id|store_name|store_street|store_streetnumber|
|-------------------------------------------------------------------|
| 1 | 100 | Jumbo | Hilversum | 14 |
|--------+---------------+----------+------------+------------------|
| 2 | 150 | Lidl | Kerkelanden| 24 |
|--------+---------------+----------+------------+------------------|
| 3 | 105 | | Loosdrecht | |
|--------+---------------+----------+------------+------------------|
| 4 | 200 | Coop | | 14 |
+--------+---------------+----------+------------+------------------+
hence the question: only show employees from the broken records. (the ones with missing data, see store_name, store_street and store_streetnumber)
UPDATE : This is what you want
SELECT *
FROM employees
WHERE store_name IS NULL OR store_street IS NULL OR store_streetnumber IS NULL ;

Codeigniter - Getting Value from a Secondary Table

I have 3 tables in my DB: ‘workouts’, ‘exercises’ and ‘exercise_list’.
workouts: | id | datetime | misc1 | misc2 |
exercises: | id | ex_id | wo_id | weight | reps | wo_order |
exercise_list: | id | title |
So far I have generated a view which grabs details of a specific workout (myurl.com/workouts/view/<datetime>)
I have built a query that grabs the fields from ‘workouts’ and also it grabs any ‘exercises’ entries that correspond to that workout (by get_where using wo_id).
I build a view which lists the exercises for that workout, but I can only get as far as foreach’ing out the ‘id’ of the exercise. I need to somehow have a further query that grabs the ‘title’ of each exercise that is associated with that workout ‘id’.
So I currently have a table (html):
| Exercise | Weight | Reps |
| 1 | 50 | 8 | ...
I need ‘1’ to become the title of the exercise in ‘exercise_list’ with an ‘id’ of ‘1’.
My solution
May not be perfect but it works:
public function get_exercises($wo_id)
{
$this->db->select('exercises.wo_id,
exercises.weight,
exercises.reps,
exercise_list.title');
$this->db->from('exercises');
$this->db->join('exercise_list','exercises.ex_id= exercise_list.id');
$this->db->where('exercises.wo_id',$wo_id);
$q = $this->db->get();
$query = $q->result_array();
return $query;
}
Not sure about the bestway to do the last few lines. This is in my model, so I needed to return the array. I am going tobet there is a way to do it better than the last 3 lines.
You can use joins and select title from your exercise_list table
$this->db->select('w.*,el.title')
->from('workouts w')
->join('exercises e','w.id = e.wo_id')
->join('exercise_list el','el.id = e.ex_id')
->where('e.wo_id',$yourid)
->get()
->result();

php symfony doctrine or taking data from 2 tables

Since I'm very new don't judge me. I am trying to make a filter but there are some difficulties on the way. I have two tables of data. First with companies, second with users.
Companies:
____________________
id | name |
1 | try |
2 | test |
3 | experiment |
Users:
_____________________________________
|id | company_id | name | status |
| 1 | 1 | Idiot |pending |
| 2 | 1 | Funny |active |
| 3 | 2 | Me |pending |
| 4 | 2 | Lucky |rejected|
| 5 | 2 | Moon |rejected|
I have to make perhaps INNER JOIN and take only companies and users that are pending. I'm not interested in 'rejected'. So I'm interested to get:
3 | 2 | Me | pending | test
and other record with pending and no active. The company must have pending user and the same company must not have active.
SELECT *
FROM users u
INNER JOIN companies c
ON u.company_id = c.id
WHERE u.status = 'pending'
AND NOT EXISTS(SELECT u2.status
FROM users u2 ON u2.id = c.id
WHERE u2.status = 'pending')
or something like that was the SQL but I can't check it now. I want to make it Doctrine
e.g. $query = ..->innerJoin(..)->where... but can't make it. Any help please. Oh and how would this handle 100,000 records database for example? Is there a better way? Thank you.
in /lib/model/doctine/UsersTable.class.php:
class UsersTable extends Doctrine_Table
{
public function getAllCompanies($user_id) {
$q = $this->createQuery('u')
->where('u.istatus = ?','pending')
->leftJoin('u.Companies c WITH c.id = u.company_id')
->execute();
return $q;
}
}

count with doctrine

I have two table:
Name
id | name | city_id
1 | aaa | 1
2 | vvv | 2
3 | ddd | 2
4 | sss | 3
5 | dds | 1
etc
City:
id | name
1 | London
2 | NY
3 | Boston
etc
how can i get City and count:
name_city | count
London | 2
NY | 2
Boston | 1
In City table:
$q = $this->createQuery('a')
->leftJoin('a.Name n')
->select('a.name_city as name_city, sum(n.city_id) as sum');
return $q->execute();
but this is wrong.
You should use count() instead of sum(), and plus, you need a group by.
You do not appear to have a FROM clause, the object type is not specified for the a entity.
Also, read the aggregate values section in the documentation.
this post was kinda helpful but I though that I would add a little more details for anyone looking to join 2 tables and with an aggregate count.
e.g. this post (http://stackoverflow.com/questions/7837671/mysql-join-tables-and-count-instances) but in doctrine.
Using the example above the query would be (doctrine 2.0):
$q = $this->em->createQueryBuilder('a')
->select('a.name_city as name_city, count(n.city_id) as sum');
->from('city','a')
->leftJoin('a.Name n')
->groupBy('n.id')
->orderBy('sum')
$query = $qb->getQuery();
echo $qb->getDql(); // if you want to see the dql created
$result = $query->getResult();

Trouble structuring MySQL query with one-to-many over multiple tables

I have a blog-style app that allows users to tag each post with topics. I keep this data in three separate tables: posts (for the actual blog posts), topics (for the various tags), and posts_topics (a table that stores the relationships between the two).
In order to keep the MVC structure (I'm using Codeigniter) as clean as possible, I'd like to run one MySQL query that grabs all the post data and associated topic data and returns it in one array or object. So far, I'm not having any luck.
The table structure is like this:
Posts
+--------+---------------+------------+-----------+--------------+---------------+
|post_id | post_user_id | post_title | post_body | post_created | post_modified |
+--------+---------------+------------+-----------+--------------+---------------+
| 1 | 1 | Post 1 | Body 1 | 00-00-00 | 00-00-00 |
| 2 | 1 | Post 1 | Body 1 | 00-00-00 | 00-00-00 |
+--------+---------------+------------+-----------+--------------+---------------+
// this table governs relationships between posts and topics
Posts_topics
+--------------+---------------------+-------------------------+-----------------+
|post_topic_id | post_topic_post_id | post_topic_topic_id | post_topic_created |
+--------------+---------------------+-------------------------+-----------------+
| 1 | 1 | 1 | 00-00-00 |
| 2 | 1 | 2 | 00-00-00 |
| 3 | 2 | 2 | 00-00-00 |
| 4 | 2 | 3 | 00-00-00 |
+--------------+---------------------+-------------------------+-----------------+
Topics
+---------+-------------+-----------+----------------+
|topic_id | topic_name | topic_num | topic_modified |
+---------+-------------+-----------+----------------+
| 1 | Politics | 1 | 00-00-00 |
| 2 | Religion | 2 | 00-00-00 |
| 3 | Sports | 1 | 00-00-00 |
+---------+-------------+-----------+----------------+
I have tried this simple query with n success:
select * from posts as p inner join posts_topics as pt on pt.post_topic_post_id = post_id join topics as t on t.topic_id = pt.post_topic_topic id
I've also tried using GROUP_CONCAT, but that gives me two problems: 1) I need all the fields from Topics, not just the names, and 2) I have a glitch in my MySQL so all GROUP_CONCAT data is returned as a BLOB (see here).
I'm also open to hearing any suggestions where I run two queries and try to build out an array for each result; I tried that with the code below but failed (this code also includes joining the user table, which would be great to keep that as well):
$this->db->select('u.username,u.id,s.*');
$this->db->from('posts as p');
$this->db->join('users as u', 'u.id = s.post_user_id');
$this->db->order_by('post_modified', 'desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$posts = $query->result_array();
foreach($posts as $p)
{
$this->db->from('posts_topics as p');
$this->db->join('topics as t','t.topic_id = p.post_topic_topic_id');
$this->db->where('p.post_topic_post_id',$p['post_id']);
$this->db->order_by('t.topic_name','asc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach($query->result_array() as $t)
{
$p['topics'][$t['topic_name']] = $t;
}
}
}
return $posts;
}
Any help greatly appreciated.
This query should do the trick. Just change the * to the field list you desire so you are not pulling excess data every time you run the query.
Select
*
FROM
posts,
post_topics,
topics
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id
ORDER BY
post_id, topic_id;
Select
*
FROM
posts,
post_topics,
topics,
users
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id AND
post_user_id = user_id
ORDER BY
post_id, topic_id;
Holy Cow, You Can do it! See it helps to help. Never knew, try this
Select
post_id,
GROUP_CONCAT(DISTINCT topic_name) as names
FROM
posts,
post_topics,
topics
WHERE
post_topic_topic_id = topic_id AND
post_topic_post_id = post_id
GROUP BY
post_id;
You get
1, 'politics,relligion'
2, 'sports,relligion'

Categories