I have a read a lot of topics on this and I can't seem to find a solution to my problem.
I feel like the problem is obvious and maybe I have just been staring at it too long.
The error is FatalErrorException: Error: Call to a member function has() on a non-object in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 198
Looking at the error line, it 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 is calling 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: FuelFormBundleControllerDatabaseController
This is the dao controller that is calling 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 the doctrine container is not present which is why I am having issues. I am not sure how to solve this.
Any help is appreciated. Thanks!
Edit
Ok so here is what I changed.
Main Controller stayed the same.
DAO Controller a couple of things were added.
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;
}
}
Service ended up looking like this
services:
database_controller:
class: FuelFormBundleControllerDatabaseController
arguments: ["@doctrine.orm.entity_manager"]
See Question&Answers more detail:
os