Doctrine Query Builder ไม่ได้ลงทะเบียนพารามิเตอร์ใช่ไหม

ฉันมีรหัสต่อไปนี้ซึ่งมีข้อยกเว้น Invalid parameter number: number of bound variables does not match number of tokens แต่เมื่อฉันพิมพ์พารามิเตอร์ที่บันทึกไว้ พารามิเตอร์ของฉันก็แสดงขึ้นมา

public function getUnitPriceFor($entityType,$entityID,$qty,$configuration_id)
{
    $this->qb = $this->getEntityManager()->createQueryBuilder();
    $this->qb   ->select($this->_entities[$entityType]['select'])
                // for Base this would be ->select(array('t','c','w','g'))
                // for the other cases below, like website, it's array('t','w')
                ->from('AcmeBundle:PriceTier', 't');

    switch($entityType) :
        case 'base' :
            $this->qb   ->leftJoin('t.customers','c')
                        ->leftJoin('t.customergroups','g')
                        ->leftJoin('t.websites','w');
        break;
        case 'website' :
            $this->qb   ->join('t.websites','w','WITH','w.id = '.$entityID);
        break;
        case 'custgrp' :
            $this->qb   ->join('t.customergroups','g','WITH','g.id = '.$entityID);
        break;
        case 'cust' :
            $this->qb   ->join('t.customers','t','WITH','t.id = '.$entityID);
        break;
    endswitch;

    $this->qb           ->where('t.printconfiguration = :configuration_id');
    $this->qb           ->setParameter('configuration_id', $configuration_id);

    print_r( $this->qb->getParameters() );

    $dql = $this->qb->getDQL();

    echo"<pre>";
    print_r($this->getEntityManager()->createQuery($dql)->getArrayResult());
    echo"</pre>";
}

การพิมพ์ $this->qb->getParameters(); แสดงให้ฉันเห็น Array ( [configuration_id] => 1 ) และการลบคำสั่งพารามิเตอร์ตำแหน่งและการตั้งค่าของฉันจะป้องกันไม่ให้เกิดข้อยกเว้น สุดท้ายนี้ (และรับสิ่งนี้) ถ้าฉันลบ Where clause ของฉันออกแต่คงการตั้งค่าพารามิเตอร์ไว้ ก็ไม่มีข้อยกเว้นเกิดขึ้น ฉันค่อนข้างสับสน


person Nick    schedule 10.11.2011    source แหล่งที่มา
comment
มีข้อยกเว้นเกิดขึ้นกับแต่ละ 'กรณี' หรือเฉพาะบางกรณีเท่านั้น นอกจากนี้ คุณได้ลองส่ง $configuration_id ไปยัง WHERE clause ของคุณโดยตรงแทนที่จะใช้ setParameter() หรือไม่   -  person cantera    schedule 10.11.2011
comment
อ่า กรณีอื่นๆ อาจทำให้เกิดข้อผิดพลาดทางความหมายเนื่องจากคำสั่ง select ฉันได้แก้ไขแล้ว และใช่ ยังคงเป็นข้อยกเว้นในทุกกรณี ฉันลอง where('t.printconfiguration = :configuration_id')->setParameter('configuration_id', $configuration_id); แล้ว แต่ยังคงมีข้อยกเว้นเดียวกันนี้เกิดขึ้น ฉันสามารถส่งผ่านมันได้โดยตรงเหมือนกับที่ฉันทำในคำสั่ง case แต่นั่นค่อนข้างขัดต่อวัตถุประสงค์ของการใช้ PDO...   -  person Nick    schedule 10.11.2011
comment
ดีใจที่คุณพบคำตอบของคุณ คุณช่วยอธิบายสิ่งที่คุณหมายถึงโดยเอาชนะวัตถุประสงค์ของการใช้ PDO ได้ไหม? ฉันไม่เห็นด้วย ฉันเพิ่งส่งพารามิเตอร์ของฉันโดยตรงเพื่อบันทึกโค้ดเล็กๆ น้อยๆ และอยากรู้ว่ามีเหตุผลใดบ้างที่ฉันไม่ควรทำเช่นนี้ ยินดีพูดคุยในแชทหากง่ายกว่า   -  person cantera    schedule 11.11.2011
comment
ฟังก์ชั่นข้างต้นส่วนใหญ่จะใช้สำหรับคำสั่งที่เตรียมไว้และขั้นตอนการจัดเก็บ นอกจากนี้ยังมีการป้องกันการฉีด sql โดยธรรมชาติอีกด้วย นี่คือตัวอย่างจากคู่มือ PHP เกี่ยวกับคำสั่งที่เตรียมไว้ สุดท้าย บทช่วยสอนเล็กๆ น้อยๆ นี้ มีส่วนเกี่ยวกับตัวยึดตำแหน่ง   -  person Nick    schedule 14.11.2011


คำตอบ (1)


เห็นได้ชัดว่า $dql = $this->qb->getDQL(); จะไม่ผ่านพารามิเตอร์

ฉันจำเป็นต้องเปลี่ยน

$dql = $this->qb->getDQL();

echo"<pre>";
print_r($this->getEntityManager()->createQuery($dql)->getArrayResult());
echo"</pre>";

ถึง

$query = $this->qb->getQuery();

echo"<pre>";
print_r($query->getArrayResult());
echo"</pre>";
person Nick    schedule 10.11.2011