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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:05:41+00:00 2026-05-29T16:05:41+00:00

Let’s say that I have two tables in my database : Rabbits and Carrots.

  • 0

Let’s say that I have two tables in my database : Rabbits and Carrots.
Rabbits can have 0 or multiples carrots and a carrot belongs to a single rabbit. That’s a 1,n relation between those two tables.

I have then two entities, rabbit and carrot.

I have an array of rabbits passed in my template and I would like to get specific carrots from each rabbit an display them : let’s say I want to get the 10 more expensive carrots (carrots prices would be stored in the carrots table) from each $rabbit in the array.

Something like :

{% for rabbit in rabbits %}
    {% for carrot in rabbit.getMoreExpensiveCarrots %}

        {{ carrot.price }}

    {% endfor %}
{% endfor %}

I’m using repository class, but if i create a function getMoreExpensiveCarrots( $rabbit ) in a rabbit repository class, I would not be able to access that function from an entity class like that, which is what I want :

$rabbit->getMoreExpensiveCarrots()

I thought that a way to do that would be to create a getMoreExpensiveCarrots() in the rabbit entity :

// Entity rabbit
class Rabbit
{
    public function getMoreExpensiveCarrots()
    {
        // Access repository functions like getMoreExpensiveCarrots( $rabbit )
        // But how can I do such thing ? Isn't that bad practise ?
        return $carrots;
    }         
}

I thought I could do that too :

    // Entity rabbit
    class Rabbit
    {
        public function getMoreExpensiveCarrots()
        {
            $this->getCarrots();

            // Then try here to sort the carrots by their price, using php            

            return $carrots;
        }         
    }

Here is my controller :

    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $rabbits = $em->getRepository('AppNameBundle:Rabbit')->getSomeRabbits();

        return $this->render('AppNameBundle:Home:index.html.twig', 
                array(
                    "rabbits"=>$rabbits
        ));
    }

What is the best practise to call a getMoreExpensiveCarrots function from each rabbit in the template ?

Thanks!

  • 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-29T16:05:42+00:00Added an answer on May 29, 2026 at 4:05 pm

    Back to basics. Forget about repository vs service and just focus on rabbits and carrots.

    class Rabbit
    {
    /** @ORM\OneToMany(targetEntity="Carrot", mappedBy="rabbit" */
    protected $carrots;
    
    public function getCarrots() { return $this->carrots; }
    
    public function getMoreExpensiveCarrots()
    {
        // Get all carrots
        $carrots = $this->getCarrots()->toArray();
    
        // Sort by price
        // http://php.net/manual/en/function.usort.php
        usort(&$carrots,array($this,'compareTwoCarrotsByPrice'));
    
        // Now slice off the top 10 carrots (slice - carrots, hah, kindo of funny
        $carrots = array_slice($carrots, 0, 10);
    
        // And return the sorted carrots
        return $carrots;
    }
    public function compareTwoCarrotsByPrice($carrot1,$carrot2)
    {
        if ($carrot1->getPrice() > $carrot2->getPrice()) return -1;
        if ($carrot1->getPrice() < $carrot2->getPrice()) return  1;
        return 0;
    }
    }
    

    Remove all the carrot stuff from your query and just get a list of rabbits.
    Your original template will now work exactly as expected:

    {% for rabbit in rabbits %}
        {% for carrot in rabbit.getMoreExpensiveCarrots %}
    
            {{ carrot.price }}
    
        {% endfor %}
    {% endfor %}
    

    The only downside here is that for each rabbit, a separate query for all carrots will be automatically generated by Doctrine. At some point performance will be hindered. When you reach that point then you can go back to your original query and see how to bring the carrots in more efficiently. But get the above class working first as I think this might be your blocking point.

    • 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 tables in db: Car and Part. Car owns arbitrialy
Let's say I have a structure named vertex with a method that adds two
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a

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.