我正在遵循symfony教程&我已设法显示我的数据库中的帖子,我现在正在尝试 显示最新的帖子,但我收到以下错误。
Undefined method 'getQueryBuilder'. The method name must start with either findBy or findOneBy!
我的设置如下
my composer.json
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.5.*",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"doctrine/migrations": "dev-master",
"doctrine/doctrine-migrations-bundle": "dev-master",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~3.0",
"sensio/framework-extra-bundle": "~3.0",
"incenteev/composer-parameter-handler": "~2.0"
}
我的src / Blog / CoreBundle / Controller / PostController
namespace Blog\CoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class PostController extends Controller
{
/**
* Show the post index
*
* @return array
*
* @Route("/")
* @Template()
*/
public function indexAction()
{
$posts = $this->getDoctrine()->getRepository('ModelBundle:Post')->findAll();
$latestPosts = $this->getDoctrine()->getRepository('ModelBundle:Post')->findLatest(3);
return array(
'posts' => $posts,
'latestPosts' => $latestPosts
);
}
}
my src/Blog/ModelBundle/Entity/Post.php
namespace Blog\ModelBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
//adds validator class for validation use
use Symfony\Component\Validator\Constraints as Assert;
/**
* Post
*
* @ORM\Table(name="post")
* @ORM\Entity(repositoryClass="Blog\ModelBundle\Repository\PostRepository")
*/
class Post extends Timestampable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=150)
* @Assert\NotBlank
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="body", type="text")
* @Assert\NotBlank
*/
private $body;
/**
* @var Author
*
* @ORM\ManyToOne(targetEntity="Author", inversedBy="posts")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
* @Assert\NotBlank
*/
private $author;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set body
*
* @param string $body
*
* @return Post
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* Get body
*
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* Set author
*
* @param \Blog\ModelBundle\Entity\Author $author
*
* @return Post
*/
public function setAuthor(Author $author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* @return \Blog\ModelBundle\Entity\Author
*/
public function getAuthor()
{
return $this->author;
}
}
我的src / Blog / ModelBundle / Repository / PostRepository.php
namespace Blog\ModelBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* Class PostRepository
*/
class PostRepository extends EntityRepository
{
/**
* Find latest
*
* @param int $num how many posts to get
*
* @return array
*/
public function findLatest($num)
{
$qb = $this->getQueryBuilder()
->orderBy('p.createdAt', 'desc')
->setMaxResults($num);
return $qb->getQuery()
->getResult();
}
/**
* @return \Doctrine\ORM\QueryBuilder
*/
public function qetQueryBuilder()
{
$em = $this->getEntityManager();
$qb = $em->getRepository('ModelBundle:Post')
->createQueryBuilder('p');
return $qb;
}
}
我的config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
framework:
#esi: ~
translator: { fallback: "%locale%" }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
所以有人可以向我建议我缺少什么或者我没有采取什么步骤
由于
答案 0 :(得分:0)
问题已解决。显然这是一个错字错误
public function qetQueryBuilder()
应该是
public function getQueryBuilder()