2

Am using symfony framework for my application. And to save records in database I want call the $this->getDoctrine()->getManager(); method in my entity class. But when I did that it gave me the error: Call to undefined method getDoctrine(),

Can some one tell me what is the right way to do this.

My entity class is like:

namespace Acme\SuperbAppBundle\Entity;

use Symfony\Component\DependencyInjection\Container; use Doctrine\ORM\Mapping as ORM;

class Users
{


/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $firstName;

/**
 * @var string
 */

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set firstName
 *
 * @param string $firstName
 * @return Users
 */
public function setFirstName($firstName)
{
    $this->firstName = $firstName;

    return $this;
}

/**
 * Get firstName
 *
 * @return string 
 */
public function getFirstName()
{
    return $this->firstName;
}


function __construct($firstName){

    $this->setFirstName($firstName);

}

function save(){

    $em = $this->getDoctrine()->getManager();
    $em->persist($create);
    $em->flush();
}

}

And my controller method is like:

   public function test(){

    $create = new Users('Rajat');

    $create->save();

 }
Randhir
  • 735
  • 4
  • 11
  • 22

4 Answers4

6

Your save method is attempting to call

$this->getDoctrine();

Whereby $this is the current Class, and any other Class it inherits. As it stands, your current Class, User, is standalone, and does not have a getDoctrine() method. If your Class were to extend the Controller Class, it would have access to that method:

class User extends Controller

I believe this simple fix will work, although it probably doesn't make real sense for it to extend Controller, as it is a User Entity, and unrelated to a Controller. A preferred, more advanced method, would be to inject the Doctrine service into the User class.

Alex
  • 1,565
  • 2
  • 9
  • 13
  • If model class extends to a controller, the model class will become an instance of another controller, which I don't think a good approach. We need to think about the MVC structure as well. I prefer inject the entity manager to the model class if you want to access the doctrine manager in the entity class. Hope this helps. Cheers! – Anjana Silva Jul 02 '14 at 16:27
  • I prefer it to. This method is a quick fix to an immediate problem, which is often enough, depending on the scale of the project. Making code functional is the first objective, making it pretty is the second. Randhir may just need his application to work, by any means, and not care much about best practices, to which I offered a solution, with a word of warning attached – Alex Jul 02 '14 at 16:35
2

Ok, first of all Doctrine Entities : Handle the entity generation and configuration Declare the operations on the setters and getters. If you wana save an object into your entity there it's your User, you have two way to store this user: One: You can use entity manager to store a user and the entity will help you to create the right object using the seters and getters:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use PATH\TO\Users;

class ExampleController extends Controller
{
    public function examplefunction()
    {
       $em = $this->getDoctrine()->getManager();
       $entity = new Users();
       $entity->setFirstName('Rajat');
       $em->persist($entity);
       $em->flush();
    }
}

The other way is to create this entry using QueryBuilder but it's a bad way in your case. Oh, i forgot please delete the save method in your entity Doctrine manager allready implement it.

Mehdi Aïssani
  • 111
  • 1
  • 5
0

Your controller probably doesnt extends Symfony\Bundle\FrameworkBundle\Controller\Controller ... You should have controller defined like this example:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
}
David Marko
  • 2,477
  • 3
  • 27
  • 58
0

Entity class does not extends ContainerAware / Controller, so you can't call $this->getDoctrine()->getManager(). I don't think your Entity class should extend to a Controller. Because your entity class will become a controller instance just because you want to access the doctrine manager. That's a not good practice. What you can do is inject doctrine manager to your Entity class through services.

I wrote a blog few weeks ago regarding injecting services container and accessing through constructor. You can inject doctrine entity manager in the same way you inject services container. You can take a look at that if you like :- http://anjanasilva.com/blog/injecting-services-in-symfony-2/

Here's a nice question regarding injecting doctrine manager. Make sure you read the answer as well. :- Symfony 2 EntityManager injection in service

And another nice tutorial on injecting custom repository manager instead of injecting the whole entity manager. Which I believe even a good solution. :- http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/

Hope this helps to increase your understanding about Symfony 2.

Cheers!

Community
  • 1
  • 1
Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
  • Please include the full contents of the knowledge necessary within your answer, as linking to third party sites is unreliable, and should not be the focus of your answer. – carbide20 Jan 02 '17 at 06:55
  • @carbide20 This is a constructive answer which includes references from various other websites. All those sites refers to the originator within the content. Probably, you want to take a look at those before jumping in to such conclusions. Thanks. – Anjana Silva Jan 02 '17 at 19:35
  • the site rules regarding anwers strictly states when using links in answers, to: "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." see: http://stackoverflow.com/help/how-to-answer You seem to be defensive about whether or not the content author is properly attributed on those sites - strange, as this was never in question. I merely suggested that you include the contents of some of those sites for a more complete answer, in case they are ever unreachable. – carbide20 Jan 03 '17 at 21:52