I have 2 Entities
User
Article
and a “likedByUsers” Many To Many relationship between both.
When I show an article, I want to know if the user has liked it so a heart icon is shown.
I've got this in the ArticleRepository:
public function findOneBySlug($slug,$userId): ?Pack
{
return $this->createQueryBuilder('p')
->andWhere('p.slug = :val')
->setParameter('val', $slug)
->addSelect('COUNT(u) AS userLike', 'p')
->leftJoin("p.users", 'u', 'WITH', 'u.id = :userId')
->setParameter('userId', $userId)
->getQuery()
->getOneOrNullResult()
;
}
But it throws an error:
Return value of App\Repository\ArticleRepository::findOneBySlug() must be
an instance of App\Entity\Article or null, array returned
I want to add "userLike" (bool) to the Article returned entity. Anyone can help me out?
calling addSelect(...) on a query builder might change the return type / format.
in your particular case, the former db result was something like [... all the article properties ...] which hydration and the getOneOrNullResult turns into one Article or null.
the new format looks like
[... all the article properties ..., userlike], which hydration turns into [Article, userlike] which can't possibly turned into one Article or a null result, because it's a "more complex" array.
So you have to use a different result fetcher. Depending on what the caller of your function expects as a return value (I would expect an article ^^) you maybe should rename the function or add a virtual property on article to hide the userlike or something, so you can return just the Article or null.
So the solution that I would choose:
$result = $this->createQueryBuilder(...)
//...
->getSingleResult();
if(!$result) {
// empty result, obviously
return $result;
}
// $result[0] is usually the object.
$result[0]->userLike = $result['userLike'];
// or $result[0]->setUserLike($result['userLike'])
return $result[0];
btw: $this->createQueryBuilder($alias) in a repository automatically calls ->select($alias), so you don't have to addSelect('... userLike', 'p') and just do addSelect('... userLike')
Related
Good afternoon, please tell me how to get the column data:
{#ORM\Index(name="localities_names_idx", columns={"name_ru", "name_cn", "name_en"})
I tried using queryBuilder :
$qb
->select('a')
->from(Locality::class, 'a')
->where('a.name_ru = :name_ru')
->andWhere('a.area is null')
->setParameter('name', 'Moscow');
$query = $qb->getQuery();
Without success
I need it for:
$em = $this->entityManager;
$qb = $em->createQueryBuilder();
$qb
->select('a')
->from(Locality::class, 'a')
->where('a.name_ru = :name_ru')
->andWhere('a.area is null')
->setParameter('name', 'Москва');
$query = $qb->getQuery();
$result = $query->getResult(Query::HYDRATE_SCALAR);
$location = new Location();
$location->setLocality($result[0]['a_id']);
$location->setAddress($address);
$em->persist($location);
$em->flush();
return $location->getId();
Edit: This turned into a review, but might as well keep it here
You're using index wrong.
I'm guessing you're using it incorrectly. I'm assuming you want an index for faster search. However, the way you do now you've created a combined index. Example:
{#ORM\Index(name="thing_colour_idx", columns={"thing", "colour"})
Thing | Colour
--------------
Car Blue
Car Red
Bike Green
Bike Yellow
If you always (or at least most of the time) select by both columns, eg you always search for a BLUE+CAR, or a GREEN+BIKE, than this is the way to go.
However, if you to select all Thing=Car, without colour, then this index does nothing. You want this:
indexes={
#ORM\Index(name="thing_idx", columns={"thing"}),
#ORM\Index(name="colour_idx", columns={"colour"})
}
You're not using getResult as intended
You do ->getResult(Query::HYDRATE_SCALAR), but then follow it up with a ->setLocality($result[0]['a_id']). Unless you have performace issues, you dont work with IDs, thats a problem for Doctrine, not you. You should only care about objects:
$locality = $em
->createQueryBuilder()
->from(Locality::class, 'a')
->where('a.name_ru = :name_ru')
->andWhere('a.area is null')
->setParameter('name', 'Москва') // <- btw, this should be 'name_ru', same as two lines heigher
->getQuery()
->getSingleResult();
$location = new Location();
$location->setLocality($locality);
Dont use the querybuilder like that, use a repository
You're now placing service logic and query logic in one code. That is incorrect. An example of the way Symfony is intented:
class LocalityRepository extends ServiceEntityRepository{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Locality::class);
}
public function getLocalityByRuName(string $name): Locality
{
return $this->createQueryBuilder('a')
->where('a.name_ru = :name_ru')
->andWhere('a.area is null')
->setParameter('name_ru', $name);
->getQuery();
->getSingleResult();
}
}
class YourService
{
public function __construct(
private LocalityRepository $localityRepository
){}
public function somethingSomething()
{
$locality = $this->localityRepository->getLocalityByRuName('Москва');
$location = new Location();
$location->setLocality($locality);
}
}
Final note: Try not to use a ->flush() in a service. The job of a service is to do work, not to decide what to do it. A controller decides when something is done. Suppose you have another service which in which you alter things, but DONT want to flush them. You can now no longer use any service method that flushes.
The error was that there were no name properties Ru
I have this script.
It fetch the rows from DB and choose the row which belongs to a Category(CatData).
getCatDatas() returns \Doctrine\Common\Collections\ArrayCollection() class.
So it can use contain() to check.
However I want to put this method in DQL itself, is there any practice??
$result = array();
$articles = $em->createQuery("SELECT a FROM DefaultBundle:ArticleData a ")
->getResult();
foreach ($articles as $a){// I want to put this function in DQL.
if ($a->getCatDatas()->contain($cat)){
array_push($articles,$result);
}
}
Yes, you can use DQL and do a where condition on relation.
You can find more info here: https://symfonycasts.com/screencast/symfony3-doctrine-relations/relation-query
In particular, the method findAllRecentNotesForGenus in GenusNoteRepository.
I think you can do something similar:
public function findPostsByCategoryData(CategoryData $cat)
{
return $this->createQueryBuilder('a')
->andWhere('a.catDatas = :cat')
->setParameter('cat', $cat)
->getQuery()
->execute();
}
#Alessandro Filira started in the right direction but forgot to take into account that this is a to-many relationship that you want to filter on.
Doctrine supports MEMBER OF operator, which works a bit like IN but in the opposite direction. It allows you to check if the specific value that you have is found as an element in the related group.
So the code should be like:
public function findPostsByCategoryData(CategoryData $cat): array
{
return $this->createQueryBuilder('a')
->andWhere(':cat MEMBER OF a.catDatas')
->setParameter('cat', $cat)
->getQuery()
->getResult();
}
See Doctrine Docs for MEMBER OF usage: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/dql-doctrine-query-language.html (use Ctrl+F and "member of")
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);
I have to change something in an existing Symfony2 project, but unfortunately I have never worked with Symfony2 before.
The database contains the following tables:
Location
========
id
....
Deal
========
id
deleted
...
deal_location
=================
deal
location
There is a Many-To-Many relationship between Deal and Location. This is mapped in the Location.orm.yml file like this:
manyToMany:
deals:
cascade: ['all']
targetEntity: Deal
mappedBy: locations
What I want to do is to exclude all deals which where deleted (deleted = 1) when reading the locations from the Database.
As I found out, this can be done in de LocationRepository class. In that class, I found the following function:
public function getFindAllByLatLonQueryBuilder($lat, $lon)
{
$qb = $this->createQueryBuilder('l');
$qb
->where('l.deleted IS NULL OR l.deleted = false')
->orderBy('(((ACOS(SIN((:lat*PI()/180)) *
SIN((l.latitude*PI()/180))+COS((:lat*PI()/180)) *
COS((l.latitude*PI()/180)) * COS(((:lon-l.longitude)*
PI()/180))))*180/PI())*60*1.1515*1.609344)', 'ASC')
->setParameter('lat', $lat)
->setParameter('lon', $lon)
;
return $qb->getQuery()->getResult();
}
I found a similar question and added the following line:
->leftJoin('l.deals', 'deals', 'WITH', 'deals.deleted = 0')
Unfortunately this doesn't work. How can I make this work?
Instead of having two conditions in your where clause I would leave only where('l.deleted IS NOT true AND deals.deleted IS NOT true') and would simply add the leftJoin clause.
Something like this should do the work:
public function getFindAllByLatLonQueryBuilder($lat, $lon)
{
$qb = $this->createQueryBuilder('l')
->leftJoin('l.deal', 'deal', 'WITH', 'deal.deleted IS NOT true') // you can also try 'deal.deleted != true'
->where('l.deleted IS NOT true')
->orderBy('(((ACOS(SIN((:lat*PI()/180)) *
SIN((l.latitude*PI()/180))+COS((:lat*PI()/180)) *
COS((l.latitude*PI()/180)) * COS(((:lon-l.longitude)*
PI()/180))))*180/PI())*60*1.1515*1.609344)', 'ASC')
->setParameter('lat', $lat)
->setParameter('lon', $lon)
;
return $qb->getQuery()->getResult();
}
try
->leftJoin('l.deals', 'deal', 'WITH', 'deal.deleted = 0')
you always have to join the object's name which in your case seems to be deals instead of deal given your yml.
Another thing which makes me wonder is why for location they check on deleted = null or false (see your code) but you check against deleted = 0. Are you sure this is the right check?
leftJoin() not help u, is only needed if u use data from it in query, it not exclude deals, because u in this query get only localization.
I think exclude localization with deleted deals not help to, because in some localization u will have deleted and not deals. It must be in other part of code where u get deals for localizations.
So u must first find where deals are get from db, to make it only take not deleted.
I have 3 tables: user, user_followers and blog_posts.
Users can follow other users and users are related to blog_post by user_id.
I need to get all blog posts that people I follow have written.
I tried something like:
$followedUsers = $user->getFollowedByMe(); //This one works
$posts = $entityManager->getRepository('<BundleHere>:BlogPosts')
->findBy(array('user_id' => $followedUsers));
And I tried a lot more variations but can't figure it out. Maybe someone knows a better way to search by multiple objects not just one.
You can use this kind of code in your BlogRepository.php in example.
public function getBlogPost($userId)
{
return $this
->_em
->createQueryBuilder('p')
->leftJoin('p.user', 'u')
->where('u.id = :id')
->setParameter('id', $userId)
->getQuery()
->getResult();
}
createQueryBuilder('p') will automatically create the select and from (select entity (post ?) from table).
Then, you can use it like this :
$posts = $entityManager->getRepository('<BundleHere>:BlogPosts')->getBlogPost($userId);
I can't give you the exact query because we don't have enough informations about your entities. But this way, you can write nice queries to get exactly what you want.
You can do:
$posts = $entityManager->getRepository('<BundleHere>:BlogPosts')
->createQueryBuilder('b')
->whereIn('b.user', $followedUsers)
->getQuery()
->getResult();
'user' should be the name of property used to hold the user in the Blogpost object.
So i figured it out (thanx guys for pointing me in the right direction with queryBuilder).
$followedByMe = $user->getFollowedByMe(); //Getting users i follow
$followedIds = $followedByMe
->map(function( $obj ) { //Using map method to create an array of id's for all followers
return $obj->getId(); //Value to put into array (in this case id)
})->toArray(); //Create an array and assign it to $followedIds variable
$qb = $em->getRepository('<BundleHere>:BlogPosts')->createQueryBuilder('b');
$posts = $qb->where($qb->expr()->in('b.user', $followedIds ))
->orWhere('b.user = :my_id')->setParameter('my_id', $user->getId()) //Get my posts too
->getQuery()
->getResult();