Symfony标准加入意外行为


Symfony Criteria Join unexpected behaviour

我正在尝试使用Propel ORM在Symfony项目中做一个复杂的查询。

我想做的查询是,用人类的话来说:

Select from the 'interface' table the registers that: 
- 1 are associated with a process (with a link table)
- 2 have a name similat to $name
- 3 its destiny application's name is $apd (application accecible by foreign key)
- 4 its originapplication's name is $apo (application accecible by foreign key)

这里是我编写的代码,并且不能工作:

    $c = new Criteria();
    $c->addJoin($linkPeer::CODIGO_INTERFASE,$intPeer::CODIGO_INTERFASE);       //1
    $c->add($linkPeer::CODIGO_PROCESONEGOCIO,$this->getCodigoProcesonegocio());//1
    if($name){                                                    
        $name = '%'.$name.'%';                                    //2
        $c->add($intPeer::NOMBRE_INTERFASE,$name,Criteria::LIKE); //2
    }
    if($apd){
        $apd = '%'.$apd.'%'; //3
        $c->addJoin($appPeer::CODIGO_APLICACION,$intPeer::CODIGO_APLICACION_DESTINO);//3
        $c->add($appPeer::NOMBRE_APLICACION,$apd,Criteria::LIKE); //3
    }
    if($apo){
        $apo = '%'.$apo.'%';//4
        $c->addJoin($appPeer::CODIGO_APLICACION,$intPeer::CODIGO_APLICACION_ORIGEN);//4
        $c->add($appPeer::NOMBRE_APLICACION,$apo,Criteria::LIKE);//4
    }

之后,我做了一个$c->toString()来查看生成的SQL,我看到当我只发送一个$apd值时,SQL是正确的,当我发送一个$apo值也是。但是,当我发送两个,只有$apo AND出现在SQL.

我猜是因为$c->add(…)调用与不同的参数是相同的,但根本不确定。这是错误吗?正确生成查询的最佳方式是什么?

非常感谢您的时间!: D

是的,它覆盖了前面的调用,因为Criteria对象每个字段只存储一个条件。解决方案是创建2个或更多独立的Criterion对象,并将它们混合到Criteria对象中:

//something like this
$cron1 = $criteria->getNewCriterion();
$cron1->add($appPeer::NOMBRE_APLICACION,$apo,Criteria::LIKE);//4
$criteria->add($cron);
//and the same with the other criterion

但是,升级到Propel15+会容易得多,在那里您在Query类级别上工作,并且同一字段上的多个限制不会相互覆盖。

希望这有帮助,丹尼尔