Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:09:23+00:00 2026-05-26T22:09:23+00:00

I have two simple entities: My\Entity\Coupon: type: entity table: coupon id: id: type: integer

  • 0

I have two simple entities:

  My\Entity\Coupon:
    type: entity
    table: coupon
    id:
      id:
        type: integer
        generator:
          strategy: AUTO
    fields:
      name:
        type: string
        length: 255
            nullable: false
      value:
         type: integer
         default: 0

    My\Entity\CouponUsers:
      type: entity
      table: coupon_users
      id:
        id:
          type: integer
          length: 11
          nullable: false
          generator:
            strategy: AUTO
      fields:
            coupon_id:
          type: integer
          length: 11
          nullable: false
        user_id:
          type: integer

Now, I want to display simple stats for used coupons.
Running this SQL in phpMyAdmin:

SELECT c.name, count( * ) AS mycount
FROM coupon c
LEFT JOIN coupon_users u ON c.id = u.coupon_id
GROUP BY c.id
ORDER BY mycount DESC

Works fine as expected, returns:

name1    54
name2    120

Then, I try to do the same from Doctrine 2:

$queryBuilder = $this->_em->createQueryBuilder()
    ->select('c.name, COUNT(*) as co')
    ->from('My\Entity\Coupon', 'c')
    ->leftJoin('My\Entity\CouponUsers', 'u',
        \Doctrine\ORM\Query\Expr\Join::ON, 'c.id = u.coupon_id')
    ->where('u.coupon_id = c.id')
    ->groupBy('c.id');

$dql = $queryBuilder->getDQL();
var_dump($dql);

SELECT c.name,
   COUNT(*) as co 
FROM My\Entity\Coupon c
LEFT JOIN My\Entity\CouponUsers u
ON c.id = u.coupon_id
WHERE u.coupon_id = c.id
GROUP BY c.id

So far, so good. But when I do:

$queryBuilder->getQuery()->getResult();

I get error:

[Syntax Error] line 0, col 88: Error: Expected Doctrine\ORM\Query\Lexer::T_DOT, got 'u'

What’s wrong? How can I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:09:23+00:00Added an answer on May 26, 2026 at 10:09 pm

    Here is how the Doctrine manual would suggest coding your query:

    $querybuilder = $this->_em->createQueryBuilder()
       ->select(array('c.name', 'COUNT(c.id) as co')
       ->from('My\Entity\Coupon', 'c')
       ->leftJoin('c.users', 'u')
       ->groupBy('c.id');
    

    To perform this join in QueryBuilder, you’ll need a bidirectional association configured between the two entities, which you don’t seem to have set up yet.

    I use annotations for my entities, but I think the YAML will look something like this:

    My\Entity\Coupon:
        manyToOne:
            users:
              targetentity: CouponUsers
               inversed-by: coupon
    
    My\Entity\CouponUsers:
        onetoMany:
           coupon:
              targetEntity: Coupon
                 mapped-by: users
    

    If users can have many coupons, then the relationship would be bidirectional ManyToMany instead of manytoOne/oneToMany. The details on how to configure this can be found here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two simple models project t.string :title vote t.references :project t.integer
I have two simple tables in my database. A card table that contains Id,
Question is simple: I have two List List<String> columnsOld = DBUtils.GetColumns(db, TableName); List<String> columnsNew
I have two simple tables as described here... Table = Person PersonID (int, PrimaryKey)
CoreData beginner I have a simple problem with CoreData. My model has two entities,
I have a pretty simple console project with an entity model (containing two simple
I have a simple JOINED hierarchy of documents: CREATE TABLE Documents ( id INTEGER
I have two entities User and Group: @Entity public class User implements Serializable {
I have two simple while loops in my program that I feel ought to
I have two very simple, identical UITableViews in my app that are populated with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.