Delete function using join in DQL - symfony

I'm trying to use the Doctrine QueryBuilder to perform the Delete function using query
I need to delete a record that is present in 2 tables ,
in TcTracks table the id is "id" and in TcWall teh id is "related_id"
my controller
public function deleteAction(Request $request){
$deleteQuery = $this->getDoctrine()
->getManager()
->createQueryBuilder('d')
->delete('TcPlayerBundle:TcTracks', 'd')
->innerJoin('TcprofileBundle:TcWall', 't', 'ON', 'd.id = t.related_id')
->where('d.id = :dId')
->setParameter('wId', $request->get('related_id'))
->setParameter('dId', $request->get('id'))
->getQuery();
$deleted = $deleteQuery->getResult();
$deleted->flush();
return $this->render('TcPlayerBundle:Default:all.html.twig',array(
'tracks' => $tracks
));
}
i need to delete same record in two tables , but its not performing for both tables, kindly help me
i'm getting error as
Invalid parameter number: number of bound variables does not match number of tokens

Remove the line below:
->setParameter('wId', $request->get('related_id'))
There is only :dId but no :wId.
Actually you did inner join by 'd.id = t.related_id', so related_id is not necessary to set any more.

You don't have wID parameter anywhere in query. In the same time you're using ->setParameter('wId', $request->get('related_id')) which is obsolete in this case. Try to remove this row and it should be fine.

Is one-to-many realation with your entities
TcPlayerBundle:TcTracks
and
TcprofileBundle:TcWall
if like that,you can do some like:
oneToMany:
cascade: [remove]
in your config 'yourentity.orm.yml' file..
hope hlep you!

Related

How to combine queryBuilder and raw queries on join table from many to many relation?

I have got following query
SELECT news.*, count(*) AS common_tags
FROM news
JOIN news_news_tag ON news.id = news_news_tag.news_id
WHERE news_tag_id IN
(SELECT news_tag_id FROM news_news_tag WHERE news_id = 2 )
AND news.id != 2 GROUP BY news.id ORDER BY common_tags DESC
What I want to achieve is to get hydrated news objects ordered by number of common tags with provided news id. News and Tag are many to many relation with news_news_tag join table.
News entity have got much more other relations. This is why I don't want to create a native query by myself to handle all other relations.
I would like to convert above query to use it with query builder. I wasn't able to use a DQL because my where statement uses a join (junction) table and I also need to use a join on that table.
All in all I have got 2 problems:
How can I create DQL subquery to select something from many to many join table)? If I know that I could do something like: ->where($queryBuilder->expr()->in('u.id', $mySubQueryAsDQL))
How to add that join statement that I could use news_tag_id in where statement?
If it is not possible I think that I would need to create two bidirectional one-to-many and many-to-one relations instead of many-to-many and work on special joining entity.
I finally came up with following solution. I decided to split that query into two separate ones. In first I can use just simple raw query as I need results only to where statement. Second query can be build using query builder in a normal way.
You have to only get rid of an extra count column at the end.
However if someone knows the solution how to use my first raw query directly inside query builder as a raw subquery for where statement please share with me.
public function getRelatedNews($newsId, $limit = 6)
$connection = $this->getEntityManager()->getConnection();
$sql = 'SELECT news_tag_id FROM news_news_tag WHERE news_id = :newsId';
try {
$stmt = $connection->prepare($sql);
} catch (DBALException $e) {
return [];
}
$stmt->execute(['newsId' => $newsId]);
$newsTagId = $stmt->fetchAll();
if (empty($newsTagId)) {
return [];
}
$newsTagId = array_column($newsTagId, 'news_tag_id');
$query = $this->createQueryBuilder('n')
->addSelect('COUNT(n.id) as common_tags_count')
->innerJoin("n.tags", "t")
->andWhere('t.id IN(:tagsId)')->setParameter('tagsId', array_values($newsTagId))
->andWhere('n.id != :newsId')->setParameter('newsId', $newsId)
->orderBy('common_tags_count', 'DESC')
->setMaxResults($limit)
->groupBy('n.id')
;
$results = $query->getQuery()->getResult();
$news = [];
foreach ($results as $result) {
$news[] = $result[0];
}
return $news;
}

Doctrine2 QueryBuilder select entity and count of associated entities

I'm having a huge problem with ORM QueryBuilder. What I need to do is:
I need to fetch order with count of its products and plenty of associated entities (associated with order), but I assume they're not relevant here. I also need to order result by that count.
Could anyone give me an example of how this can be achieved? I would like to avoid "inline" DQLs if possible.
You can get data via Doctrine Query Builder.
You are supposed to left join products from Order and then group by order id. You can have COUNT(product.id) in your select statement and use the alias in order by clause to make your orders sorted. Below is a small code snippet from Repository.
/**
* #return \Doctrine\ORM\Query
*/
public function getHotelAndRoomType()
{
$qb = $this->createQueryBuilder('order')
->select('partial order.{id, orderId} as order, count(product.id) as total_products_in_order')
->leftJoin('AppBundle:Product', 'product', 'WITH', 'product.order = order.id')
->groupBy('order.id')
->orderBy('total_products_in_order', 'DESC')
;
return $qb->getQuery()->execute();
}
Note : Code not tested.

Symfony2 join query

I have a table of videos and in that table I have field comment which contains id of comment in other table, now I used join query to get that in one query, but how do I get that comment?
Here is my code:
$Actions = $this->EntityManager()->getRepository('AppBundle:Video')
->createQueryBuilder('V')
->join('AppBundle:VideoComment', 'VC')
->where('V.videoId = :VideoID')
->andWhere('VC.videoId = :VideoID')
->setParameter('VideoID', $VideoID)
->getQuery()
->getResult();
How do I get the actual comment from that joined entity?
You can do what #cezar said earlier but with one little change: you have to define field to retrieve related entries from comments table.
So, your query might look like this:
$em = $this->get('doctrine.orm.entity_manager');
$videos = $em->createQuery('select v
from YourBundle:Video v
left join YourBundle:Comment c
where v.comment = c.id')
->getResult();
or you can do the similar stuff using query builder:
$videos = $em->createQueryBuilder('v')
->add('select', 'v, c')
->add('from', 'YourBundle:Video v')
->leftJoin('YourBundle:Comment', 'c')
->where('v.comment = c.id')
... // some other conditions if you need
->getQuery()
->getResult();
Both cases I described account for that Video and Comment entity might not be in formal relations (I mean their relations might not be described in your doctrine/orm file).
Here is one proposal:
<?php
namespace You\AppBundle\Repository; // You is your vendor name, AppBundle is your bundle
use Doctrine\ORM\EntityRepository;
class VideoCommentRepository extends EntityRepository
{
public function getVideoComment($VideoId)
{
$query = $this->getEntityManager()->createQuery(
'SELECT v FROM YouAppBundle:Video v LEFT JOIN v.comment c
WHERE v.id = :id'
)->setParameter('id', $VideoId);
return $query->getResult();
}
}
As you said you have a table 'video' and in that table there is a field 'comment' that contains the IDs of the comments. I suppose you have 'oneToMany' relation from 'video' to 'comment'. With this simple query you should be able to get all comments for a given VideoID. I didn't test this, but I think it should work. Try it out and adapt it as needed.

Symfony2 Select one column in doctrine

I'm trying to refine the query trying to select fewer possible values ​​..
For example I have an entity "Anagrafic" that contains your name, address, city, etc.,
and a form where I want to change only one of these fields, such as address.
I have created this query:
//AnagraficRepository
public function findAddress($Id)
{
$qb = $this->createQueryBuilder('r')
->select('r.address')
->where('r.id = :id')
->setParameter('id', $Id)
->getQuery();
return $qb->getResult();
}
there is something wrong with this query because I do not return any value, but if I do the query normally:
//Controller
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
Return the right value.
How do I do a query selecting only one column?
Since you are requesting single column of each record you are bound to expect an array. That being said you should replace getResult with getArrayResult() because you can't enforce object hydration:
$data = $qb->getArrayResult();
Now, you have structure:
$data[0]['address']
$data[1]['address']
....
Hope this helps.
As for the discussion about performance in comments I generally agree with you for not wanting all 30 column fetch every time. However, in that case, you should consider writing named queries in order to minimize impact if you database ever gets altered.
You can use partial objects to only hydrate one field and still return a object.
This worked for me:
$qb = $repository->createQueryBuilder('i')
->select('i.name')
->...
Use partial objects like this to select fields
$qb = $this->createQueryBuilder('r')
->select(array('partial r.{id,address}'))
...
Put your field names between the brackets

Doctrine2: using the querybuilder for many-to-many relationships

To search people in a system I have written a function in a repository.
This function receives an array with field names and criteria, and uses the querybuilder to formulate a valid DQL-statement.
Everything works fine, until the moment I want to make a search rule like:
"Find all people that belong to a certain domain."
...where the Person and Domain entities have a many-to-many relationship.
According to Symfony2 that relationship is defined correctly, and indeed everything works in Twig templates and controllers.
The function looks as follows:-
private function query($extra_conditions = null)
{
$qb = $this->_em->createQueryBuilder();
$query =
$qb->select('p')
->from('AppMainBundle:Person', 'p')
->leftjoin('AppMainBundle:Domain', 'd')
->where("p.firstname IS NOT NULL")
->andWhere("p.lastname IS NOT NULL")
->andWhere("p.function IS NOT NULL");
if ($extra_conditions!=null)
{
foreach ($extra_conditions as $condition)
{
#filter on firstname
if (key($condition)=='firstname')
$query = $query->andWhere('p.firstname LIKE ?1')
->setParameter(1, $condition['firstname']);
#filter on lastname
if (key($condition)=='lastname')
$query = $query->andWhere('p.lastname LIKE ?1')
->setParameter(1, $condition['lastname']);
#filter on gender
if (key($condition)=='gender')
$query = $query->andWhere('p.gender = '.$condition['gender']);
#filter on domain(s)
if (key($condition)=='domains')
$query = $query->andWhere('d.id IN ('.$condition['domains'].')');
#filter on town
if (key($condition)=='town')
$query = $query->andWhere('p.town LIKE ?1')
->setParameter(1, $condition['town']);
}
}
$query = $query->orderBy("p.lastname", "ASC");
$query = $qb->getQuery();
return $query->getResult();
}
There is no error, but whatever domains are given as search criteria; people from all domains are found and returned.
Domains are provided as an array like (2, 4).
What can I change to make it possible to find people belonging to certain domains?
After some trial-and-error I found two possible answers:
1) Apparently in the querybuilder you can refer to fields that live in join-tables, and are not actually part of an entity. Hence one solution would be:
#filter on domain(s)
if (key($condition)=='domains')
$query = $query->andWhere('p.domain_id IN ('.$condition['domains'].')');
...where domain_id lives in the join table "person_domain" created by Symfony2 unther the hood.
2) The inner join is now no longer needed. Using an inner join is possible as well, however it was written the wrong way and would not have worked anyway. It should have been written as:
->leftjoin('p:domain', 'd', 'ON p.id = d.id')
Hope that other people may benefit from the above.

Resources