I have two entities ServeApp and App. They both have a relation to AppPage. I'm trying to get ServeApps that have AppPages which App does not have.
So ServeApps.appPages should not contain the same AppPages that Apps.appPages contains.
I'm trying to write a DQL query that would be something similar to
SELECT ServeApps WHERE ServeApps.appPages NOT IN Apps.appPages
but I'm at a loss how to go about it. I tried $queryBuilder->whereNotIn() but I guess that method doesn't exists?
Any help would be appreciated :)
EDIT:
I ended up changing my approach. Since the AppPages had a many to many relationship to both ServeApps and Apps, I did the following:
SELECT sa FROM ServeApps sa WHERE sa.id NOT IN (
SELECT ssa.id FROM AppPage ap JOIN ap.serveApps ssa WHERE ap.id = :apppage
)
I changed my logic to take out the Apps from the equation and then I checked it differently. I also had a version where I resulted to raw SQL for counting the ServeApps.
Your where-not-in statement should look like this
$queryBuilder->where($queryBuilder->expr()->notIn('alias.field', $arrayOfValues));
You can find a reference of all expressions in Doctrine's documentation.
As a side note, a where-in statement should look like this
$queryBuilder->where($queryBuilder->expr()->in('alias.field', $arrayOfValues));
Related
I am so convenient using built-in codedigniter query builder.
I have to output searching to an existing page since that page involves multi-table.
Currently, I already have a lot of if there,
I am curious how to check if statement db->where() or db->like() have been called or not?
My second option is to construct new query that join 2 tables, but my table does not have any direct relationship, I try to use $this->db->join() it fails on the join condition. I am lack of example of using join, especially the join type. I am expecting to use something like
SELECT * FROM p JOIN hs ON hs.id=%-p.id-%
I am already stuck here, and I am aware that the db->get() will erase the data filtering.
Any suggestion?
Greetings to everyone.
How can I add condition to "join on" for belongsToMany (many to many) relation in Laravel 5.3?
For example, let's say here are Products and Parameters of those Products with many to many relation. When I want to get all Parameters for specific Product, it is just fine. I do $product->parameters and it gives me what I need with query like this:
SELECT
parameters.*,
product_parameters.product_id AS pivot_product_id,
product_parameters.parameter_id AS pivot_parameter_id
FROM parameters
INNER JOIN product_parameters ON parameters.id = product_parameters.parameter_id
WHERE product_parameters.product_id = 1;
Problem appears when I want to define some "Global" flag on Parameter which defines that Parameter should be bound to every Product. With plain SQL, all I have to do is to add condition to JOIN, OR parameters.global = 1, like this:
SELECT
parameters.*,
product_parameters.product_id AS pivot_product_id,
product_parameters.parameter_id AS pivot_parameter_id
FROM parameters
INNER JOIN product_parameters ON parameters.id = product_parameters.parameter_id OR parameters.global = 1
WHERE product_parameters.product_id = 1;
But I didn't find if there is correct and clean way to do it with Laravel to be able to get Eloquent\Relation object back from function with additional data I require.
I have found "bad" way, which is to "fetch" join which I need from query builder object and add another "where" to it, but it is very ugly and I'm not sure that it will work in future without problems.
This is what I have tried to do and it almost worked (have problem with incorrect number of PDO parameters, but it generates SQL which I need):
$relation = $this->belongsToMany(Parameter::class, 'product_parameters');
$relation->getQuery()->getQuery()->joins[0]->where('parameters.definition', '=', 0, 'or');
Any help or advice is appreciated. Thank you.
I have left original idea to place everything in queries, tried to do several requests and combine resulting Eloquent Collections. Then I did some research and in the end I decided that what I was going wrong way, and it is better to use patterns like Repository and Specification to achieve required logic.
I am relatively new to Doctrine and am having an absolute nightmare trying to get a join between two entities returned correctly. I don't really understand the DRM that Doctrine uses as much as I would like at the moment so it really is just a case of trial and error at the moment.
I have two entities, one is document and one is document-sow-detail. I need to join the two where the document_id exists in the document-dow-detail table/entity as to get a couple of fields that exist in the document-dow-detail entity returned.
So, within my document repository, I have attempted the following code (with not a lot of understanding of what is happening). Can someone point me as to what is happening here and why it is not working?
$db = $this->createQueryBuilder($this->alias);
$db->addSelect("dsd");
$db->leftJoin("doc_sow_detail",'dsd','ON',"id_document");
When I breakpoint and evaluate the $db->getQuery() function in my IDE this is the _dql that has been generated.
SELECT d, dsd FROM BillingBundle\Entity\Document d LEFT JOIN doc_sow_detail dsd ON id_document
Can anyone give me a clue as to what I am doing wrong here before I chuck my computer out the window?
Thanks!
A simple example of doctrine join which joins product category on category:
$qb = $this->_em->createQueryBuilder();
$qb->select('p')
->from('BRBProductBundle:Product', 'p')
->join('p.category', 'c')
->where('1 = 1');
Hope this may help you.
I thought I'd post my solution.
I had to change my entity structures due to the one-way relationship between the two entities.
I moved the required entity properties onto the document entity which negated my need for a join. This is probably where these properties needed to be in the first place.
I have a USER table structure as shown below:
id parent_id userName
10 01 Project manager
11 10 manager
12 11 teamlead
13 12 team member
I need to find the project manager ID if I give the team member ID in where clause. I can get the results in each individual query.
But I'm trying to implement it with a JOIN query. I'm new to JOIN queries. How do I do it?
It looks as if this involves a bit more than a simple join. Be ready to enter a world of pain :). I recently had a similar problem, but with type hierarchies being stored in a table with a similar structure. What I ended up with is writing a recursive query. In Sql Server, you would use a Common Table Expression. In mysql, you would use loops.
Basically, the idea is that you join a table against itself, walking a hierarchy until you reach the top-level element. Behind the scenes, the server is creating virtual tables and joining them against each other until some "stopping condition" is reached. This point is very important: be sure that you have your stopping condition correct, or you could cause some serious problems.
This post is a great run-down. Also, a general search for the terms hierarchical query mysql in google will result in a wealth of information.
I believe this should work with your existing schema
SELECT ParentUser.UserName AS ManagerName, BaseUser.UserName AS TeamMemberName
FROM User AS BaseUser
INNER JOIN User AS ParentUser
ON BaseUser.parent_id = ParentUser.id
WHERE BaseUser.Id = #PassedInTeamMemberId
Basically you want to do this:
SELECT * FROM TableA
INNER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.whatever = 'whatever'
I find this visual explanation of joins from the lovely and talented Jeff Atwood to be quite helpful.
I have two entities in Doctrine 2.1: Category and Site each category has many sites and each site has a parent category.
I would like to make a single update query (in DQL) which will update a field called count of the Category entity with the number of related sites.
So in SQL I would do something like this:
UPDATE categories c SET c.count = (SELECT COUNT(s.id) FROM sites s WHERE s.category_id = c.id);
This would work beautifuly, in DQL it might something like this:
UPDATE PackageNameBundle:Category c SET c.count = (SELECT COUNT(s.id) FROM PackageNameBundle:Site s WHERE s.category = c)
Such attempt raises [Syntax Error] line 0, col 61: Error: Expected Literal, got 'SELECT'.
Subqueries DO work in DQL, but the problem here (as far as I see it) is that Doctrine cannot assign the returned value from the subquery, to the c.count. This is understandable since I might fetch more than 1 field in the subquery and even more than one row. It magicaly works in MySQL since it sees one row, one field and for convenience returns a single integer value. Doctrine on the other hand has to be object oriented and has to work with different engines where such convertions might not be supported.
Finally, my question is:
What is the best way to do this in Doctrine, should I go with Native SQL or it can be done with DQL and how?
Thanks in advance!
EDIT: I just found this quote in the DQL Docs:
References to related entities are only possible in the WHERE clause and using sub-selects.
So, I guess assigning anything but a scalar value is impossible?
The main question remains though..
You can use native sql queries in Doctrine also, for that kind of specific queries. DQL is powerful in its own way, but it's also limited due to performance constraints. Using native sql queries and mapping the results will achieve the same thing, and there is no disadvantage in doing that.
The documentation explains it in detail.