Symfony2 QueryBuilder无法正常工作

时间:2015-04-24 12:06:34

标签: php symfony doctrine-orm

我正在使用Doctrine2学习Symfony2

我正在尝试使用createQueryBuilder(),但是当我尝试在indexController中调用结果时收到错误。

错误是:

Catchable fatal error: Object of class API\TestBundle\TestDoctrine\Repository\TestRepo could not be converted to string in /Users/tomasz.koprowski/Dev/jv-api/src/API/TestBundle/Controller/IndexController.php on line 60

我的TestRepo:

<?php

namespace API\TestBundle\TestDoctrine\Repository;

use API\TestBundle\TestDoctrine\DatabaseRepository;
use Doctrine\DBAL\Connection;
use Psr\Log\LoggerInterface;


class TestRepo extends DatabaseRepository{

    protected $test = array();

    public function __construct(
        Connection $connection,
        LoggerInterface $logger
    ){
        parent::__construct($connection, $logger);
    }

    public function getTestData()
    {

        $check = $this->db->createQueryBuilder()

            ->select('test.user_name')
            ->from('test');

        $this->test = $check->execute()->fetchAll(\PDO::FETCH_ASSOC);
        return $this->test;
    }
} 

我的控制器:

<?php
namespace API\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

use API\TestBundle\TestDoctrine\Repository\TestRepo;

class IndexController
{
    public $test;

    public function __construct(
        TestRepo $test
    ) {
        $this->$test = $test;
    }

    public function testDoctrineAction()
    {
       return new JsonResponse($this->test->getTestData());
    }
}

我对这个问题出了什么问题?

1 个答案:

答案 0 :(得分:4)

这里:

$this->$test = $test;

你可能会弄清楚自己出了什么问题:)我非常确定这是第60行,如错误信息中所述。

我没有给你答案,因为这通常是你在询问StackOverflow之前应该自己发现的一种错误。如果你能自己弄清楚,你可能不会再做它了。

相关问题