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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:14:05+00:00 2026-06-11T10:14:05+00:00

I have a controller which has actions for inserting in the database, updating, deleting

  • 0

I have a controller which has actions for inserting in the database, updating, deleting and some others, but almost all of the actions contain in them this lines:

$em = $this->getDoctrine()->getEntityManager(); 
$friend = $em->getRepository('EMMyFriendsBundle:Friend')->find($id);
$user = $this->get('security.context')->getToken()->getUser();

Is this OK, or it’s code duplication? I tried to make a property called $em and to have a constructor like this:

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

but it didn’t work. As for the queries especially the one with the $id parameter, I don’t even know how to separate them in one place, so each action to be able to use them. One way is a function, but is there sense in a function like this? And if yes what should it return? An array?

Please advise me for the optimal way!

  • 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-11T10:14:06+00:00Added an answer on June 11, 2026 at 10:14 am

    You can do:

    private $em;
    private $friend;
    private $user;
    
    private function init($id==null) {
        $this->em = $this->getDoctrine()->getEntityManager(); 
        $this->friend = $id?$this->em->getRepository('EMMyFriendsBundle:Friend')->find($id):null;
        $this->user = $this->get('security.context')->getToken()->getUser();
    }
    

    Then you can call in your actions

    $this->init($id);
    

    or

    $this->init();
    

    And you will have

    $this->em;
    $this->friend;
    $this->user;
    

    available. Note that I allowed for the $id parameter not to be set, as I guess that in some actions you will not have it.

    If you want this init function to be available in different controllers, create a base controller and extend from it, as suggested in another answer.

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

Sidebar

Related Questions

I have a controller which has a few actions, which are triggered by hitting
I have a Controller X which basically has 3 actions new, search and result.
Say if I have a controller, profile, which has just two actions. The first
I have a controller named Content, which has an Index action, which is associated
I have a controller which has a method called history class UsersController < ApplicationController
In my IB I have a navigation controller which has in it a view
I have a navigation controller which also has a table view. I want to
I have a Snake class, which has a pointer to a Controller class object.
I have an controller which is an ArrayProxy and contains some computed arrays. In
I have a universal view controller class which all of the view controller classes

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.