简体   繁体   English

如何使用symfony在实体类中调用学说

[英]How to call doctrine in an entity class using symfony

Am using symfony framework for my application. 我正在为我的应用程序使用symfony框架。 And to save records in database I want call the $this->getDoctrine()->getManager(); 为了将记录保存在数据库中,我想调用$ this-> getDoctrine()-> getManager();。 method in my entity class. 我的实体类中的方法。 But when I did that it gave me the error: Call to undefined method getDoctrine(), 但是,当我这样做时,它给了我错误:调用未定义的方法getDoctrine(),

Can some one tell me what is the right way to do this. 有人可以告诉我什么是正确的方法。

My entity class is like: 我的实体类如下:

namespace Acme\\SuperbAppBundle\\Entity; 命名空间Acme \\ SuperbAppBundle \\ Entity;

use Symfony\\Component\\DependencyInjection\\Container; 使用Symfony \\ Component \\ DependencyInjection \\ Container; use Doctrine\\ORM\\Mapping as ORM; 使用Doctrine \\ ORM \\ Mapping作为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. 因此, $this是当前类以及它继承的任何其他类。 As it stands, your current Class, User, is standalone, and does not have a getDoctrine() method. 就目前而言,您当前的Class User是独立的,并且没有getDoctrine()方法。 If your Class were to extend the Controller Class, it would have access to that method: 如果您的类要扩展Controller类,则可以访问该方法:

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. 我相信这个简单的修补程序将起作用,尽管它对于扩展Controller可能没有真正意义,因为它是用户实体,并且与Controller无关。 A preferred, more advanced method, would be to inject the Doctrine service into the User class. 一种首选的,更高级的方法是将Doctrine服务注入User类。

Ok, first of all Doctrine Entities : Handle the entity generation and configuration Declare the operations on the setters and getters. 好的,首先是教义实体:处理实体的生成和配置声明对setter和getter的操作。 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. 另一种方法是使用QueryBuilder创建此条目,但对您而言,这是一种不好的方法。 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: 您的控制器可能不会扩展Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller ...您应该像以下示例那样定义控制器:

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(). 实体类不会扩展ContainerAware / Controller,因此您不能调用$ this-> getDoctrine()-> getManager()。 I don't think your Entity class should extend to a Controller. 我认为您的Entity类不应该扩展到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. 您可以做的是通过服务将学说管理器注入到Entity类中。

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/ 如果愿意,可以看一下: -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 -Symfony 2 EntityManager注入服务

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/ -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. 希望这有助于增进您对Symfony 2的了解。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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