简体   繁体   中英

Symfony2 QueryBuilder not working

I am learning Symfony2 with Doctrine2

I am trying to use createQueryBuilder() , but I get an error when I try to call the results in my indexController .

The Error is:

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

My 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;
    }
} 

My Controller:

<?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());
    }
}

Any Idea where I am going wrong with this one?

here :

$this->$test = $test;

You can probably figure out yourself what is going wrong : ) And i'm pretty sure this is line 60, as said in the error message.

I'm not giving you the answer because this is typically a kind of error your should spot by yourself before asking on StackOverflow. If you can figure it yourself, you probably won't make it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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