I need to make a sql query like this,
UPDATE org_mapping SET is_active = 1 WHERE (org_id = ? AND service_provider_id = ? )OR (org_id = ? AND service_provider_id = ?)
I tried this but its now working:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where('om.organization = ?1')->andWhere('om.serviceProvider = ?2')
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
When i am running, i am getting the following query:
UPDATE org_mapping SET is_active = 1 WHERE ((org_id = ? AND service_provider_id = ?) OR org_id = ?) AND service_provider_id = ?
Replace
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
With
->orWhere('om.organization = ?2 AND om.serviceProvider = ?1')
Try this:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where(
$qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?1')
,
$qb->expr()->eq('om.serviceProvider','?2')
),
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?2')
,
$qb->expr()->eq('om.serviceProvider','?1')
)
)
)
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
You should not do it.
If you do an UPDATE query, you're missing the whole point of using Doctrine, that is mapping objects to database rows, not just an abstraction to queries.
Instead, extract your objects, then do a cycle and perform actions on single objects, then flush after the cycle.
Related
How could I write this sql query on symfony query builder syntax?
Analysis, Region, Nature and Garden are Entities
SELECT * FROM `analysis`
INNER JOIN sample ON sample.id = analysis.sample_id
INNER JOIN region ON sample.region_id = region.id
INNER JOIN nature ON sample.nature_id = nature.id
INNER JOIN garden ON sample.garden_id = garden.id
WHERE sample.stateProduct = 'Origin'
AND analysis.status = '0'
AND region.name = 'Jangsu'
AND garden.name = 'North Tukvar'
AND nature.name = 'Thé Vert'
AND sample.sampleBio = '1'
AND sample.supplierCountry = 'Inde'
I tried this way but, I don't have error msg, but it's not same result as sql query.
public function countAnalysisByCriteria($stateProduct, $status, Nature $nature, Region $region, Garden $garden, $supplierName, $bio, $country, $startDate, $endDate){
$qb = $this->createQueryBuilder('analysis')
->addSelect('count(analysis) as result')
->innerJoin('analysis.sample', 'sample', 'WITH', 'analysis.sample = sample')
->innerJoin('sample.nature', 'nature', 'WITH', 'sample.nature = :nature')
->innerJoin('sample.region', 'region', 'WITH', 'sample.region = :region')
->innerJoin('sample.garden', 'garden', 'WITH', 'sample.garden = :garden')
->groupBy('result');
->andWhere('sample.stateProduct = :stateProduct');
->setParameter('stateProduct', $stateProduct);
->andWhere('sample.nature = :nature');
->setParameter('nature', $nature);
->andWhere('sample.region = :region');
->setParameter('region', $region);
->andWhere('sample.garden = :garden');
->setParameter('garden', $garden);
->andWhere('sample.dateReception BETWEEN :startDate AND :endDate');
$qb->setParameter('startDate', $startDate);
$qb->setParameter('endDate', $endDate);
}
return $qb->getQuery()->getArrayResult();
Your code snippet is completely wrong, try the following:
use Doctrine\ORM\Query\Expr\Join;
public function countAnalysisByCriteria(
$stateProduct,
$status,
Nature $nature,
Region $region,
Garden $garden,
$supplierName,
$bio,
$country,
$startDate,
$endDate
) {
$qb = $this->createQueryBuilder();
return $qb->select('count(analysis) as result')
->innerJoin('analysis.sample', 'sample', Join::WITH, 'analysis.sample = sample.id')
->innerJoin('sample.nature', 'nature', Join::WITH, 'sample.nature = nature.id')
->innerJoin('sample.region', 'region', Join::WITH, 'sample.region = region.id')
->innerJoin('sample.garden', 'garden', Join::WITH, 'sample.garden = garden.id')
->groupBy('result')
->andWhere('sample.stateProduct =:stateProduct')
->setParameter('stateProduct', $stateProduct)
->andWhere('sample.nature =:nature')
->setParameter('nature', $nature)
->andWhere('sample.region =:region')
->setParameter('region', $region)
->andWhere('sample.garden =:garden')
->setParameter('garden', $garden)
->andWhere('sample.dateReception BETWEEN :startDate AND :endDate')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->getQuery()
->getArrayResult();
}
DO NOT add spaces on assignments = : (wrong), =: (right)
Check your code properly, notice how I have removed some colon ; at some pieces because does not make sense have them there.
I'm trying to update a certain number of rows of my entity "Vehicule". I have no idea how could it work.
I'm actually trying to modify only two rows where direction= 5.This is the function I used in order to update.
public function ValidAction(\OC\UserBundle\Entity\User $direction) {
$qb = $this->getDoctrine()
->getRepository('CarPfeBundle:Vehicule')
->createQueryBuilder('v');
$q = $qb->update ('CarPfeBundle:vehicule v')
->set('v.direction', '?1')
->where('v.direction = ?2')
->setParameter(1, $direction)
->setParameter(2, 5)
->getQuery();
$p = $q->execute();
return $this->redirect($this->generateUrl('demandeveh_afficher'));
}
But the above code update all rows of my database. I need to update only two rows. Any help please?
Try to do this ;
public function ValidAction(\OC\UserBundle\Entity\User $direction) {
$qb = $this->getDoctrine()
->getRepository('CarPfeBundle:Vehicule')
->createQueryBuilder('v');
// $ids an array that contains all ids with your condition
$ids = $qb->select('v.id')
->where('v.direction = :direction')
->setParameter(
array(
'direction' => $direction
)
)
->getQuery()
->getResult();
$id1 = $ids[array_rand($ids)];
$id2 = $ids[array_rand($ids)];
//To be sure that $id1 is different from id2
while ($id1 == $id2) {
$id2 = $ids[array_rand($ids)];
}
$q = $qb->update ('CarPfeBundle:vehicule v')
->set('v.direction', ':val1')
->where('v.direction = :val2')
->andWhere('v.id IN (:id1, :id2)')
->setParameter(
array(
'val1' => $direction ,
'val2' => 5 ,
'id1' => $id1,
'id2' => $id2,
)
)
->getQuery();
$p = $q->execute();
return $this->redirect($this->generateUrl('demandeveh_afficher'));
}
With the above code I hope you can update only two rows and randomly.
Good luck !
While a solution like Houssem Zitoun suggested may work, why not use a subquery?
If you get the (like I did, if not, just skip the middle SELECT)
Error: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
go with this answer and something like (doc): - untested
UPDATE CarPfeBundle:Vehicule v
SET v.direction = ?1
WHERE v.direction IN
(SELECT * FROM (
SELECT v.direction
FROM CarPfeBundle:Vehicule v2
WHERE v.direction = ?2 LIMIT 2
)) AS sq
i have written a DQL query for my multiple search,working fine if i specify the all fields but if i keep one of the field blank it won't show me the result
$query = $em->createQuery
('SELECT d FROM EntityBundle:Doctor d WHERE d.name =:name AND d.degree =:degree AND d.area = :area_id AND d.sex = :sex AND
(
( YEAR(d.dob) BETWEEN :d2 AND :d1 ) OR
( YEAR(d.dob) BETWEEN :e2 AND :e1 ) OR
( YEAR(d.dob) BETWEEN :f2 AND :f1 )
)' )
->setParameter('name', $name)
->setParameter('degree', $degree)
->setParameter('area_id', $area)
->setParameter('sex', $sex)
->setParameter('d1', $this->p1)
->setParameter('d2', $this->p2)
->setParameter('e1', $this->q1)
->setParameter('e2', $this->q2)
->setParameter('f1', $this->r1)
->setParameter('f2', $this->r2)
->getResult();
i have tried setParameter('name', (is_null($name)? "IS NULL" : $name)) also but still there is no luck..
You can use QueryBuilder instead of DQL
$qb = $qb->select('d');
if($name !== null) {
$qb = $qb->where('d.name = :name')->setParameter(...);
}
...
$qb->getQuery()->getResult();
So this is how I use criteria:
$criteria = Criteria::create();
$criteria->andWhere($criteria->expr()->eq('author', /**/));
$criteria->orWhere($criteria->expr()->eq('author', /**/));
$criteria->orWhere($criteria->expr()->eq('author', /**/));
which results in this sql command:
WHERE
(
(
t0.author_id = ?
OR t0.author_id = ?
)
OR t0.author_id = ?
)
What if I need this?
WHERE
(
(
t0.author_id = ?
OR t0.author_id = ?
OR t0.author_id = ?
)
)
Is there any way how I can change the association of brackets?
Try this:
$criteria = Criteria::create();
$criteria->andWhere(
$criteria->expr()->orX(
$criteria->expr()->eq('author', /**/),
$criteria->expr()->eq('author', /**/),
$criteria->expr()->eq('author', /**/)
)
);
I am applying this query for below D6 query , not working ..dont know wat wrong i'm doing ....does innerjoin fails in some condition
$result = db_select('px_slides','s')
->join('node','n','s.vid = n.vid')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=')
->execute()->fetchObject();
drupal 6 query i have:
$result = db_query('
SELECT n.nid, n.vid, n.sticky, n.title, n.created, s.cid, s.ref_id, s.viewurl, s.specimen_type, s.tissue_type, s.body_site, s.test_type, s.algorithm, s.result
FROM {px_slides} s INNER JOIN {node} n ON n.vid = s.vid
WHERE n.status = 1 ')->execute();
You need to put your call to ->join() on a separate line altogether, as it doesn't return the query object:
$query = db_select('px_slides','s')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$query->join('node','n','s.vid = n.vid');
$result = $query->execute()->fetchObject();
The join method does not chain like that. You will have to do something like:
$query = db_select('px_slides','s')
->join('node','n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$result = $query->execute()->fetchObject();
Also you can use the to string magic method to see the query it is going to execute.
$query->__toString();
Try this...
$query = db_select('px_slides', 's');
$query->innerJoin('node,'n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'));
$query->fields('n',array('sticky','title'));
$query->condition('n.status','1');
$query->condition('s.cid','126');
$result= $query->execute()->fetchAll(PDO::FETCH_ASSOC);