简体   繁体   中英

How to call doctrine in an entity class using symfony

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();

 }

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.

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.

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
{
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM