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

  • Home
  • SEARCH
  • 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 9015381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:44:14+00:00 2026-06-16T03:44:14+00:00

Assume there’s a very basic structure: A Category can contain multiple items of Product

  • 0

Assume there’s a very basic structure: A Category can contain multiple items of Product:

/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category", cascade="remove")
 */
protected $products;

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 */
protected $category;

I get all products, when I access them through the category like this:

$category->products;

Let’s say a Product has an attribute invisible. How can I force Doctrine to only load the products that are visible. Where do I put this query? Into the repository? But than: How Do I call it or switch between “load all” and “load certain”? I could of course request the products separately, but this seems not like a good solution.

  • 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-16T03:44:15+00:00Added an answer on June 16, 2026 at 3:44 am

    The easiest way is to simple add a visibility parameter to your category->getProducts() method:

    class Category
    {
    public function getProducts($invisible = false)
    {
        if ($invisible) return $this->products;
    
        $visibleProducts = array();
        foreach($this->products as $product)
        {
            if ($product->isVisible()) $visibleProducts[] = $product;
        }
        return $visibleProducts;
    }
    

    This does not prevent the loading of all products but it does give you an easy way to decide which products you want. And it’s very easy to implement. You can refine it later.

    A second approach would be to create a query to load all your categories with the products. That will avoid the lazy loading of products and allow you to specify exactly which products you want.

    And finally, you could follow @w1cked’s suggestion and make a D2 filter. Takes a little bit of effort to understand the process.

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

Sidebar

Related Questions

Assume there are two models: User and Question . I can use @OneToMany to
Assume there is a string hexString = 0x12 or 0x45 etc. How can I
This is a fairly basic question about state server but assume there are 2
Let's assume there a website «somesite.com». One can login into it using FB app,
Assume there exists the directory structure C:\users\myuser\Desktop\bob\marley. In windows, from the starting directory C:\users\myuser\Desktop
Assume there's an XMLBeans XmlObject with attributes, how can I get selected attributes in
I assume there might be a HTML5 or some JS that can be used
assume there is a grid with some points in it just like in the
Assume there are two separate web applications: a sports site with admin backend an
Assume there is a TFS project Project with the subfolders trunk and 1.0 .

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.