Php Symfony3,条令\ORM\EntityRepository缺少参数1::\uu construct(),如何以及为什么

Php Symfony3,条令\ORM\EntityRepository缺少参数1::\uu construct(),如何以及为什么,php,doctrine-orm,symfony,Php,Doctrine Orm,Symfony,我正在symfony上制作我的第一个小应用,一个简单的博客 现在,我一直在使用symfony和doctor的文档,并希望执行一个简单的初学者任务:显示一个json编码的简单表 然而不知何故,我似乎无法与教义相处 这是我的数据,与只显示值的视图不同: //AppBundle/Controller/DefaultController.php <?php namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundl

我正在symfony上制作我的第一个小应用,一个简单的博客

现在,我一直在使用symfony和doctor的文档,并希望执行一个简单的初学者任务:显示一个json编码的简单表

然而不知何故,我似乎无法与教义相处

这是我的数据,与只显示值的视图不同:

//AppBundle/Controller/DefaultController.php
<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Post;
use Doctrine\Common\Persistence;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $database = new Post();
        $output = $database->findAll();

        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
            'x' => json_encode($output)
        ]);
    }
}


<?php
//AppBundle/Entity/Post.php

namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;
/**
* @ORM\Entity
* @ORM\Table(name="sqltest")
*/
class Post extends EntityRepository
{
    //The post for now uses data from a temponary test table
    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @ORM\Column(type="string", length=100)
    */
    private $name;

    /**
    * @ORM\Column(type="integer", scale=2)
    */
    private $number;



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

    /**
     * Set name
     * @param string $name
     * @return Post
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set number
     * @param integer $number
     * @return Post
     */
    public function setNumber($number)
    {
        $this->number = $number;

        return $this;
    }

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

}
问题是,当我尝试显示网站时,我会遇到此异常

警告:缺少的参数1 条令\ORM\EntityRepository::\构造,在中调用 C:\Users\Alan\Desktop\symf blog\src\AppBundle\Controller\DefaultController.php 在第19行,定义了

有问题的行是$database=new Post的行

我对这一点很陌生,我知道答案很简单,我就是看不出来。回答时请提供能理解的信息和解释

非常感谢你的耐心

PS:还有一个关于$em变量的解释,我已经看到了很多关于它的用途和从哪里得到它的内容,这将是一个很好的解释,如中所述,您可以通过doctrine entity manager访问自定义存储库类

$em = $this->getDoctrine()->getManager();  
$posts = $em->getRepository('YourBundle:Post')->findAll();
此外,您将实体定义与存储库定义混合在一起,这不是一个好主意


有关正确用法,请参阅。

如果您想要自定义DB函数的存储库类,那么这是正确的方法:

namespace AppBundle\Entity;    
use Doctrine\ORM\EntityRepository;

class PostRepository extends EntityRepository
{
    public function findAll()
    {
        return $this->findBy(array(), array('id' => 'DESC', 'createdAt' => 'DESC'));
    }  
   ....
}
然后在控制器中:

$em = $this->getDoctrine()->getManager(); 
$entities = $em->getRepository("AppBundle:Post")->findAll();

删除它们属于实体的注释。还要注意@james_bond告诉你的。试试看

你熟悉OOP吗?如果是的话,这里是EntityRepository所需的参数,您从中继承的类。因此,您需要通过它们,或者根本不扩展检查我是否理解$class元素,但仍然找不到有关变量$emThis的来源的任何信息这可以给您一个提示$em=$this->getdoctor->getManager$实体=$em->getRepositoryCommonBundle:Bank->findAll;重置一点并阅读入门章节:条令是ORM而不是活动记录模块,因此让实体扩展存储库毫无意义。不用担心。如果你真的想要一个活动记录方法,有几个方法可以选择。@ReynierPM$这是指在这种情况下会发生什么?添加了“$em=$this->getdoctor->getManager$entities=$em->getRepositoryAppBundle:Post->findAll;`然后在$json_encode内打印。结果是[{}{}}]对于一个3行表,添加,使更多的{}在里面