您可以在 Query.php 类中使用 php 开关来构建条件(由于您的问题显示了简化的示例,因此您的条件所需的开关可能要复杂得多):
//in YourModelQuery.php
public function addMyItemPositionCriteria($itemPosition) {
//TODO: validate $itemPosition values as needed
switch ($itemPosition) {
case 1:
return $this->add([$criteria see http://api.propelorm.org/1.6.8/ browse propel.runtime.query Criteria and ModelCriteria classes ]);
break;
case 2:
return $this->add([$criteria here]);
break;
}
}
或者您可以类似地在您的 ModelPeer.php 类(或没有函数调用的操作类)中构建一个 $criteria 对象:
// in MyObjectPeerClass.php
public static function addMyItemPositionCriteria($itemPosition) {
//TODO: validate $itemPosition values as needed
$criteria = new Criteria;
switch ($itemPosition) {
case 1:
return $criteria->add([$criteria see http://api.propelorm.org/1.6.8/ browse propel.runtime.query Criteria and ModelCriteria classes ]);
break;
case 2:
return $criteria->add([$criteria here]);
break;
}
}
然后,您可以使用此标准:
//depending on context
// in ModelQuery.php call
$this->addMyItemPostion($itemPosition);
// call to model query
ModelQuery::create()->addMyItemPosition($itemPosition)->find();
//in action or somewhere else (adding to previously defined $criteria)
$new_criteria = MyObjectPeerClass::addMyItemPosition($itemPosition);
$criteria->add($new_criteria);