query-builder

Laravel join query with conditions

◇◆丶佛笑我妖孽 提交于 2019-11-27 09:54:24
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 id and the approved field is 1 and the course_id in that row has its own parent_id (from CourseInfo )

INNER JOIN Results from Select Statement using Doctrine QueryBuilder

孤者浪人 提交于 2019-11-27 09:37:04
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 has no effect on GROUP BY, and MySQL will return random results. There are a ton of answers out there (e

Subquery in doctrine2 notIN Function

亡梦爱人 提交于 2019-11-27 07:50:45
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') ->setParameter(1, $service); $qb = $this->_em->createQueryBuilder(); $qb->select('m') ->from('Custom

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

折月煮酒 提交于 2019-11-27 05:24:54
问题 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

How to select distinct query using symfony2 doctrine query builder?

人盡茶涼 提交于 2019-11-27 05:20:53
问题 I have this symfony code where it retrieves all the categories related to a blog section on my project: $category = $catrep->createQueryBuilder('cc') ->Where('cc.contenttype = :type') ->setParameter('type', 'blogarticle') ->getQuery(); $categories = $category->getResult(); This works, but the query includes duplicates: Test Content Business Test Content I want to use the DISTINCT command in my query. The only examples I have seen require me to write raw SQL. I want to avoid this as much as

Doctrine QueryBuilder delete with joins

余生长醉 提交于 2019-11-27 04:49:26
问题 I'm trying to use the Doctrine QueryBuilder to perform the following SQL query: DELETE php FROM product_hole_pattern php INNER JOIN hole_pattern hp ON php.hole_pattern_id = hp.id INNER JOIN hole_pattern_type hpt ON hp.hole_pattern_type_id = hpt.id WHERE php.product_id = 4 AND hpt.slug='universal'; I have this $qb = $this->entityManager->createQueryBuilder(); $query = $qb->delete('\SANUS\Entity\ProductHolePattern', 'php') ->innerJoin('php.holePattern', 'hp') ->innerJoin('hp.holePatternType',

How to safely use reserved SQL names?

你离开我真会死。 提交于 2019-11-27 02:18:26
I'm using Cakephp 3 using sqlserver as datasource server. I am sure there's no problem with my database connection.. as home.ctp prompts that I am connected to my database.. and I'm as well using migrations plugin to create my tables.. it seems like there is no problem working with these tools. but after I bake my MVC, I only got page full of errors.. for example $bin\cake bake all tests there are no errors I found and MVC are in its specific folder, testController.php, testTable, etc. and in browsers localhost:8765\tests but all I got is page of different errors.. Im seeing Error: SQLSTATE

Laravel: how to use derived tables / subqueries in the laravel query builder

萝らか妹 提交于 2019-11-27 01:55:42
问题 Edit: Though this question originally was specific for the query I'm describing underneath, the answer I got applies to almost all questions related to using derived tables / subqueries in Laravel Original Question: Lately I'm a bit stuck on the laravel query builder. It has some really nice features but I feel like it just isn't build for more complex database operations. This is the query I'm trying to build: select 'IFNULL(counted.product_count, 0) AS product_count', 'uncounted.value',

Symfony2 Doctrine querybuilder where IN

不打扰是莪最后的温柔 提交于 2019-11-27 01:49:51
问题 I losted trilion hours google this but none of the solutions were good. I have this querybuilder: $qb2=$this->createQueryBuilder('s') ->addSelect('u') ->innerJoin('s.user','u') ->where("u.id IN(:followeeIds)") ->andWhere('s.admin_status = false') ->setParameter('user', $user) ->setParameter('followeeIds', $arrayFolloweeIds) ->orderBy('s.id','DESC') ->setMaxResults(15) ; I could do a second query and then do like $qb->getDQL() but have would i cache the query ? Error: Invalid parameter number:

How to order by count in Doctrine 2?

夙愿已清 提交于 2019-11-27 01:37:42
问题 I'm trying to group my entity by a field (year) and do a count of it. Code: public function countYear() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('b.year, COUNT(b.id)') ->from('\My\Entity\Album', 'b') ->where('b.year IS NOT NULL') ->addOrderBy('sclr1', 'DESC') ->addGroupBy('b.year'); $query = $qb->getQuery(); die($query->getSQL()); $result = $query->execute(); //die(print_r($result)); return $result; } I can't seem to say COUNT(b.id) AS count as it gives an error,