模型是:
我的行动如下:
public function fahrzeugeAction()
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$userId = $user->getId();
$repository = $this->getDoctrine()
->getRepository('FPMAppBundle:Car');
/* This is the normal Select and it is testet with the mysqlworkbench.
SELECT c.id, c.description, c.active
FROM car c
INNER JOIN customer cu ON cu.id = c.id_customer
INNER JOIN customer_user cuu ON cuu.id_customer = cu.id
WHERE c.active = 1 AND cuu.id_user = 1
*/
$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder()
->select('c.id, c.description, c.active')
->from('Car', 'c')
->innerJoin('Customer', 'cu', 'ON', 'c.idCustomer = cu.id')
->innerJoin('CustomerUser', 'cuu', 'ON', 'cu.id = cuu.id_customer')
->where('cuu.idUser = ?', $userId)
->orderBy('c.id', 'ASC')
->getQuery();
$cars = $qb->getResult();
return $this->render( "FPMAllgemeinBundle:Fahrzeuge:overview.html.twig"
array(
"cars" => $cars // I know that i have at the moment no variable named $cars
)
);
}
警告:get_class()期望参数1为对象,给定整数
当我使用正常的Select-Statement时,我得到了正确的结果。
答案 0 :(得分:1)
我的建议是将您的查询从控件移动到存储库,看起来您已经拥有了Car存储库并正在检索它。
存储库:
<?php
namespace Acme\Entity\Repository;
use Doctrine\ORM\EntityRepository;
class CarRepository extends EntityRepository {
public function myMethodName() {
try {
$sql = "SELECT c.id, c.description, c.active
FROM car c
INNER JOIN customer cu ON cu.id = c.id_customer
INNER JOIN customer_user cuu ON cuu.id_customer = cu.id
WHERE c.active = 1
AND cuu.id_user = 1";
$sth = $this->getEntityManager()->getConnection()->prepare($sql);
$sth->execute();
return $sth->fetchAll();
} catch (\PDOException $e) {
return false;
}
}
}
控制器:
public function fahrzeugeAction() {
$user = $this->get('security.token_storage')->getToken()->getUser();
$userId = $user->getId();
$cars = $this->getDoctrine()
->getRepository('FPMAppBundle:Car')
->myMethodName();
return $this->render( "FPMAllgemeinBundle:Fahrzeuge:overview.html.twig"
array(
"cars" => $cars
));
}
我的回答是返回一个数组,但您可以使用存储库中的查询构建器并返回一个对象......这两种方法都可以工作,我更喜欢我的方法。