query-builder

Laravel 5.1 Create or Update on Duplicate

柔情痞子 提交于 2019-11-27 01:17:28
问题 In Laravel 5.1, for MySQL insert, I want to see if the record already exists and update on duplicate or create new if none exists. I have already searched SO where the answers were for old laravel versions. In one of the old topics it said that a new updateOrCreate() method was added in core last year. But when I try that, I am getting the error: Integrity constraint violation: 1062 Duplicate entry '1' for key 'app_id' This is the query I use: AppInfo::updateOrCreate(array( 'app_id' =>

How to paginate associated records?

£可爱£侵袭症+ 提交于 2019-11-26 23:42:57
问题 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

Select entries between dates in doctrine 2

纵然是瞬间 提交于 2019-11-26 22:26:48
I will go insane with this minimal error that I'm not getting fix. I want to select entries between two days, the examples below ilustrate all my fails: opt 1. $qb->where('e.fecha > ' . $monday->format('Y-m-d')); $qb->andWhere('e.fecha < ' . $sunday->format('Y-m-d')); result (0 entries): SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2 FROM reservacion r0_ WHERE (r0_.fecha > 2012 - 07 - 16) AND (r0_.fecha < 2012 - 07 - 22) opt 2 $qb->add('where', 'e.fecha between 2012-01-01 and 2012-10-10'); result (0 entries): SELECT r0_.id_reservacion AS id

How to use WHERE IN with Doctrine 2

倾然丶 夕夏残阳落幕 提交于 2019-11-26 21:33:46
I have the following code which gives me the error: Message: Invalid parameter number: number of bound variables does not match number of tokens Code: public function getCount($ids, $outcome) { if (!is_array($ids)) { $ids = array($ids); } $qb = $this->getEntityManager()->createQueryBuilder(); $qb->add('select', $qb->expr()->count('r.id')) ->add('from', '\My\Entity\Rating r'); if ($outcome === 'wins') { $qb->add('where', $qb->expr()->in('r.winner', array('?1'))); } if ($outcome === 'fails') { $qb->add('where', $qb->expr()->in('r.loser', array('?1'))); } $qb->setParameter(1, $ids); $query = $qb-

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

好久不见. 提交于 2019-11-26 21:23:00
问题 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. 回答1: 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() -

Join subquery with doctrine 2 DBAL

我们两清 提交于 2019-11-26 16:55: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(

Group by not working - Laravel

主宰稳场 提交于 2019-11-26 16:14:24
问题 I'm not able to run this simple query in Laravel 5.3 $top_performers = DB::table('pom_votes') ->groupBy('performer_id') ->get(); It gives me: SQLSTATE[42000]: Syntax error or access violation: 1055 'assessment_system.pom_votes.id' isn't in GROUP BY (SQL: select * from `pom_votes` group by `performer_id`) However if I copy raw query from the error and fire directly in PhpMyAdmin, it works fine. I have already checked this: https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset

Laravel join query with conditions

旧街凉风 提交于 2019-11-26 14:55:35
问题 I have 4 tables. User table: id col1 col2 CoursesAssigned table: id user_id course_id approved CourseInfo table: id parent_id CourseParents table: id start_date end_date I think the table names and its column names are self explanatory. I have 2 kinds of users - i) assigned ii) unassigned. I show them in 2 different pages. While showing the assigned students I need those students from users table for each of whom there is at least one row in CoursesAssigned table where user_id is his own user

INNER JOIN Results from Select Statement using Doctrine QueryBuilder

我只是一个虾纸丫 提交于 2019-11-26 14:47:06
问题 Can you use Doctrine QueryBuilder to INNER JOIN a temporary table from a full SELECT statement that includes a GROUP BY ? The ultimate goal is to select the best version of a record. I have a viewVersion table that has multiple versions with the same viewId value but different timeMod. I want to find the version with the latest timeMod (and do a lot of other complex joins and filters on the query). Initially people assume you can do a GROUP BY viewId and then ORDER BY timeMod , but ORDER BY

Subquery in doctrine2 notIN Function

依然范特西╮ 提交于 2019-11-26 13:51:35
问题 I'd like to select members who are not in specific service. I have 3 tables : membre service membre_service (relation between membre and service ) I'm using doctrine 2 and in SQL my query is : SELECT m.* FROM membre m WHERE m.`id` NOT IN ( SELECT ms.membre_id FROM membre_service ms WHERE ms.service_id != 29 ) In Doctrine, I do : $qb = $this->_em->createQueryBuilder(); $qb2 = $qb; $qb2->select('m.id') ->from('Custom\Entity\MembreService', 'ms') ->leftJoin('ms.membre', 'm') ->where('ms.id != ?1