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();
Related
The products have the attribute Tempo. This is a text field in which to enter the number.
How to sort products by the minimum and maximum attribute value?
using such url ?filtering=1&filter_min-tempo=100&filter_max-tempo=150
Is it possible to create an attribute of type integer?
All attributes are stored as text, but you can cast them to the type you need.
Look at this code:
function isequal($v1,$v2) {
return intval($v1) == intval($v2);
}
function filter_loop_shop_post_in( $array ) {
if ( !array_key_exists('min_tempo',$_GET) && !array_key_exists('max_tempo',$_GET) ) return $array;
global $wpdb;
$min = isset( $_GET['min_tempo'] ) ? floatval( $_GET['min_tempo'] ) : 0;
$max = isset( $_GET['max_tempo'] ) ? floatval( $_GET['max_tempo'] ) : 9999999999;
$query = "
SELECT DISTINCT
tr.object_id
FROM
{$wpdb->term_relationships} AS tr
LEFT JOIN
{$wpdb->term_taxonomy} AS tt
ON
tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN
{$wpdb->terms} AS t
ON
tt.term_id = t.term_id
WHERE
tt.`taxonomy` = 'pa_tempo' AND CAST(t.name AS DECIMAL(10, 1)) BETWEEN $min AND $max";
$raw_results = $wpdb->get_results( $query );
if (!sizeof($raw_results)) return $array;
$results = array();
foreach ($raw_results as $res) {
$results[] = intval($res->object_id);
}
if (!sizeof($array)) return $results;
return array_uintersect($results, $array, 'isequal');
};
add_filter( 'loop_shop_post_in', 'filter_loop_shop_post_in', 10, 1 );
It works for me in WooCommerce 2.6.14.
pls help i get this error
Cannot count query which selects two FROM components, cannot make distinction
when i try to break my query to add conditional statement
i have read this
KnpPaginatorBundle/Resources/doc/manual_counting.md and i arrived at this
public function findCategoryProduct($category,$minPrice=null,$maxPrice=null,$gender=null)
{
$countgb = $this->createQueryBuilder('1')
->select('count(p)')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$count = $countgb->getQuery()->getSingleScalarResult();
$query = $this->createQueryBuilder('1')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$query ->getQuery()
->setHint('knp_paginator.count', $count);
return $query;
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$request->query->getInt('page', 1),10,array('distinct' => false));
and i still get the error
Hi I advise you to use subquery like :
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
$ids = $query->getResult();
or with expr:
$query->andWhere($query->expr()->notIn('c.id', $subquery->getDQL()));
some documentation here
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 the following tables in mysql with given relationships and same has been define in the symfony2. I want to achieve the following query which I wrote it down below. can some one help me how can I write it in querybuilder?
unitids(id,unitid,databaseid)
Mappaths(id,ref_unitid2(forigen key for unitids id) ,ref_unitid1 (forigen key for unitids id),nomal value)
traveltime(id,ref_map_path(forigen key for mappaths id), traveltime,noobjs,ondate)
my mysql query is like this :
SELECT t.ID,t.ondate, un.databaseid as source1,
un1.databaseid as desitnation, t.traveltime, t.noobjs
FROM test.traveltime t
left join test.mappaths p on t.ref_map_path = p.ID
left join test.unitids un on (p.ref_unitids1 = un.id )
left join test.unitids un1 on (p.ref_unitids2= un1.id)
where un.databaseid=50 and un1.databaseid =1 limit 1;
which give me each one row of source and destination of and objects etc like this :
in symfony2 when i run this query
$query = $em->createQueryBuilder();
$results = $query->select('un.databaseid,un1.databaseid')
->from('ApiMapBundle:Traveltime', 't')
->leftJoin('t.refMapPath', 'p')
->leftJoin('p.refUnitids2', 'un')
->leftJoin('p.refUnitids1', 'un1')
->where('un.databaseid = :bdatabaseid1')
->setParameter('bdatabaseid1', 2)
->andwhere('un1.databaseid = :bdatabaseid2')
->setParameter('bdatabaseid2',1)
//->setMaxResults(1)
->getQuery()
->getResult();
it give me output like
Array ( [0] => Array ( [databaseid] => 1 ) [1] => Array ( [databaseid] => 1 ))
but instead it should give me
Array ( [0] => Array ( [databaseid] => 1 ) [1] => Array ( [databaseid] => 2 ))
How can i achieved this above output?????
Assumed that you are writing code in Repository and you also defined relations in your Entities
$em = $this->getEntityManager();
$query = $em->getRepository('BUNDLE:traveltime')->createQueryBuilder('t');
$query->select('t.Id,..')
->leftJoin('t.ref_map_path','p')
->leftJoin('p.ref_unitids1','un')
->leftJoin('p.ref_unitids2','un1')
->where('un.databaseid = :bdatabaseid')
->setParameter('bdatabaseid',1)
->orWhere('un1.databaseid = :bdatabaseid1')
->setParameter('bdatabaseid1',2);
How do I write this "WHERE" condition in a way which is compatible with Doctrine?
( ( `translations_es_google`.`translation_date` < `translations_masters`.`translation_date` ) OR ( `translations_es_google`.`translation_date` is null ) )
What I have tried (unsuccessfully) are:
$query1
->andWhere('((t.translationDate < m.translationDate) OR (t.translationDate is NULL))');
$query1
->andWhere('(t.translationDate < m.translationDate) OR (t.translationDate is NULL)');
Table alias for the Doctrine query are
t represents the table translations_es_google
m represents the table translations_masters
When I remove this "andWhere" condition the rest of the QueryBuilder query executes as expected.
The query I am trying to execute is
SELECT
`translations_masters`.`translation_key` , `translations_masters`.`text_domain` , `translations_masters`.`translation_date`
FROM
`translations_masters`
LEFT JOIN
`translations_es_google` ON ( ( `translations_masters`.`text_domain` = `translations_es_google`.`text_domain` ) AND ( `translations_masters`.`translation_key` =`translations_es_google`.`translation_key` ) )
WHERE
`translations_masters`.`language_iso` NOT LIKE 'es-Google' AND ( ( `translations_es_google`.`translation_date` < `translations_masters`.`translation_date` ) OR ( `translations_es_google`.`translation_date` is null ) )
GROUP BY
`translations_masters`.`translation_key` , `translations_masters`.`text_domain`
ORDER BY
`translations_masters`.`translation_date` ASC
As requested the QueryBuilder version of this query is:
$query1 = $this->entityManager
->createQueryBuilder()
->select('m.textDomain , m.translationKey , m.translationDate , m.translationDate AS translationMasterDate , t.translationDate')
->from(
'AMDatabase\Entity\TheVerse\Translations' . $explode1 . $explode2,
't'
)
->leftJoin(
'AMDatabase\Entity\TheVerse\TranslationsMasters',
'm',
Join::WITH,
'(m.textDomain = t.textDomain) AND (m.translationKey = t.translationKey)'
)
->groupBy('m.textDomain , m.translationKey')
->orderBy(
'm.translationDate',
'ASC'
)
->setMaxResults('1');
$query1
->andWhere(
$query1->expr()
->notLike(
'm.languageIso',
':languageIso'
)
)
->setParameter(
'languageIso',
$value
);
$query1->andWhere(
$query1->expr()->orX(
't.translationDate < m.translationDate',
't.translationDate IS NULL'
)
);
$result1 = $query1->getQuery()
->getArrayResult();
The output of $query1->getQuery()->getSQL() is
SELECT
t0_.text_domain AS text_domain_0, t0_.translation_key AS translation_key_1, t0_.translation_date AS translation_date_2, t0_.translation_date AS translation_date_3, t1_.translation_date AS translation_date_4
FROM
thev1010_theverseoftheday.translations_es_google t1_
LEFT JOIN
thev1010_theverseoftheday.translations_masters t0_ ON ((t0_.text_domain = t1_.text_domain) AND (t0_.translation_key = t1_.translation_key))
WHERE
t0_.language_iso NOT LIKE ? AND (t1_.translation_date < t0_.translation_date OR t1_.translation_date IS NULL)
GROUP BY
t0_.text_domain, t0_.translation_key
ORDER BY
t0_.translation_date ASC
LIMIT 1
The ? value is 'es-Google'
The output of $query1->getQuery()->getDQL(); is
SELECT
m.textDomain , m.translationKey , m.translationDate , m.translationDate AS translationMasterDate , t.translationDate
FROM
AMDatabase\Entity\TheVerse\TranslationsEsGoogle t
LEFT JOIN
AMDatabase\Entity\TheVerse\TranslationsMasters m WITH (m.textDomain = t.textDomain) AND (m.translationKey = t.translationKey)
WHERE
m.languageIso NOT LIKE :languageIso AND (t.translationDate < m.translationDate OR t.translationDate IS NULL)
GROUP BY
m.textDomain , m.translationKey
ORDER BY
m.translationDate ASC
The ? value is 'es-Google'
How about orX expression?
You can try it this way:
$query1->andWhere(
$query1->expr()->orxX(
't.translationDate < m.translationDate',
't.translationDate IS NULL'
)
);
orX gives you the ability to add multiple conditions, separated by comma.
You can also separate your orX condition to its own variable and pass everything added to it to andWhere condition.