Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
symfony2在实体内部创建自定义查询-getEntityManager()未定义_Symfony_Undefined Behavior - Fatal编程技术网

symfony2在实体内部创建自定义查询-getEntityManager()未定义

symfony2在实体内部创建自定义查询-getEntityManager()未定义,symfony,undefined-behavior,Symfony,Undefined Behavior,我希望在实体类中运行自定义查询。我不想对这个查询使用表映射,我只想返回一个结果数组,但我希望查询仍然记录到记录器。我已经精简了这个类并重新命名,试图说明我想要实现的目标 // src/Name/ExampleBundle/Entity/ExampleEntity.php namespace Name\ExampleBundle\Entity; class ExampleEntity { public function getArrayFromExample(){

我希望在实体类中运行自定义查询。我不想对这个查询使用表映射,我只想返回一个结果数组,但我希望查询仍然记录到记录器。我已经精简了这个类并重新命名,试图说明我想要实现的目标

// src/Name/ExampleBundle/Entity/ExampleEntity.php

namespace Name\ExampleBundle\Entity;

class ExampleEntity
{

    public function getArrayFromExample(){

        $results = $this->getEntityManager()
        ->createQuery("SELECT * FROM exmapleTable LIMIT 50")
        ->getResult();

        return $results;
    }

}
上面的返回值类似于

Fatal error: Call to undefined method {path}\ExampleEntity::getEntityManager() 

您的查询应该位于实体存储库中,而不是实体本身

然后你可以做一些类似的事情:

public function getArrayFromExample(){
    $results = $this->_em
    ->createQuery("SELECT * FROM exmapleTable LIMIT 50")
    ->getResult();

    return $results;
}

对于插入存储库中的数据,您应该如何处理?对于插入,您应该编辑实体并将其保存到实体管理器中。