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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:13:22+00:00 2026-06-04T04:13:22+00:00

I have two entities with a Unidirectional Many-to-One mapping. Here’s Product : use Doctrine\Common\Collections\ArrayCollection;

  • 0

I have two entities with a Unidirectional Many-to-One mapping.

Here’s Product:

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity
 * @Table(name="Product")
 * @gedmo:TranslationEntity(class="GPos_Model_Translation_ProductTranslation")
 */
class GPos_Model_Product extends GPos_Doctrine_ActiveEntity {
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue
     */
    protected $id;

    /**
     * @ManyToMany(targetEntity="GPos_Model_Category")
     * @JoinTable(name="products_categories",
     *      joinColumns={@JoinColumn(name="product_id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id")}
     *      )
     */
    protected $categories;

    public function __construct() {
        $this->categories = new ArrayCollection();
    }

    public function addCategory(GPos_Model_Category $category) {
        if (!$this->categories->contains($category))
            $this->categories->add($category);
    }
}

As you can see, $categories is an ArrayCollection of GPos_Model_Category entities.

Now what?
Well now I’d like to retrive all products that are in a given category and also all products that are NOT in a given category.

I’ve tried $products = GPos_Model_Product::findByCategories($category->getId());
but that only gave me
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1 and $category‘s ID is 1 so I guess it’s not the way to go. Anyone knows how to deal with that ?

Thank you!

  • 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-06-04T04:13:24+00:00Added an answer on June 4, 2026 at 4:13 am

    I finally found out how to select all products that are in a category thanks to https://stackoverflow.com/a/9808277/1300454.

    I tweaked his solution a bit so I could pass an array of Category entities and it would find all products that are within these categories. If you give more than one entity it will return any product that is in at least one of the given categories.

    Here’s my tweak (I located this function in my Product entity):

    /**
     *
     * Takes an array of GPos_Model_Category entities as parameter and returns all products in these categories
     * @param array $categories
     */
    public static function findByCategories($categories) {
        $categoryArray = array();
        foreach ($categories as $category) {
            array_push($categoryArray, $category->getId());
        }
        $qb = Zend_Registry::get('entityManager')->createQueryBuilder();
        $qb ->select('p')
        ->from('GPos_Model_Product', 'p')
        ->leftJoin('p.categories', 'c')
        ->andWhere($qb->expr()->in('c.id', $categoryArray));
    
        return $qb->getQuery()->execute();;
    }
    

    Here’s how you call it:

    $products_cat = GPos_Model_Product::findByCategories(array($category));
    

    In this case $category is an entity alone that’s why I put it in an array before giving it to the function.

    And here is the way you find products that are not in a given category or list of category:

    /**
     *
     * Takes an array of GPos_Model_Category entities as parameter and returns all products not in these categories
     * @param array $categories
     */
    public static function findByNotCategories($categories) {
        $categoryArray = array();
        foreach ($categories as $category) {
            array_push($categoryArray, $category->getId());
        }
        $qb = Zend_Registry::get('entityManager')->createQueryBuilder();
        $qb2 = Zend_Registry::get('entityManager')->createQueryBuilder();
        $qb->select('p')
        ->from('GPos_Model_Product', 'p')
        ->where($qb->expr()->notIn('p.id',
            $qb2->select('p2.id')
            ->from('GPos_Model_Product', 'p2')
            ->leftJoin('p2.categories', 'c')
            ->andWhere($qb->expr()->in('c.id', $categoryArray))
            ->getDQL()
        ));
    
        return $qb->getQuery()->execute();
    }
    

    This is actually using a subselect. I’m selecting all products id that are in the given category (that’s the subselect) then I’m selecting all products that are not in the result of the subselect. My job here is done!

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

Sidebar

Related Questions

I have two entities: product and tag connected by many-to-many relation. Here is a
I have two entities, Job and Language , in a many-to-one relationship. The mapping
I am trying to generate a unidirectional one-to-many mapping between two JPA entities. In
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have two entities, a User and Role with a one-to-many relationship from user
I have two entities, A and B. A has to-Many relation with B. One
This is situation I have: I have two entities with one-to-many relationship mapped like
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities that usually have one-to-many relationship, but in rare cases should
I have two entities, Entity1 and Entity2, on a one to many relationship. On

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.