我有一些問題,試圖保存有4張圖片的文章。事情是,我需要使用文章ID爲了命名圖片像article_id."-"$i
問題與saveAssociated cakephp
由於我只有4張圖片每篇文章這$我應該從1到4或從0到三。
現在的問題是,爲了實現這一目標,我需要創建和保存文章的模型,所以我可以有一個id使用,但隨後進行所有的腳本,使拇指和形成名字,當我去後文章 - > saveAssociated()我有兩倍的文章記錄創建!我試圖在保存之前將ID設置爲「-1」,但沒有任何內容...
任何建議將非常感謝!
代碼:
public function add() {
if ($this->request->is ('ajax')) {
$this->layout = 'ajax';
} else {
$this->layout = 'default';
}
if ($this->request->is ('post')) {
$this->Article->create();
$this->request->data ['Article'] ['time_stamp'] = date ('Y-m-d H:i:s', time());
if ($this->Article->save($this->request->data)) {
for ($i=0; $i<4; $i++){
$img_path = "./images/";
$extension[$i] = end(explode('.', $this->request->data['Image'][$i]['image']['name']));
$this->request->data['Image'][$i]['image'] = array('name'=>$this->Article->id."-".$i, 'tmp_name' => $this->request->data['Image'][$i]['image']['tmp_name']);
// $this->request->data['Image'][$i]['name'] = $this->Article->id."-".$i;
$this->request->data['Image'][$i]['ext']= $extension[$i];
$target_path[$i] = $img_path . basename($this->request->data['Image'][$i]['image']['name'].".".$extension[$i]);
if(!move_uploaded_file($this->request->data['Image'][$i]['image']['tmp_name'], $target_path[$i])) {
die(__ ('Fatal error, we are all going to die.'));
}else{
$this->Resize->img($target_path[$i]);
$this->Resize->setNewImage($img_path.basename($this->request->data['Image'][$i]['image']['name']."t.".$extension[$i]));
$this->Resize->setProportionalFlag('H');
$this->Resize->setProportional(1);
$this->Resize->setNewSize(90, 90);
$this->Resize->make();
}
}
$this->Article->id;
pr($this->Article->id);
$this->Article->saveAssociated($this->request->data, array('deep' => true));
//$this->redirect (array ('action' => 'view', $this->Article->id));
pr($this->Article->id);
exit;
$this->Session->setFlash (__ ('Article "' . $this->request->data ["Article"] ["name"] . '" has been saved'));
} else {
$this->Session->setFlash (__ ('The article could not be saved. Please, try again.'));
}
}
$items = $this->Article->Item->find ('list');
$payments = $this->Article->Payment->find ('list');
$shippings = $this->Article->Shipping->find ('list');
$this->set (compact ('items', 'payments', 'shippings'));
}
我怎樣才能得到futre ID,而無需事先保存?可能嗎?這就是爲什麼我不會有這個保存兩次! –