Php Symfony条件连接意外行为
我正试图在一个Symfony项目中使用propeorm进行一个复杂的查询 我想提出的问题是,用人类的话来说:Php Symfony条件连接意外行为,php,sql,join,symfony1,symfony-1.4,Php,Sql,Join,Symfony1,Symfony 1.4,我正试图在一个Symfony项目中使用propeorm进行一个复杂的查询 我想提出的问题是,用人类的话来说: 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 (applicati
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值时。但是当我同时发送这两个数据时,只有SQL上的$apo和apears
我猜这是因为$c->add。。。调用是相同的,但参数不同,但根本不确定。这是错误吗?正确生成查询的最佳方法是什么
非常感谢您抽出时间!:D是的,它覆盖了以前的调用,因为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+会容易得多,在这里您可以在查询类级别工作,并且同一字段上的多个限制不会相互覆盖
希望这有帮助,
丹尼尔