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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:16:22+00:00 2026-05-25T12:16:22+00:00

I’m using Symfony2 and Doctrine2. How would I go about using a different query

  • 0

I’m using Symfony2 and Doctrine2. How would I go about using a different query for a get method? (poorly worded, let me explain)

i.e. I have an application entity, this has a one to many relation with Version

so in my application entity I have:

/**
 * Get versions
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getVersions()
{
    return $this->versions;
}

This is all good, however, how can I implement a method, such as

getCurrentVersion, where I perform a simple query, such as:

SELECT v.* FROM version WHERE application_id = 1 AND current = 1

and so in a twig template, I can do:

{% for application in applications %}
   Name : {{ application.name }}
   Current Version: {{ application.getCurrentVersion }}
{% endfor %}

Any help is much appreciated.

p.s. if i’m going about this wrong, please enlighten me.

Thanks.

EDIT: http://www.doctrine-project.org/docs/orm/2.1/en/reference/limitations-and-known-issues.html#join-columns-with-non-primary-keys Seriously?!

EDIT, I really don’t want to do this, it is unnecessary and resourcefully wasteful:

public function getCurrentVersion
{
   foreach($this->versions as $version)
   {
      if($version->current)
      {
        return $version;
      }

   }
}
  • 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-25T12:16:23+00:00Added an answer on May 25, 2026 at 12:16 pm

    First, you shouldn’t touch the database from your entity. It’s considered bad practice to inject an entity manager into a managed entity, and it has no way of knowing about the data layer otherwise.

    To do what you’re asking, you can use a custom entity repository with a getCurrentVersion method that takes an id or instance of the entity and runs the query on it:

    // class Foo\Bundle\Repository\BarRepository
    public function getCurrentVersion(MyClass $class)
    {
        $dql = 'SELECT v FROM Namespace\Of\Version\Class v WHERE v.application_id=:id AND current=1';
        $query = $this->_em->createQuery($dql);
        $query->setParameter('id', $class->getId());
        $version = $query->execute();
    
        return $version;
    }
    

    Note that if you’re only expecting one version, you can use $query->getSingleResult(); instead of $query->execute();

    However, it sounds like you’re trying to do versioning/history on some of your classes, in which case, you should check out the Gedmo DoctrineExtensions and associated Symfony2 bundle; specifically, have a look at the Loggable behavioral extension, which handles versioning of your entities transparently.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.