query-builder

Symfony 2: INNER JOIN on non related table with doctrine query builder

穿精又带淫゛_ 提交于 2019-11-28 04:41:58
I'm trying to build a query with the doctrine query builder which joins a non related table like this: $query = $this->createQueryBuilder('gpr') ->select('gpr, p') ->innerJoin('TPost', 'p') ->where('gpr.contentId = p.contentId') But this doesn't work. I still get an error: Error: Identification Variable TPost used in join path expression but was not defined before. I searched for this error message and everybody answered to use the table alias + attribute like p.someAttribute. But the table I want to join isn't related in the table I start my select from. As a normal mysql query i would write

Laravel: performing some task on every insert/update when using Query Builder or Eloquent ORM

半腔热情 提交于 2019-11-28 03:56:10
问题 The Problem I would like to automatically add created_by and modified_by fields to every insert/update to a database table in Laravel 4, regardless of whether I am using Eloquent or Query Builder. However, not all my tables have these fields so any solution will have to check these columns exist before adding. Attempted Solution I have extended the Illuminate\Database\Eloquent\Model class and written an overwrite method save() in order to add some additional meta data fields for every record

How to generate SQL function calls with the CakePHP query builder?

半城伤御伤魂 提交于 2019-11-28 03:55:10
问题 I have a fullname column for authors and would like to extract the surname into another column. I do that with the following raw SQL: SELECT name, SUBSTRING_INDEX(`name`, ' ', -1) AS `surname` FROM qr.authors; Output: Under "Using SQL Functions" the Cookbook says: In addition to the above functions, the func() method can be used to create any generic SQL function such as year, date_format, convert, etc. But how can I create this SUBSTRING_INDEX function through the func() method so that I can

How to paginate associated records?

喜你入骨 提交于 2019-11-28 02:25:22
Products belongsToMany Categories and Categories hasMany Products , inside my Product view I'm showing a list of all it's categories but I want to paginate or limit these results. My current code on ProductsController is: $product = $this->Products ->findBySlug($slug_prod) ->contain(['Metas', 'Attachments', 'Categories']) ->first(); $this->set(compact('product')); I know I need to set $this->paginate() to paginate something but I can't get it working to paginate the categories inside the product. I hope you guys can understand me. UPDATE: Currently I have this going on: $product = $this-

How to create a `IN` clause in CakePHP query?

蓝咒 提交于 2019-11-27 23:06:04
How do you make it where in clause in new CakePHP? I'm trying: $restaurants->find()->where(['id' => $r_ids])->all(); Where $r_ids is array of ids I need in my query, but this doesn't work as expected. With CakePHP 3.x it's now necessary to either indicate the data type, which for an array of values need to have [] appended to the type: $query = $articles ->find() ->where(['id' => $ids], ['id' => 'integer[]']); or to explicitly make use of the IN keyword: $query = $articles ->find() ->where(['id IN' => $ids]); See also Cookbook > Database Access & ORM > Query Builder > Automatically Creating IN

Laravel Query Builder where max id

爱⌒轻易说出口 提交于 2019-11-27 21:30:14
问题 How do I accomplish this in Laravel 4.1 Query Builder? select * from orders where id = (select max(`id`) from orders) I tried this, working but can't get the eloquent feature. DB::select(DB::raw('select * from orders where id = (select max(`id`) from orders)')); Any idea to make it better? 回答1: You should be able to perform a select on the orders table, using a raw WHERE to find the max( id ) in a subquery, like this: \DB::table('orders')->where('id', \DB::raw("(select max(`id`) from orders)"

doctrine 2 query builder and join tables

对着背影说爱祢 提交于 2019-11-27 17:25:57
问题 I'm trying to get all comments for each post in my home page return $this->createQueryBuilder('c') ->select('c') ->from('Sdz\BlogBundle\Entity\Commentaire' ,'c') ->leftJoin('a.comments' ,'c')->getQuery()->getResult() ; but I'm getting this error [Semantical Error] line 0, col 58 near '.comments c,': Error: Identification Variable a used in join path expression but was not defined before. PS : The mapping is correct because I can see the page article with its comments. 回答1: In case this is

Join subquery with doctrine 2 DBAL

只谈情不闲聊 提交于 2019-11-27 14:49:14
I'm refactoring a Zend Framework 2 application to use doctrine 2.5 DBAL instead of Zend_DB (ZF1). I have the following Zend_Db query: $subSelect = $db->select() ->from('user_survey_status_entries', array('userSurveyID', 'timestamp' => 'MIN(timestamp)')) ->where('status = ?', UserSurveyStatus::ACCESSED) ->group('userSurveyID'); $select = $db->select() // $selectColNames contains columns both from the main query and // the subquery (e.g. firstAccess.timestamp AS dateFirstAccess). ->from(array('us' => 'user_surveys'), $selectColNames) ->joinLeft(array('firstAccess' => $subSelect), 'us

Find nearest points with MySQL from points Table

佐手、 提交于 2019-11-27 14:10:58
问题 I have a DB Schema like this (from this tutorial by Google) - So the actual points in a graph for them is like this- What I want is to find points near a given point (by point_id ) point ordered by distance Location of a point (x,y) is ( point_x , point_y ) in DB I want to solve it with MySQL because my DB is already in MySQL. Update- Finding distance of 2 points is so easy like this- I want to sort on distance with MySQL. Re- For removing the confusions, I want the points inside the circle,

Laravel 5.2 - pluck() method returns array

眉间皱痕 提交于 2019-11-27 12:53:23
问题 I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me: The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck . The method signature remains the same. That's ok, rename refactoting from lists() to pluck() isn't a problem. But what with useful pluck() method which was in L5.0 and L5.1? From the 5.0 documentation: Retrieving A Single Column From A Row $name = DB::table('users')->where(