您可以使用PHP的開關在你的Query.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類對象(或動作類沒有函數調用):
// 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);
你看過[這裏](https://groups.google.com/forum/?fromgroups=#!topic/propel-users/MF9H_V7Mijc)嗎? – j0k 2013-02-21 12:45:36
@ j0k 我試過使用此代碼,但在最好的情況下,我有「注意:未定義的偏移量」。 – skyner87 2013-02-21 14:01:10