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 7167891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:40:05+00:00 2026-05-28T14:40:05+00:00

This may seem like a rudimentary request, but I can’t seem to get it

  • 0

This may seem like a rudimentary request, but I can’t seem to get it to work, so I’m either missing something stupid, or am not understanding how it should be done. Thanks in advance.

I have two doctrine entities with a many-to-many relationship: Items and Categories. Which are joined by items_has_categories.

/**
* Item
*
* @Table(name="items")
* @Entity(repositoryClass="Entity\Repository\Item")
*/
class Item
{

....

 /**
 * @var Categories
 *
 * @ManyToMany(targetEntity="Categorie", inversedBy="items", cascade={"persist"})
 * @JoinTable(name="items_has_categories",
 *   joinColumns={
 *     @JoinColumn(name="items_id", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @JoinColumn(name="categories_id", referencedColumnName="id")
 *   }
 * )
 */
private $categories;

....
}

/**
* Categorie
*
* @Table(name="categories")
* @Entity(repositoryClass="Entity\Repository\Categorie")
*/
class Categorie
{
 .....

/**
 * @var Items
 *
 * @ManyToMany(targetEntity="Item", mappedBy="categories")
 */
private $items;

....
}

And what I’m trying to do is run a query to return all items that are in all of “x” number of categories – which I think/thought should be a SELECT with and AND clause:

class Item extends EntityRepository
{
    public function findItemsByCategories($categories)
    {

    $qString = 'SELECT j, t, c FROM Technique\Entity\Item j LEFT JOIN j.itemImages t JOIN j.categories c WHERE';

    $i = 0;
    foreach ($categories as $c)
    {
        $qString .= ' c.name = ?' . $i;

        if ($i < (count($categories)-1))
        {
            $qString .= ' AND';
        }
        $i++;
    }

    $query = $this->_em->createQuery($qString);
    $query->setParameters($categories);

    return $query->getResult();
}

That little bit of code has no errors and spits out the following DQL SELECT query (when 2 categories are sent in the array: $categories):

SELECT j, t, c FROM Technique\Entity\Item j LEFT JOIN j.itemImages t JOIN j.categories c WHERE c.name = ?0 AND c.name = ?1

This is always returning an empty array, i.e., no results. Even though in my DB, there are more than 20 items fitting the criteria: are in both categories.

Anyone see what I’m doing wrong here? Is this supposed to be an AND selection…? Basically I’d just like to know how to query a many-to-many relationship in Doctrine 2+, where there’s more than one value that must be met…

  • 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-28T14:40:07+00:00Added an answer on May 28, 2026 at 2:40 pm

    For anyone interested, I figured it out (painfully). Doctrine should really explain this better instead of the the one line they have on the DQL page….

    Basically it’s not an AND query, it’s a MEMBER OF AND query. For each category a MEMBER OF must be created and then added to the whole query with an AND:

    SELECT j, t FROM Entity\Item j
    LEFT JOIN j.itemImages t
    WHERE ?0 MEMBER OF j.categories AND ?1 MEMBER OF j.categories
    AND ?2 MEMBER OF j.categories, etc.
    

    That will return all items that are in all categories requested.

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

Sidebar

Related Questions

This may seem like a stupid question, but i can't quite remember how to
I realise that this may seem like a stupid request but I'm going to
This may seem like a dumb question, but can an app build with c#
This may seem like a stupid question, but what message do i send to
This may seem like a silly question but I can't figure it out. let's
this may seem like a stupid question, but it is stumping me nontheless. I'm
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
This may seem like a simple question, but I can't find the answer anywhere
This may seem like a stupid question, but I need to know where the
This may seem like an obvious thing, but I can't find it. When using

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.