I've got an SQL query that returns all the rows in one table (country) which have a related entry in another table (ducks) but I'm struggling to turn this into DQL. This is a standard one-many relationship as each country can have multiple ducks, I believe it is all set up correctly as I can return ducks within a country and return the country a duck is in using standard code.
The working query is:
SELECT c.* FROM country c
INNER JOIN ducks d
ON c.id = d.country_id
GROUP BY c.country
ORDER BY c.country ASC
I've tried converting this to:
SELECT c FROM WfukDuckBundle:Country c
INNER JOIN WfukDuckBundle:Ducks d
ON c.id = d.country_id
GROUP BY c.country
ORDER BY c.country ASC
which produces the following error:
[Semantical Error] line 0, col 79 near 'd ON': Error: Identification Variable
WfukDuckBundle:Ducks used in join path expression but was not defined before.
I'm quite new to Symfony/Doctrine so I suspect it's probably something obvious!
I'm using Symfony 2.0.11 with doctrine
Update:
I've Also tried:
SELECT c FROM WfukDuckBundle:Country c
INNER JOIN c.ducks d
ON c.id = d.country_id
GROUP BY c.country
ORDER BY c.country ASC
where 'ducks' is defined in the Country class as:
/**
* #ORM\OneToMany(targetEntity="Ducks", mappedBy="country")
*/
protected $ducks;
public function __construct()
{
$this->ducks = new ArrayCollection();
}
the definition for country in the ducks class is:
/**
* #ORM\ManyToOne(targetEntity="Country", inversedBy="ducks")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id")
*/
private $country;
Do yourself a favour and use the query builder. Easier to read and update and reuse your queries
<?php
namespace Vendor\Prefix\Repository;
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
{
public function countryDucks()
{
// $em is the entity manager
$qb = $em->createQueryBuilder();
$qb
->select('country', 'duck')
->from('WfukDuckBundle:Country', 'country')
->innerJoin('country.ducks', 'duck')
->groupBy('country.country')
->orderBy('country.country', 'ASC')
;
$query = $qb->getQuery();
// Potential Hydration Modes
// --------------------------------
// Doctrine\ORM\Query::HYDRATE_OBJECT
// Will give you an array of your object entities
// --------------------------------
// Doctrine\ORM\Query::HYDRATE_ARRAY
// Will give you an array mimicking
// your object graph
// --------------------------------
return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
}
Related
I'm creating a function that should return an array of User ORM object. The function should run a query to the DB and return the users where the users' contact persons has 1 company (not more or less). The relationship is like this: every user has one or more contact person and every contact person has one or more companies.
The SQL to locate these users are like this. We are using PHP 7.1, Symfony 3.4 and Doctrine 2.7.
The problem that I have is that I cannot manage to describe this in Doctrine QueryBuilder syntax so that an array of User ORM objects are returned. Can anybody give me some advice?
SELECT users.email
FROM company
INNER JOIN contact_person ON contact_person.id = company.belongs_to_contact_person_id
INNER JOIN users ON users.id = contact_person.belongs_to_user_id
GROUP BY users.email
HAVING COUNT(company.id) = 1
Depending on how your mapping is on your entities, you have multiple solution.
It would be nice if you can show us what you tried so we can see what you miss.
The best is to use the repository of the entity you whish to have an array of:
namespace App\Repository;
use App\Entity\User;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
/**
* #return User[]
*/
public function findUsersHavingAtLeastOneCompany():array
{
return $this->createQueryBuilder('user')
->join('user.contact', 'contact')
->join('contact.company', 'company')
->where('contact.company = 1')
->getQuery()
->getResult();
}
}
When using the createQueryBuilder function, it will auto populate the select and the from.
The getResult will return an array of entity (if you have not defined a select)
I fixed this by using the following code using createNativeQuery. It can probably be done by using fever lines of code, but it does the job for me :)
$em = $this->getEntityManager();
$sql = <<<SQL
SELECT users.id
FROM company
INNER JOIN contact_person ON contact_person.id = company.belongs_to_contact_person_id
INNER JOIN users ON users.id = contact_person.belongs_to_user_id
GROUP BY users.id
HAVING COUNT(company.id) = 1
SQL;
$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'text');
$query = $em->createNativeQuery($sql, $rsm);
$locatedUsers = [];
foreach ($query->getResult() as $lUser) {
foreach ($lUser as $user) {
$locatedUser = $em->find("Project\User\User", $user);
array_push($locatedUsers, $locatedUser);
}
}
return $locatedUsers;
Im trying to get the best rated movies by average from my database and hydrate them nicely into a DTO with doctrine so i can work well later with it and integrate them e.g. into my api with api platform.
I already managed to get it work with a raw SQL query but i cannot manage to get it work with hydration into a DTO with doctrine.
namespace App\Repository;
use App\Entity\Movie;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
class MovieRepository extends ServiceEntityRepository
{
public function findBestRated()
{
$sql = "
SELECT m.*, AVG(Cast(r.rating as Decimal)) AS avg_rating, COUNT(r.id) AS count_rating
FROM `movie` m
JOIN rating r ON r.movie_id = m.id
GROUP BY m.id
ORDER BY avg_rating DESC
LIMIT 10
";
$connection = $this->getEntityManager()->getConnection();
$statement = $connection->prepare($sql);
$result = $statement->executeQuery();
return $result->fetchAllAssociative();
}
}
I would like to use a DTO like below:
namespace App\Dto;
use App\Entity\Movie;
class RatedMovie
{
public Movie $movie;
public float $averageRating;
public int $ratingCount;
public function __construct(Movie $movie, float $averageRating, int $ratingCount)
{
$this->movie = $movie;
$this->averageRating = $averageRating;
$this->ratingCount = $ratingCount;
}
}
I found some information on https://geek-week.imtqy.com/articles/en496166/index.html, but still i cannot get the hydration running. I already tried with ResultSetMapping and ResultSetMappingBuilder and a native doctrine query. With ResultSetMappingBuilder i can kind of simulate my raw sql query but the result is still an associative array and not mapped into the RatedMovie DTO.
public function findBestRated()
{
$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addScalarResult('id', 'movieId');
$rsm->addScalarResult('name', 'movieName');
$rsm->addScalarResult('avg_rating', 'averageRating', Types::FLOAT);
$rsm->addScalarResult('count_rating', 'countRating', Types::INTEGER);
$sql = "
SELECT m.*, AVG(r.rating) AS avg_rating, COUNT(r.id) AS count_rating
FROM `movie` m
JOIN rating r ON r.name_id = m.id
GROUP BY m.id
ORDER BY avg_rating DESC
LIMIT 10
";
$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();
}
I cannot get it running with newObjectMappings or the kind of strange syntax SELECT NEW DepartmentSalary(d.dept_no, avg_salary) FROM ... from the linked article. Any ideas?
I struggle to create a query to get information from two tables. I want to count rows and group it by category and type.
My normal PostgresSQL query looks like this:
SELECT c.name AS category_name, i.type, count(i) AS number_of_items
FROM item i
INNER JOIN category c
ON i.category_id = c.id
GROUP BY c.name, i.type
How do I build this query using Doctrine Query Builder?
What I have tried:
$qb->select(['c.name', 'i.type', 'count(i)'])
->from('AppBundle:Item', 'i')
->innerJoin('i', 'AppBundle:Category', 'c', 'i.category_id = c.id')
->groupBy('c.name')
->groupBy('i.type');
return $qb->getQuery()->getResult();
But this give me an error:
[Semantical Error] line 0, col 80 near 'i AppBundle:Category':
Error: Class 'i' is not defined.
I'm trying to follow the principle in the documentation found here: http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/query-builder.html#join-clauses
Any help would be appreciated.
Using doctrine and without defining any mapping between your related entities is not a good practice you should start from Association Mapping
Once you have defined mappings in your entities you can simply join your main entity using the properties which holds the reference of linked entities, doctrine will automatically detects the join criteria you don't need to specify in query builder, sample mapping for your entities can be defined as
class Item
{
/**
* #ORM\ManyToOne(targetEntity="Category", inversedBy="items")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
}
-
use Doctrine\Common\Collections\ArrayCollection;
class Category
{
/**
* #ORM\OneToMany(targetEntity="Item", mappedBy="category")
*/
private $items;
public function __construct()
{
$this->items = new ArrayCollection();
}
}
And then your query builder will look like
$qb->select('c.name', 'i.type', 'count(i)'])
->from('AppBundle:Category', 'c')
->innerJoin('c.items','i')
->groupBy('c.name')
->addGroupBy('i.type');
Relationship Mapping Metadata
Or if you still don't want to have mappings and use the other approach you have to use WITH clause in doctrine
$qb->select(['c.name', 'i.type', 'count(i)'])
->from('AppBundle:Item', 'i')
->innerJoin('AppBundle:Category', 'c', 'WITH' , 'i.category_id = c.id')
->groupBy('c.name')
->addGroupBy('i.type');
you don't need to use array inside select try this:
$qb->select('c.name', 'i.type', 'count(i)')
->from('AppBundle:Item', 'i')
->innerJoin('i', 'AppBundle:Category', 'c', 'i.category_id = c.id')
->groupBy('c.name')
->groupBy('i.type');
return $qb->getQuery()->getResult();
Tried to use full expression for your entity Like this :
->from('AppBundle\Entity\Item','i')
I'm new in Symfony and I have 2 entities (not me who created them):
1st entity: test1 (id,test2_id)
2nd entity: test2 (id,label)
I want to create the query that select from test where test2.label = 1.
$Websites = $this->_em
->createQuery("
SELECT w
FROM \Bundle\Entity\test1 t1
JOIN t1.test2 t2
WHERE t2.label=1")
->getResult();
But I got an error:
Bundle\Entity\test1 has no association named test2
Is there the solution or another way to make it work.
Try this:
$Websites = $this->_em
->createQuery("
SELECT w
FROM \Bundle\Entity\test1 t1
JOIN \Bundle\Entity\test2 t2
WHERE t2.id = t1.test2_id AND t2.label=1")
->getResult();
You can also use createQueryBuilder method
Using createQueryBuilder you can do with the following
$labelValue = 1;
/* Get the EntityManager Resource */
$em = $this->getDoctrine->getManager();
$em->getRepository('AppBundle:test1')
->createQueryBuilder('t1')
->select('t1.w') /*Here you can select respective table columns */
->innerJoin('t1.test2_id', 't2')
->where('t2.label = :label') /* if label value coming from external value else ->where('t2.label = 1') and omit next line */
->setParameter('label',$labelValue) /*In case if your label value coming for external value */
->getQuery()
->getResult();
I have 2 classes that I want to select from DB using 1 query in Symfony2 Doctrine.
The first entity is Calculation and the second is Polynomial. They have 1:1 relationship:
/**
* Acme\UserBundle\Entity\Calculation
*
* #ORM\Table(name="Calculation")
* #ORM\Entity(repositoryClass="AppBundle\Entity\CalculationRepository")
*/
class Calculation {
//...
/**
* #ORM\OneToOne(targetEntity="Polynomial")
* #ORM\JoinColumn(name="result_polynomial_id", referencedColumnName="id", nullable=false)
**/
private $resultPolynomial;
//...
}
I have a query, that returns me all Calculations of one user:
public function findByUser( $user ) {
return $this->getEntityManager()->createQuery(
'SELECT c
FROM AppBundle:User u
JOIN AppBundle:Polynomial p WITH u = p.user
JOIN AppBundle:Calculation c WITH p = c.resultPolynomial
WHERE u = :user
ORDER BY c.id'
)
->setParameter('user', $user)
->getResult();
}
And to the question... Is there a way to get resultPolynomials of the calculations in one query? If I use something like SELECT c, c.resultPolynomial I get error:
[Semantical Error] line 0, col 12 near 'resultPolynomial
': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
And if I use foreach cycle all calculations to get the their resultPolynomials there are many queries to the DB (1 for each calculation) and it is not good for performance if I have many calculations.
I making a guess because you did not post your User definition nor your Polynomial definition.
I think you can make your DQL this way:
SELECT c, p
FROM AppBundle:User u
JOIN u.polynomial p
JOIN p.calculation c
WHERE u = :user
ORDER BY c.id
I guess you already defined your relations in your model so you don't need to repeat it here.