Symfony Order entities by field OneToMany - php

Who knows how to solved this simple question?
I have entity project with field likedUsers and in twig count up this field but I want order projects by this count up (DESC) - first who have more likedUsers. How to do it? In query builder or in twig create filter?Help with doctrine I know count
"likes_user" => count($this->getLikedUsers()->getValues())
how to sort my all projects from this field?
or how its solved with query builder?
something like that, but this is not work:
public function getProjects()
{
$qb = $this->getEntityManager()->createQueryBuilder('d');
$qb
->select('d')
->from('AppBundle:Project', 'd')
->where('d.confirm = :identifier')
->setParameter('identifier', 'approved')
->orderBy('COUNT(d.likedUsers)', 'DESC')
->getQuery()
->getResult()
;
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
entity:
/**
* Project
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\ProjectRepository")
*/
class Project implements \JsonSerializable
{
/**
* #var Collection
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="likedProjects")
*/
private $likedUsers;
{% for project in projects %}
<div>LIked: <span>{{ project.likedUsers|length|number_format(0, '.', ' ') }}</span></div>
{% endfor %}
I think maybe like that:
{% for project in projects.likedUsers|length|sort %}
but not effect
maybe who knows? How array projects sort by count of field LikedUser. I don’t know how do this. Query builder or twig extension or usort..

IMHO, this seems like a job for PHP instead of Doctrine. Not sure if t could be achieved by Doctrine and, if possible, what would be an impact.
One way to do this is usort().
$projects = ...;
usort($projects, function($p1, $p2){
// Both $p1 and $p2 are instance of Project
// Assuming that `getLikedUsers()` return Doctrine `Collection`...
return count($p1->getLikedUsers()) > count($p2->getLikedUsers());
});
usort takes an array and in each pass offers two items (two projects in your case) for you to decide which one is "greater". In your case, the project with more likes is definitely "greater", right?
Hope this helps...

I solved with query builder. And I add only project who have like user - user live in some city no all city
public function getProject()
{
$qb = $this->getEntityManager()->createQueryBuilder('d');
$qb
->select('d')
->from('AppBundle:Project', 'd')
->addSelect('COUNT(m.id) as nMethods')
->join('d.likedUsers', 'm')
->join('m.location', 'l')
->where('l.city = :identifier')
->setParameter('identifier', 'someCity')
->groupBy('d.id')
->orderBy("nMethods", 'DESC')
->getQuery()
->getResult()
;
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}

Related

Symfony Doctrine disable cache

In my symfony project I have two entities that are related via one to many.
I need to find the first and last child, so I use repository functions that look like this:
public function getFirstPost(Topic $topic)
{
$query = $this->createQueryBuilder('t')
->addSelect('p')
->join('t.posts', 'p')
->where('t.id = :topic_id')
->setParameter('topic_id' => $topic->getId())
->orderBy('p.id', 'ASC')
->setMaxResults(1)
->getQuery();
return $query->getOneOrNullResult();
}
public function getLastPost(Topic $topic)
{
$query = $this->createQueryBuilder('t')
->addSelect('p')
->join('t.posts', 'p')
->where('t.id = :topic_id')
->setParameter('topic_id' => $topic->getId())
->orderBy('p.id', 'DESC')
->setMaxResults(1)
->getQuery();
return $query->getOneOrNullResult();
}
So the only difference is in in ->orderBy(), for the first Post I use ASC and for the last I use DESC.
Now If I use one of those functions from my controller, the return the expected result and work just fine. But If I run them both at the same time from my controller, they return the same result, which they shouldn't.
My guess is that Doctrine caches these queries and the results somehow and that's why the return the same so I tried using $query->useResultCache(false) but that didn't do anything.
So my question is, why is this happening and how can I fix it?
Well, it is cache issue indeed, but mostly it is query issue. Instead of returning a post in these function you return the whole topic with joined posts.
What you can do is to rewrite these queries to select Post entity directly and join Topic entity to it which will be filtered by.
If you really(dont do this) need these queries to work you can detach first topic returned by one of those methods and then call the other method:
$this->getDoctrine()->getManager()->detach($firstTopic);

Order attribute of an object in Symfony

I would like to reorder the attribute (COMMENTS) of my object (instance of ARTICLE) after I retrieve it from the DBB. Is this possible?
My object is ARTICLE and it is linked to COMMENTS (which is defined as a collection in entity article)
I know I can order through the repository but the order of my comments depend on many conditions, some not available through the DB.
Condition exemple:
I want at the top the comment whose attribute show_first are set to true whatever their score, and then the other comments ordered depending of their score.
You could add a hidden field to you query that sorting things in the order that you wanted so that you don't need to process the complete ArrayCollection to sort.
public function findByArticleInOrderOfState()
{
return $this->createQueryBuilder('c')
->select('c')
->addSelect('
CASE
WHEN c.state = :state_new THEN 1
WHEN c.state = :state_viewed THEN 2
WHEN c.state = :state_deleted THEN 3
ELSE 4
END AS HIDDEN order_by
')
->setParameter('state_new', 'new')
->setParameter('state_viewed', 'viewed')
->setParameter('state_deleted', 'deleted')
->orderBy('order_by', 'ASC')
->addOrderBy('c.createdAt', 'ASC')
->getQuery()
->getResults();
}
This would create a hidden field order_by and set that depending on the current state of that object, then it would order by that hidden field and then createdAt.
It doesn't really make sense to order comments like that but it does show how you could do it. With a little more info on the actual use case I would (hopefully) be able to make work a bit closer to your specific needs.
Update
In your case when you have show_first == 'yes'|'no' you could do the following..
public function findByArticleInOrderOfState()
{
return $this->createQueryBuilder('c')
->select('c')
->addSelect('
CASE
WHEN c.show_first = :show_first THEN 1
ELSE 2
END AS HIDDEN order_by
')
->setParameter('show_first', 'yes')
->orderBy('order_by', 'ASC')
->addOrderBy('c.createdAt', 'ASC')
->getQuery()
->getResults();
}
Set the getter of comments (getComments()) in your Article entity to get the comments in the order you want.
public function getComments(){
$iterator = $comments->getIterator();
$iterator->uasort(function ($a, $b) {
// change getProperty() with the field you want to order on
return ($a->getProperty() < $b->getProperty()) ? -1 : 1;
});
$comments= new ArrayCollection(iterator_to_array($iterator));
return $comments;
}
For more Infos visit this post "usort" a Doctrine\Common\Collections\ArrayCollection?
For simple ordering of associations, you can use Doctrine annotations.
/**
* #ORM\OneToMany(targetEntity="Comment", mappedBy="article")
* #ORM\OrderBy({"show_first" = "ASC", "score" = "DESC"})
*/
private $comments;
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/ordered-associations.html
The following possible within an entity object
public function getCommentsActiveLast3()
{
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('status', Comment::STATUS_PUBLISHED));
$criteria->setMaxResults(3);
if ($this->comments) {
return $this->comments->matching($criteria);
}
}

How convert SQL query to Doctrine2?

I want to convert this in Doctrine2.
Similar to these questions:
Symfony2 / Doctrine2 - How to convert this SQL request with QueryBuilder?
How to convert a complex MySQL Query to Doctrine2
Convert SQL to Doctrine 2 Query Builder or DQL by using NOT IN?
Is it possible?
SELECT
SUM(q.amount)
FROM(
SELECT amount
FROM expense_report e0_
WHERE
(e0_.is_account_transfer = 0)
AND (e0_.remove_date IS NULL)
LIMIT 0, 2) q
Thanks
you can archive your problem with this custom repository method:
public function getAmount()
{
$qb = $this->createQueryBuilder("q");
return
$qb
->select("sum(q.amount)")
->where($qb->expr()->isNull('q.remove_date'))
->andWhere('q.is_account_transfer = 0')
->setFirstResult(0)
->setMaxResults(2)
->getQuery()
->getSingleScalarResult();
}
And simply use it, as example, in a controller method as:
public function testAction()
{
....
$totalAmount = $this->getDoctrine()
->getRepository("AcmeDemoBundle:ExpenseReport")
->getAmount();
...
}
Hope this help

Get all entities ordered with Doctrine query builder

I'm getting a little crazy with this. I have a PhoneCodes entity and I simply want to retrieve all entities ordered by a field so no where condition but I tried to achieve this by many ways and not working. Currently I have this:
$phonecodes = $this->getDoctrine()
->getRepository('AcmeDemoBundle:PhoneCodes')
->createQueryBuilder('p')
->orderBy('p.length', 'ASC')
->getQuery()
->getResult();
What's the way to do this? Thanks.
Your code should be something like this:
$phonecodes = $this->getEntityManager()
->createQueryBuilder()
->select("p")
->from("AcmeDemoBundle:PhoneCodes", "p")
->orderBy("p.length", "ASC")
->getQuery()
->getResult()
If you're in a controller just do this:
$phonecodes = $em->getRepository('AcmeDemoBundle:PhoneCodes')->findBy(
array(),//conditions, none in this case
array(//orderBy, multiple possible
"length"=>"asc"
)
);
This way you don't need to write a custom repository function.
If you wan't to create it as a repository function (e.g. in PhoneCodesRepository.php) do it that way:
/**
* Returns all phoneCodes hydrated to objects ordered by length
* #param string $order - ASC | DESC
* #return \Doctrine\Common\Collections\Collection
*/
function findAllOrderedByLength($order="ASC")
{
$qb = $this->createQueryBuilder("p");
$qb->orderBy("p.length", $order);
return $qb->getQuery()->getResult();
}
http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

How to use a findBy method with comparative criteria

I'd need to use a "magic finder" findBy method using comparative criteria (not only exact criteria). In other words, I need to do something like this:
$result = $purchases_repository->findBy(array("prize" => ">200"));
so that I'd get all purchases where the prize is above 200.
The class Doctrine\ORM\EntityRepository implements Doctrine\Common\Collections\Selectable API.
The Selectable interface is very flexible and quite new, but it will allow you to handle comparisons and more complex criteria easily on both repositories and single collections of items, regardless if in ORM or ODM or completely separate problems.
This would be a comparison criteria as you just requested as in Doctrine ORM 2.3.2:
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria->where(\Doctrine\Common\Collections\Criteria::expr()->gt('prize', 200));
$result = $entityRepository->matching($criteria);
The major advantage in this API is that you are implementing some sort of strategy pattern here, and it works with repositories, collections, lazy collections and everywhere the Selectable API is implemented.
This allows you to get rid of dozens of special methods you wrote for your repositories (like findOneBySomethingWithParticularRule), and instead focus on writing your own criteria classes, each representing one of these particular filters.
This is an example using the Expr() Class - I needed this too some days ago and it took me some time to find out what is the exact syntax and way of usage:
/**
* fetches Products that are more expansive than the given price
*
* #param int $price
* #return array
*/
public function findProductsExpensiveThan($price)
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb->select(array('p'))
->from('YourProductBundle:Product', 'p')
->where(
$qb->expr()->gt('p.price', $price)
)
->orderBy('p.price', 'DESC')
->getQuery();
return $q->getResult();
}
You have to use either DQL or the QueryBuilder. E.g. in your Purchase-EntityRepository you could do something like this:
$q = $this->createQueryBuilder('p')
->where('p.prize > :purchasePrize')
->setParameter('purchasePrize', 200)
->getQuery();
$q->getResult();
For even more complex scenarios take a look at the Expr() class.
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria->where($criteria->expr()->gt('id', 'id'))
->setMaxResults(1)
->orderBy(array("id" => $criteria::DESC));
$results = $articlesRepo->matching($criteria);
The Symfony documentation now explicitly shows how to do this:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Product p
WHERE p.price > :price
ORDER BY p.price ASC'
)->setParameter('price', '19.99');
$products = $query->getResult();
From http://symfony.com/doc/2.8/book/doctrine.html#querying-for-objects-with-dql
I like to use such static methods:
$result = $purchases_repository->matching(
Criteria::create()->where(
Criteria::expr()->gt('prize', 200)
)
);
Of course, you can push logic when it is 1 condition, but when you have more conditions it is better to divide it into fragments, configure and pass it to the method:
$expr = Criteria::expr();
$criteria = Criteria::create();
$criteria->where($expr->gt('prize', 200));
$criteria->orderBy(['prize' => Criteria::DESC]);
$result = $purchases_repository->matching($criteria);
Copying the findBy query and modifying it to return your expected result is a good approach.

Categories