query-builder

Laravel: how to add where clause using query builder?

雨燕双飞 提交于 2019-12-05 02:06:09
I have this query, made using Laravel query builder: $rows = DB::table('elements')->where('type', 1); That corresponds to: "SELECT * from elements WHERE type=1" Now, in some cases I need to add a second Where to create a query like this: SELECT * from elements WHERE type=1 AND lang='EN' Using classic php I'd do something like: $sql = 'SELECT * from elements WHERE type=1'; if($var==true) $sql .= " AND lang='EN'"; How can I do that using Laravel Query Builder? Thank you. The Alpha You may try something like this $query = DB::table('elements'); $query->where('some_field', 'some_value'); //

symfony2 doctrine select IFNULL

馋奶兔 提交于 2019-12-05 00:57:30
Ok i have this code: SELECT IFNULL(s2.id,s1.id) AS effectiveID, IFNULL(s2.status, s1.status) AS effectiveStatus, IFNULL(s2.user_id, s1.user_id) as effectiveUser, IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount FROM statuses AS s1 LEFT JOIN statuses AS s2 ON s2.id = s1.shared_from_id WHERE s1.user_id = 4310 ORDER BY effectiveID DESC LIMIT 15 And i need to rewrite it to querybuilder. Something like that? $fields = array('IFNULL(s2.id,s1.id) AS effectiveID','IFNULL(s2.status, s1.status) AS effectiveStatus', 'IFNULL(s2.user_id, s1.user_id) as effectiveUser','IFNULL(s2.likes_count, s1

How to use wildcards in createQueryBuilder?

北战南征 提交于 2019-12-04 15:56:24
In my repository class i use: public function getItemsByTag($tag) { $qb = $this->createQueryBuilder('c') ->select('c') ->where('c.tags LIKE %bipolar%') ->addOrderBy('c.id'); return $qb->getQuery() ->getResult(); } But unfortunately this doesn't work.. Anybody knows how this can work? Or do I have to build a custom query without the QueryBuilder? Thanks! If you don't want to substitute your query with variables but use a static string you have to put the string in apostrophes. You have to use apostrophes instead of quotes! Otherwise the Doctrine2 Lexer will throw an Exception. So in your case

doctrine2 - querybuilder, empty parameters

本小妞迷上赌 提交于 2019-12-04 15:16:50
what can i do if the parameter has no value? my query: $query = $this->_em->createQueryBuilder() ->select('u') ->from('Users', 'u') ->where('u.id = ?1') ->andWhere('u.status= ?2') ->setParameter(1, $userid) ->setParameter(2, $status) ->getQuery(); return $query->getResult(); if theres no $status, then it doesnt display anything. i tried putting a condition before the query to check if its null but what value can i set $status iif theres no status set The query builder is exactly there for building conditional queries. You could do: $qb = $this->_em->createQueryBuilder(); $query = $qb->select(

How do I add a 'where not' to a QueryBuilder Query

痞子三分冷 提交于 2019-12-04 14:35:49
问题 I want to search the entire content tree but not specific tress that have a 'Do Not Search' property at their base. The Query Builder API page does not reference anything besides AND and OR. Is it possible to exclude paths from the search or can I only explicitly include paths? The first three lines are "/content AND /content/path/es". I want "/content AND NOT(/content/path/es)" map.put("group.1_path", "/content"); map.put("group.2_path", "/content/path/es"); map.put("group.p.or","false"); I

Help creating a flexible base 'find' method in a service class using the DRY principle

孤街醉人 提交于 2019-12-04 13:14:20
问题 For years now I've been reimplementing the same code over and over (with evolution) without finding some method of cleanly, and efficiently, abstracting it out. The pattern is a base 'find[Type]s' method in my service layers which abstracts select query creation to a single point in the service, but supports the ability to quickly create easier to use proxy methods (see the example PostServivce::getPostById() method way below). Unfortunately, so far, I've been unable to satisfy these goals:

Laravel whereIn OR whereIn

让人想犯罪 __ 提交于 2019-12-04 08:55:06
问题 I'm making a products search by filters: My code: ->where(function($query) use($filter) { if(!empty($filter)){ foreach ($filter as $key => $value) { $f = explode(",", $value); $query-> whereIn('products.value', $f); } } }) Query: and (products.value in (Bomann, PHILIPS) and products.value in (red,white)) But I need: and (products.value in (Bomann, PHILIPS) OR products.value in (red,white)) Is there something similar in Laravel: orWhereIn 回答1: You could have searched just for whereIn function

laravel query php how to get max value within a range

泪湿孤枕 提交于 2019-12-04 07:53:25
hello how do i get the max value of scores, where column ID range starts at 3-5 example table I want to get the max value of scores, where column ID ranging from 3-5 , please help, what I have done so far: $max_scores_table= DB::table('scores_table') ->where('id', '>', 2) ->max('score'); another problem is when i have a decimal points in the table when I used the max() function it gets the ID=5, which has a Score of 4.5, instead of ID=4 with a value of 4.6, tnx in advance Try to use whereBetween hope this works: $max_scores_table= DB::table('scores_table') ->select(DB::raw('MAX(score) FROM

Query Builder / DQL not working with INNER JOIN - Syntax Issue

对着背影说爱祢 提交于 2019-12-04 07:01:14
I know I have a syntax isse here however I cant figure it out. I'm trying to do a SELECT and INNER JOIN of 5 tables but Symfony is complaining about the Entities in the JOIN are used before being defined. Actual error is as follows: [Semantical Error] line 0, col 121 near 'I ON C.id = ': Error: Identification Variable MySiteBundle:Items used in join path expression but was not defined before. Here is the PHP code. Note: I have shortened this query to two columns, two tables, and one join to keep the question simple and show my point. The actual query is much longer and is producing the same

doctrine 2 - query builder conditional queries… If statements?

我只是一个虾纸丫 提交于 2019-12-04 05:20:15
My query is doctirne 2. i have a status field in users, private or public. i want to be able to run this query and display all comments where status= public and private only if userid = current logged in user id(which i know, $loggerUserVarID) $q = $this->em->createQueryBuilder() ->select('c') ->from('\Entities\Comments', 'c') ->leftJoin('c.users', 'u') ->where('status = public') ??? display all public comments but private if it belpongs to the logged in user.? ->setParameter(1, $loggerUserVarID) ->getQuery(); at the moment, i am using an if statement after i get thee results, is there a way