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.
Related
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')
doctrine/symfony project:
i try to only get results if a reference is set.
so the colum for the relation can be filled with reference ids or it can be null if no reference is "set"... im not able to exclude the actual datasets with a null column
$qb = $this->em->createQueryBuilder();
$qb->select('am', 'lb')->from('MyBundle:Brand', 'am')
->leftJoin('MyBundle:XBuyer', 'lb')
->where('lb.id = am.buyer')
->andWhere('am.buyer IS NOT NULL');
another format i tried
$qb->select('am', 'lb')->from('MyBundle:Brand', 'am')
->leftJoin('MyBundle:XBuyer', 'lb')
->where('lb.id = am.buyer')
->andWhere('am.buyer != :buyer_id_val')
->setParameter('buyer_id_val', '');
also
$qb->select('am', 'lb')->from('MyBundle:Brand', 'am')
->leftJoin('MyBundle:XBuyer', 'lb')
->where('lb.id = am.buyer')
->andWhere($qb->expr()->isNotNull('am.buyer'));
am.buyer is the reference to another table - its actually buyer_id in the brands table
followed by
$data = $qb->getQuery()->execute(null, Query::HYDRATE_SCALAR);
no idea what im doing wrong here
the problem was that i'm still thinking in the context of the database (other projects) but in the case of using doctrine it was necessary to think in the context of an object - more specific if there is an relation between entities.
the actual IS NOT NULL expression wasnt the problem - the problem was the actual leftJoin on an entitiy instead of the relation-"name".
$qb->select('am', 'lb')->from('MyBundle:Brand', 'am')
->leftJoin('am.buyer', 'lb')
->where('am.buyer IS NOT NULL')
->andWhere('lb.id = am.buyer');
thanks guys for all the comments and support in this little timeframe
in my case,
$qb = $this->em->createQueryBuilder();
generated :
Too few arguments to function Doctrine\ORM\EntityRepository::createQueryBuilder()…
I finally wrote:
$qb = $this->em->createQueryBuilder('am');
$qb->select('am')
->leftJoin('am.buyer', 'lb')
->where('lb.id = am.buyer')
->andWhere('am.buyer IS NOT NULL');
->orderBy('am.id', 'ASC')
->setMaxResults(30)
->getQuery()
->getResult()
;
which works fine!
Nota: with Symfony 4.2.3
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'm really having a hard time converting this query to Doctrine:
public function get_order($id)
{
$this->db->select('*');
$this->db->from('tbl_orderline');
$this->db->join('tbl_order', 'tbl_order.orderNo = tbl_orderline.orderNo');
$this->db->join('tbl_customer', 'tbl_customer.customerNo = tbl_order.customerNo');
$this->db->join('tbl_product', 'tbl_product.productNo = tbl_orderline.productNo');
$this->db->where('tbl_order.orderNo', $id);
$query = $this->db->get();
return $query->result_array();
}
Could you please help me with this?
Any suggestions? Thanks
// if you're currently in custom Repository class then:
$qb = $this->createQueryBuilder('ol');
// if you're in a controller, then should be:
$qb = $em->getRepository('AppBundle:OrderLine')->createQueryBuilder('ol'); // Or whatever your bundle name is.
// query alias legend:
// ol - order line
// o - order
// c - customer
// p - product
// Your query builder should look something like this:
$qb
->addSelect('o, c, p')
->leftJoin('ol.order', 'o') // this is your relation with [order]
->leftJoin('o.customer', 'c') // this is your relation with [customer] from [order]
->leftJoin('ol.product', 'p') // this is your relation with [product] from [order line]
->where($qb->expr()->eq('ol.id', ':orderLineId')
->setParameter('orderLineId', $id)
->getQuery()
->getOneOrNullResult();
Note:
Since you did not provide any entity mappings, this is completely out of the blue. You are most likely going to change the properties in this query, but at the very least, it should give you the start you need.
Don't hesitate to ask, if you don't understand something.
Ive always found it easier to write in straight dql. Trying to use the querybuilder for complex stuff drives me nuts. This obviously requires you to have the correct relationships mapped in your entities, either annotated or with an orm file.
Obviously its hard to test what Ive put below, so you may need to debug a little.
$query = $this->getEntityManager()->createQuery(
'select orderline, order, customer, product
from BundleName:tlb_orderline orderline
join orderline.orderNo order
join order.customerNo customer
join orderline.productNo product
where order.orderNo = :id');
$query->setParameter('id' => $id);
return $query->getResult();
So recently I have been thinking and can't find a solution yet to this problem since my lack of development with doctrine2 and symfony query builder.
I have 2 tables:
Goals: id,user_id,target_value...
Savings: id,goal_id,amount
And I need to make a select from goals (all the informations in my table are from the goals table, except that I need to make a SUM(amount) from the savings table on each goal, so I can show the user how much did he saved for his goal)
This is the MySQL query:
select
admin_goals.created,
admin_goals.description,
admin_goals.goal_date,
admin_goals.value,
admin_goals.budget_categ,
sum(admin_savings.value)
from admin_goals
inner join admin_savings on admin_savings.goal_id=admin_goals.id
where admin_goals.user_id=1
group by admin_goals.id
It returns what I want but I have no idea how to implement it with doctrine or query builder, can you please show me an example in both ways?
I highly appreciate it !
I am going to assume you need this fields only and not your AdminGoals entity. On your AdminGoalsRepository you can do something like this:
public function getGoalsByUser(User $user)
{
$qb = $this->createQueryBuilder('goal');
$qb->select('SUM(savings.value) AS savings_value')
->addSelect('goal.created')
->addSelect('goal.description')
->addSelect('goal.goalDate')
->addSelect('goal.value')
->addSelect('goal.budgetCat') //is this an entity? it will be just an ID
->join('goal.adminSavings', 'savings', Join::WITH))
->where($qb->expr()->eq('goal.user', ':user'))
->groupBy('goal.id')
->setParameter('user', $user);
return $qb->getQuery()->getScalarResult();
}
Keep in mind that the return object will be an array of rows, each row is an associated array with keys like the mappings above.
Edit
After updating the question, I am going to change my suggested function but going to leave the above example if other people would like to see the difference.
First things first, since this is a unidirectional ManyToOne between AdminSavings and AdminGoals, the custom query should be in AdminSavingsRepository (not like above). Also, since you want an aggregated field this will "break" some of your data fetching. Try to stay as much OOP when you are not just rendering templates.
public function getSavingsByUser(User $user)
{
$qb = $this->createQueryBuilder('savings');
//now we can use the expr() function
$qb->select('SUM(savings.value) AS savings_value')
->addSelect('goal.created')
->addSelect('goal.description')
->addSelect('goal.goalDate')
->addSelect('goal.value')
->addSelect('goal.budgetCat') //this will be just an ID
->join('savings.goal', 'goal', Join::WITH))
->where($qb->expr()->eq('goal.user', ':user'))
->groupBy('goal.id')
->setParameter('user', $user);
return $qb->getQuery()->getScalarResult();
}
Bonus
public function FooAction($args)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
//check if user is User etc depends on your config
...
$savings = $em->getRepository('AcmeBundle:AdminSavings')->getSavingsByUser($user);
foreach($savings as $row) {
$savings = $row['savings_value'];
$goalId = $row['id'];
$goalCreated = $row['created'];
[...]
}
[...]
}
If you use createQuery(), then you can do something like this:
$dqlStr = <<<"DSQL"
select
admin_goals.created,
admin_goals.description,
admin_goals.goal_date,
admin_goals.value,
admin_goals.budget_categ,
sum(admin_savings.value)
from admin_goals
inner join admin_savings on admin_savings.goal_id=admin_goals.id
where admin_goals.user_id=1
group by admin_goals.id
DSQL;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery($dqlStr);
$query->getResult();
On the other hand, if you would like to use createQueryBuilder(), you can check this link: http://inchoo.net/dev-talk/symfony2-dbal-querybuilder/