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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:30+00:00 2026-05-26T14:04:30+00:00

I have entity Product and entity Subcategory : Subcategory.php namespace Project\Entities; /** * Subcategory

  • 0

I have entity Product and entity Subcategory:

Subcategory.php

namespace Project\Entities;    

/**
 * Subcategory 
 * @Entity
 * @Table(name="subcategories")
 *
 */
class Subcategory {
/**
 * 
 * @Id 
 * @GeneratedValue 
 * @Column(type="integer")
 * @var integer $id
 */
public $id;

/**
 * @Column(type="string")
 * @var string $name
 */
public $name;
}

Product.php

namespace Project\Entities;

/**
 * 
 * Product 
 * @Entity
 * @Table(name="products")
 */
class Product {
    /**
     * 
     * @Id 
     * @GeneratedValue 
     * @Column(type="integer",length=16)
     * @var integer $id
     */
    public  $id;

    /**
     * 
     * @Column(type="integer",length=3)
     * @var integer $id_subcat
     */
    public $id_subcat;

    /**
     * 
     * @Column(type="string")
     * @var integer $name
     */
    public $name;


    /**
     * 
     * @Column(type="string")
     * @var integer $available
     */
    public $available;
}

I would like to get all Subcategories that have Products available field equal 1.
Ergo. Get Subcategories that have only available products in them.

I know how to write SQL query for that but I don’t know how to achieve this in DQL.

Should I provide some additional annotation?

  • 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-26T14:04:30+00:00Added an answer on May 26, 2026 at 2:04 pm

    You have to begin by declaring the relationship in your entities. I am going to assume that your Subcategory can have many Products (one-to-many):

    I haven’t tested this code obviously, so I apologize if there are some errors, this is off the top of my head mostly.

    /**
     * Subcategory 
     * @Entity
     * @Table(name="subcategories")
     *
     */
    class Subcategory {
        /**
         * @OneToMany(targetEntity="Project\Entities\Product", mappedBy="id_subcat")
         * @var Products[]
         */
        protected $products;
    
        public function __construct()
        {
                 $this->products = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        // Leaving out rest of class for simplicity...
    }
    
    
    /**
     * 
     * Product 
     * @Entity
     * @Table(name="products")
     */
    class Product {
    
        // Rest of class....
    
        /**
         * @ManyToOne(targetEntity="Project\Entities\Subcategory", inversedBy="products")
         * @JoinColumn(name="id_subcat", referencedColumnName="id")
         */
         protected $id_subcat = null;
    
    }
    

    ** Then to query, in SF2, it would be like this:

    /* var \Doctrine\ORM\EntityManager $em */
    $dql = "SELECT p FROM Project\Entities\Product p JOIN p.subcategoryId sc ORDER BY p.name ASC"
    $query = $em->createQuery($dql);
    $results = $query->getResult();
    $products = array();
    

    Now, because we have protected variables in our entity, you’d want setters and getters on your objects.
    This can be very helpful for formatting how content is returned, etc.

    foreach ($results as $product)
    {
         $products[] = array("id" => $product->getId(),
                             "name" => $product->getName(),
                             "subCategory" => $product->getSubcategoryId()                           
                              );
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 entites: @Entity @Table(name=product) public class Product { private Long id; private
I have entity for Doctrine: <?php /** * @Entity * @Table(name=orders) */ class Orders
I have this Entity called 'Operation': @Entity @Table(name=operation) public class Operation implements Serializable {
I have this entity: public class Product { public string Name { get; set;
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
I have an entity class that has a property with an underlying db column
I have the following query: $qb = $this->em->createQueryBuilder() ->select(array('p','c' ,'sc')) ->from('Project\Entity\Product', 'p') ->innerJoin(p.category, c)
Imagine I have an entity called Product and a repository for it: public class
I have a Product admin class. The Product entity has a many-to-one relationship with
I have a entity called Product,this is a part of it's declration: [EdmEntityTypeAttribute(NamespaceName=NorthwindModel, Name=Product)]

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.