I have many questions on this, and I can not find a solution to my problem.
I feel the problem is obvious, and maybe I just looked at it for too long.
Error: FatalErrorException: Error: calling a member function has () for a non-object in the line /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 198
Looking at the error line, he says.
public function getDoctrine() { if (!$this->container->has('doctrine')) { throw new \LogicException('The DoctrineBundle is not registered in your application.'); } return $this->container->get('doctrine'); }
Here is my code ...
This is the main controller that calls the DAO controller
public function clickThroughAction(request $request, $hash) { if (isset($hash)) { $dbController = $this->get('database_controller'); print_r($dbController->getReferralIdByHash($hash)); return $this->redirect($this->generateUrl('home')); } else { return 0; } }
This is the service that is being used.
services: database_controller: class: Fuel\FormBundle\Controller\DatabaseController
This is the dao controller that calls the database.
public function getReferralIdByHash($hash) { $em = $this->getDoctrine()->getManager(); $query = $em->createQuery( 'Select u From FuelFormBundle:UserReferrals u WHERE u.user_hash = :hash' )->setParameter('hash', $hash); $referral = $query->getResult(); if (!$referral) { throw $this->createNotFoundException( 'No product referral found' ); $logger = $this->get('logger'); $logger->info('I just got the logger'); $logger->crit('An error occurred, hash for referrals is not recognized. current hash is: ' . $hash . " ."); return $this->redirect($this->generateUrl('home')); } $clickThroughCount = $referral[0]->getClickThrough(); $referral[0]->setClickThrough($clickThroughCount + 1); $em->flush(); return $referral; }
I think the problem is that there is no doctrine container, so I am having problems. I am not sure how to solve this.
Any help is appreciated. Thank you
Edit
So here is what I changed.
The main controller remains the same.
DAO Controller added a few things.
class DatabaseController extends Controller{ protected $entityManager; public function __construct($entityManager) { $this->entityManager = $entityManager; } public function getReferralIdByHash($hash) { $em = $this->entityManager; $query = $em->createQuery( 'Select u From FuelFormBundle:UserReferrals u WHERE u.user_hash = :hash' )->setParameter('hash', $hash); $referral = $query->getResult(); if (!$referral) { throw $this->createNotFoundException( 'No product referral found' ); $logger = $this->get('logger'); $logger->info('I just got the logger'); $logger->crit('An error occurred, hash for referrals is not recognized. current hash is: ' . $hash . " ."); return $this->redirect($this->generateUrl('home')); } $clickThroughCount = $referral[0]->getClickThrough(); $referral[0]->setClickThrough($clickThroughCount + 1); $em->flush(); return $referral; } }
The service turned out to be similar to this
services: database_controller: class: Fuel\FormBundle\Controller\DatabaseController arguments: ["@doctrine.orm.entity_manager"]