query-builder

Laravel: how to add where clause using query builder?

[亡魂溺海] 提交于 2019-12-10 03:01:06
问题 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. 回答1: You may try

Using `DATE()` in Doctrine Querybuilder

可紊 提交于 2019-12-09 09:46:50
问题 I need to get all rows where DATE(a.when) matches the string 2014-09-30 . $builder = $this->em->createQueryBuilder(); $builder->select('a') ->from('Entity\Appointment', 'a') ->andWhere('a.when = :date') ->setParameter('date', $date); a.when is a full DATETIME ; :date is only a string (in DATE format). The following and variations didn't work: ->andWhere('DATE(a.when) = :date') Error: Expected known function, got 'DATE' What's the correct usage here? 回答1: This actually is a very common

How to select a specific field additionally to a tables default fields?

我的梦境 提交于 2019-12-09 03:36:13
问题 I an looking to use a JOIN to select data from a table and a view in CakePHP like so : $this->Annonces->find('all') ->where($arrFiltres) ->order($arrOrder) ->join([ 'table' => 'annonces_suivis', 'alias' => 'AnnoncesSuivis', 'conditions' => [...], ]); And would like to be able to select all the fields from the first table and som of the jointed table like so : ->select(['Annonces.*', 'AnnoncesSuivis.id']); But this creates a faulty SQL query. 回答1: .* isn't supported by the ORM Query, it will

Yii2: update field with query builder

感情迁移 提交于 2019-12-09 02:10:39
问题 How can I update field with query builder in Yii2? I can't find this in documentation. Thanks! UPD This is the solution: // UPDATE $connection = Yii::$app->db; $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute(); 回答1: Query builder is for select queries only (sum, max, count too). You should use other methods - AR or raw queries (https://github.com/yiisoft/yii2/blob/master/docs/guide/db-dao.md#basic-sql-queries) 回答2: Create command can be used directly as

Doctrine2: Limiting with Left Joins / Pagination - Best Practice

拟墨画扇 提交于 2019-12-08 17:19:15
问题 i have a big query (in my query builder) and a lot of left joins. So i get Articles with their comments and tags and so on. Let's say i have the following dql: $dql = 'SELECT blogpost, comment, tags FROM BlogPost blogpost LEFT JOIN blogpost.comments comments LEFT JOIN blogpost.tags tags'; Now let's say my database has more than 100 blogposts but i only want the first 10, but with all the comments of those 10 and all their tags, if they exist. If i use setMaxResults it limits the Rows. So i

cakephp 3 paginator doesn't work

倖福魔咒の 提交于 2019-12-08 06:45:37
问题 I'm curently using this query with Cakephp 3 for a little search engine $query_tweet = $this->Tweet ->find() ->select([ 'Users.username', 'Users.avatarprofil', 'Tweet.contenu_tweet', 'Tweet.created', 'Tweet.nb_commentaire', 'Tweet.nb_partage', 'Tweet.nb_like', ]) ->where([ "MATCH(Tweet.contenu_tweet) AGAINST(:search)" ]) ->where(['private' => 0]) // on ne cherche que les tweets publics ->bind(':search', '$search') ->order(['Tweet.created' => 'DESC']) ->contain(['Users']); This query works

How to create a join that uses a subquery?

牧云@^-^@ 提交于 2019-12-08 05:27:02
问题 How do you convert the following SQL Query to a CakePhp Find Query? SELECT a.id, a.rev, a.contents FROM YourTable a INNER JOIN ( SELECT id, MAX(rev) rev FROM YourTable GROUP BY id ) b ON a.id = b.id AND a.rev = b.rev I have tried the code below: return $model->find('all', [ 'fields' => $fields, 'joins' => [ [ 'table' => $model->useTable, 'fields' => ['id','MAX(rev) as rev'], 'alias' => 'max_rev_table', 'type' => 'INNER', 'group' => ['id'], 'conditions' => [ $model->name.'.id= max_rev_table.id

Symfony2 join query

故事扮演 提交于 2019-12-08 04:16:25
问题 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

How use LIKE in Yii query builder

南楼画角 提交于 2019-12-08 02:27:20
问题 How build this query with YIi query builder SELECT * FROM `table` WHERE type_item = 2 AND name_item LIke '%name%' I tried so return Yii::app()->db->createCommand() ->select('*') ->from('{{event_field_variants}}') ->where('type_item = :type AND name_item LIKE "%:substr%"', array(':type' => '2', ':substr' => 'name')) ->order('variant ASC') ->queryAll(); But this query get CdbcException. On Yii documentation are examples only with like or only with simple param. 回答1: ->where('name_item LIKE

Laravel - named subquery using Query Builder

大兔子大兔子 提交于 2019-12-08 02:12:01
问题 In MySQL subqueries documentation there's an exmaple of subquery: SELECT ... FROM (subquery) [AS] name ... Here's the raw query which I want to transform: select SUBQUERY_NAME.* from (select id, name from items) AS SUBQUERY_NAME Is there any way to do this in Laravel Query Builder without using DB::raw() ? 回答1: Unfortunatelly no. The Query Builder has its limitations and more complex queries are outside its scope, that's why DB::raw() is there. But, if you want to make it a little more