2014-12-28 6 views
0

Doctrine 1.2를 사용하여 올바른 쿼리를 생성하여 관련 simple_products의 총 재고가 1보다 큰 제품을 검색하는 방법에 어려움을 겪고 있습니다. 내가 시도Doctrine 1.2 : 관련 단순 제품에 재고가있는 제품 가져 오기

Product: 
    columns: 
    id: 
Simple_product: 
    columns: 
    id: 
    quantity: { type: integer } 
    product_id: { type: integer(11), notnull: true } 
    relations: 
    product_id: { class: Product, local: product_id, foreign: id, foreignAlias: Simple_products } 

예 질의 :

$qProducts = Doctrine_Query::create() 
    ->select('p.*, SUM(s.quantity)') 
    ->from('Product p') 
    ->innerJoin('p.Simple_products s') 
    ->where('p.category_id = ?', $_SESSION['eshop'], 
    ->andWhere('p.brand = ?', $_SESSION['eshop'], 
    ->andWhere('p.is_active = 1') 
    ->having('SUM(s.quantity) > 0'); 

이 쿼리는 반환하지 않습니다 expecte 1 이상)

내 schema.yml 파일의 재고 결과에 따라 데이터베이스에 재고가있는 간단한 제품이 포함되어 있음에도 불구하고 빈 컬렉션을 반환합니다.

내가 뭘 잘못하고 있니?

편집 :

$ qProducts-> getSqlQuery은() 반환

SELECT p.id AS p__id, p.sku AS p__sku, p.name AS p__name, p.gender AS p__gender, p.description AS p__description, p.custom_price AS p__custom_price, p.base_price AS p__base_price, p.sales_price AS p__sales_price, p.eshop_price AS p__eshop_price, p.brand AS p__brand, p.product_url AS p__product_url, p.image_url AS p__image_url, p.is_active AS p__is_active, p.category_id AS p__category_id, SUM(s.quantity) AS s__0 FROM product p INNER JOIN simple_product s ON p.id = s.product_id WHERE (p.category_id = ? AND p.brand = ? AND p.is_active = 1) HAVING SUM(s.quantity) > 0

+0

당신의 수량 필드가 마십시오 simple_product 테이블 맞지? (schema.yml 발췌 부분에 표시되지 않습니다. – rtome

+0

또한 ** echo $ qProducts-> getSqlQuery(); ** 당신은 무엇을 얻을 수 있습니까? 합리적인 쿼리를 얻는다면 mysql 명령 줄 클라이언트? (또는 phpMyAdmin 또는 adminer ...) 반환 값은 0 행입니까? – rtome

답변

1

DQL 쿼리 내가 찾던 :

$qNbProducts = Doctrine_Query::create() 
    ->select('p.id') 
    ->addSelect('SUM(s.quantity) as sum_stock') 
    ->from('Product p') 
    ->innerJoin('p.Category c') 
    ->leftJoin('p.Simple_products s') 
    ->where('c.root_id = ?', $categoryId) 
    ->andWhere('p.brand = ?', $_SESSION['eshop']) 
    ->andWhere('p.is_active = 1') 
    ->groupBy('p.id') 
    ->having('sum_stock > 0');