2015-07-10 1 views
0

Модель:Получить данные из автомобиля стола

enter image description here

Мои действия выглядит следующим образом:

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

Использование 'createQueryBuilder()' вам нужно создать DQL not SQL, также обновите свой вопрос, добавив определение ваших сущностей –

ответ

1

Мои предложения состоят в том, чтобы переместить ваш запрос из элемента управления в репозиторий, кажется, у вас уже есть репозиторий автомобилей и его извлекает.

Repository:

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

Мой ответ возвращает массив, но вы можете использовать конструктор запросов в хранилище и возвращает объект ... оба метода работают, я просто предпочитаю мой метод.

Смежные вопросы