我有種每日隨機圖像系統建立與Codeigniter和Doctrine。
有一個關鍵功能,它返回每日圖像或設置一個新的。我正在使用遞歸調用,但有時是兩個圖像選擇一天。
class Daily extends Controller {
function find_daily(){
$connection = Doctrine_Manager::connection();
$connection->setCharset('utf8');
$q = Doctrine_Query::create()
->select('COUNT(id) as dailycount')
->from('Image')
->where('daily_date = ?',date("Y-m-d"));
$set = $q->execute();
if($set[0]->dailycount != 0){ //theres one or more for today
$q = Doctrine_Query::create()
->select('id')
->from('Image')
->where('daily_date = ?',date("Y-m-d"))
->limit(1);
$set = $q->execute();
$i = Doctrine::getTable('Image')->findOneById($set[0]["id"]);
return $i;
}else { // none image selected with today date
$q = Doctrine_Query::create()
->select('id')
->from('Image')
->where('daily_date = ?', "0000-00-00")
->andWhere("dir =?","queue")
->orderBy("rand()")
->limit(1);
$set = $q->execute();
$i = Doctrine::getTable('Image')->findOneById($set[0]["id"]);
$i->daily_date = date("Y-m-d");
$i->dir = "archive";
$i->save();
$this -> find_daily();
};
}
...
我覺得那裏有一些愚蠢的錯誤..也許叫什麼「主義:: doAllAndClear」 $this -> find_daily();
之前或我忘了什麼東西......
還是最好不要使用遞歸調用?
謝謝。
忍受我,因爲我可能只是被你的代碼弄糊塗了,但你爲什麼要遞歸?爲什麼不說,只是'返回$我;'而不是再次調用'$ this-> find_daily()'?另外,如果你遞歸,那麼無論你打電話給你什麼都不會收到回報價值,是嗎?因爲你沒有返回遞歸調用的結果。我們可能需要查看函數的其餘部分來確定發生了什麼。 – 2011-03-04 12:01:18
另外,這段代碼如何被啓動?有兩個用戶同時運行這個機會嗎? – 2011-03-04 12:01:57
@matt你是對的我可以做到這一點沒有遞歸...我可以這樣做,但我會很高興找到我做錯了什麼。當某些用戶查看日常圖像時使用此代碼。我有約10名用戶(項目不公開)。我的表是時間戳 - 並且同時選擇兩個圖像(db行更新)。我認爲,現在同一時間(午夜)有兩名用戶並不是可能的(此問題反覆出現)。 – 2011-03-04 12:28:18