1

Symfony 2 typical problem, yet no clear response to it(I did some research).

Given the following "DefaultController" class which actually works:

<?php

namespace obbex\AdsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    public function indexAction()
    {
       $em = $this->getDoctrine()->getEntityManager();
       $connection=$em->getConnection();
       $string="SELECT DISTINCT country_code FROM country_data";
       $statement = $connection->prepare($string);
       $statement->execute();
       $result = $statement->fetchAll();
       var_dump($result); //works not problem
       die();
    }
}

I want to delegate database calls to another class called "DatabaseController", the "DefaultController" now is set as following:

<?php

namespace obbex\AdsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use obbex\AdsBundle\Controller\DatabaseController; //new DatabaseController

class DefaultController extends Controller
{
    public function indexAction()
    {
        $dbController = new DatabaseController();
        $res = $dbController->getQuery();
    }
}

and the "DatabaseController" is set as following:

namespace obbex\AdsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DatabaseController extends Controller{

    public function __construct() {
    }


    public function getQuery()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $connection=$em->getConnection();
        $string="SELECT DISTINCT country_code FROM country_data";
        $statement = $connection->prepare($string);
        $statement->execute();
        return $statement->fetchAll();
    }

}

And this throw and the following error: FatalErrorException: Error: Call to a member function has() on a non-object in /home/alfonso/sites/ads.obbex.com/public_html/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 202

my mind is blowing right now because I am extending the exact same class "Controller". Why it is working in one case and not in the other?

Apparently it is a "container problem" that can be set trough a service according to a response in another thread or via extending the "Controller· class however does not work in this case.

2 Answers2

0

First of all, you shouldn`t delegate database management to another controller, that's a bad practice.

Instead, you can inject a service containing all DB logic

Symfony2 Use Doctrine in Service Container

Or use a EntityRepository

http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes

Regarding the issue with has() function, you are creating an instance of a Controller without any container on it. Therefore, when the controller tries to call $this->container->has() throws an error, as container is not defined.

Community
  • 1
  • 1
Gorka Lauzirika
  • 303
  • 5
  • 18
0

I finally set the object caller and I requested the container service as follows:

on the services.yml file

service:
   manage_ads:
    class: obbex\AdsBundle\Classes\ManageAds
    calls: 
        - [setContainer, ["@service_container"]]

on the main controller:

$ads_manager = $this->get('manage_ads');
$ads_manager->functionCallingTheRawQuery();

But I still use this optionally because now I am setting my queries from the repository of an entity rather than create my own objects (For now, I am new to symfony2)