繁体   English   中英

Symfony2 QueryBuilder无法正常工作

[英]Symfony2 QueryBuilder not working

我正在使用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());
    }
}

任何想法,我要去哪里错了吗?

这里 :

$this->$test = $test;

您可能可以弄清楚自己出了什么问题:)我很确定这是第60行,如错误消息中所述。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM