2016-12-09 41 views
1

我在symfony中創建了我的第一個小應用,一個簡單的博客。Symfony3,對於Doctrine ORM EntityRepository :: __ construct()缺少參數1,爲什麼和爲什麼

現在,我一直在使用這兩種Symfony和學說的文件,並希望大跳簡單,初學者的任務:顯示一個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行 Doctrine \ ORM \ EntityRepository :: __ construct(),在第19行調用 C:\ Users \ Alan \ Desktop \ symf-blog \ src \ AppBundle \ Controller \ DefaultController.php 並定義爲

問題的行作爲一個與$database = new Post();

我對這個很新的,我知道,響應很簡單,我只是沒有看到它。在回答時請提供和解釋哪個even a dead rabbit能理解。

非常感謝您的耐心配合。

PS:還有什麼的$em可變我見過這麼多是從哪兒可以得到這將是很好

+0

您是否熟悉OOP?如果是這樣的話,請查看(http://www.doctrine-project.org/api/orm/2.5/class-Doctrine.ORM.EntityRepository.html)這裏的「EntityRepository」所需的參數。 。所以你將需要通過它們或不擴展(檢查[這裏](http://symfony.com/doc/current/doctrine.html)) – ReynierPM

+0

我瞭解$ class元素,但仍然不能找到任何有關變量起源的信息$ em – aln447

+0

這可以給你一個提示'$ em = $ this-> getDoctrine() - > getManager(); $ entities = $ em-> getRepository(「CommonBundle:Bank」) - > findAll();' – ReynierPM

回答

2

如果你想定製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告訴你的是什麼。試試看!

+0

很高興它的工作,不客氣 – ReynierPM

2

正如documentation規定的解釋,您可以通過訪問學說定製庫類實體經理。

$em = $this->getDoctrine()->getManager(); 
$posts = $em->getRepository('YourBundle:Post')->findAll(); 

此外,您正在混合您的實體定義與您的存儲庫定義,這不是一個好主意。

正確使用請參考doctrine documentation in symfony

+0

增加了'$ em = $ this-> getDoctrine() - > getManager(); $ entities = $ em-> getRepository(「AppBundle:Post」) - > findAll();' 後來打印在'$ json_encode'裏面。 結果是'[{} {} {}]'(對於一個3行的表格,添加了更多內部的{}} – aln447