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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:54:59+00:00 2026-06-11T00:54:59+00:00

A. I have a table with users and a table with friends and here

  • 0

A. I have a table with users and a table with friends and here are their connections:

This in the User class:

    /**
     * @ORM\OneToMany(targetEntity="Friend", mappedBy="user")
     */
    protected $friends;

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

and this is in the Friend class:

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

I have an template, in which I render the friends of the current user just using

$friends = $user->getFriends(); 

But now I want to be able to sort the friends of the user. I created a repository class with this function:

public function findAllOrderedByName()
    {
        return $this->getEntityManager()
            ->createQuery('SELECT f FROM EMMyFriendsBundle:Friend f ORDER BY f.name ASC')
            ->getResult();
    }

but it displays all the friends of all the users, not just the current user. Is there a way to show only the friends of the current user without using something like “WHERE friend.user_id = user.id”? If yes, please tell me how to do it.

B. Another thing I would like to ask is how to manage my controllers.

This is the action which renders the template to show all unsorted friends:

/*
 * @Displays the basic information about all the friends of an user
 */
public function displayAction()
{
    $user = $this->get('security.context')->getToken()->getUser();

    if($user->getName() === NULL)
    { 
        $name = $user->getUsername();
    } 
    else 
    {    
        $name = $user->getName();
    }

    $this->get('session')->setFlash('hello', 'Hello, '.$name.'!');

    $friends = $user->getFriends();

    $cat = new Category();

    $dd_form = $this->createForm(new \EM\MyFriendsBundle\Entity\ChooseCatType(), $cat);

    return $this->render('EMMyFriendsBundle:Home:home.html.twig', array(
        'name' => $name, 'friends' => $friends, 'user' => $user,
        'dd_form' => $dd_form->createView()));
}

And I have a sortAction, but I don’t know how to write it. It should be the same as the above, only the row

 $friends = $user->getFriends();

should be replaced with

$friends = $em->getRepository('EMMyFriendsBundle:Friend')
        ->findAllOrderedByName();

but if I copy it, it’s a lot code duplication and it’s stupid. I thought of one controller with an if-statement, but what to have in it? Please give me some ideas! Thanks in advance!

  • 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-11T00:55:01+00:00Added an answer on June 11, 2026 at 12:55 am

    Do this:

    /**
     * @ORM\OneToMany(targetEntity="Friend", mappedBy="user")
     * @ORM\OrderBy({"name" = "ASC"})
     */
    protected $friends;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the table users and scores . Here are the associations: belongs_to :user
I have a users table and a friends table. I want to get the
I have a friends table with columns user1 and user2 and a users table
I have user profile model with M2M field class Account(models.Model): ... friends = models.ManyToManyField('self',
Here's my User model: class User < ActiveRecord::Base has_many :friends, :class_name => 'Friendship', :dependent
I have a social network The users table is around 60,000 rows The friends
I have written this query which returns a users friends and the friends on
Here's a mysql table : friends : followed INT, <-- The user's id (
Say I have users and that users can be friends. I have a table
Hi, I have these two tables: users and friends (friend_status = 1 means the

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.