Yii Framework Forum: Update field yang berasal dari CUploadFile - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Update field yang berasal dari CUploadFile Rate Topic: -----

#1 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 543
  • Joined: 21-June 10

Posted 24 June 2010 - 01:53 AM

Mau tanya donk, saya kan upload file gambar dengan menggunakan CUploadFile. Nah masalahnya setiap kali saya update model, saya harus selalu upload gambar. Soalnya kalau tidak upload gambar maka di record tersebut field gambar nya akan NULL, padahal field sebelumnya sudah terisi.

Bagaimana caranya supaya ketika update, jika gambar tidak saya upload, maka dia tidak update field gambar yah? Saya menemukan thread disini hanya saja saya kurang memahami maksud dari postingnya...
http://www.yiiframew...pdate-a-record/
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#2 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 24 June 2010 - 02:59 AM

Belum pernah pake CUploadFile,

mungkin bisa cek beforesave dan membandingkannya dengan hidden field field gambar tersebut.



	protected function beforeSave()
	{
		if(parent::beforeSave())
		{

                if($this->upload === null)
                       $this->upload = $hiddengambar;
                else
                       $this->upload = $this->upload;
                       
		}
		else
			return false;
	}



func save() akan mengupdate semua field yang dideklarasikan di function rules(),
belum tau apakah ada scenario untuk set null utk fieldfield ? :blink:

saya biasa gunakan ckfinder utk upload2x file. :)
0

#3 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 543
  • Joined: 21-June 10

Posted 24 June 2010 - 04:03 AM

Thanks atas pencerahannya. Terakhir2 saya tidak coding seperti itu sih..
jadi saya buat sebuah hiddenField dengan nama yang sama dengan fileField() nya.
akhirnya dengan sendirinya dia bisa mengutamakan fileField(), kalau kosong baru ambil dari yang hiddenField()
Saya gak tau sih cara kerjanya seperti apa... tapi yang jelas jalan...
codingnya seperti begini :

	public function actionUpdate()
	{
		$model=$this->loadModel();

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
        
        
		if(isset($_POST['DekArticle']))
		{
			$model->attributes=$_POST['DekArticle'];

			//ambil gambar
			$image = CUploadedFile::getInstance($model,'image');
            if (is_object($image) && get_class($image)==='CUploadedFile') $model->image = $image;
			
			$model->update_date = date("Y-n-j h:i:s");
			if($model->save()){
				//uploading file
				if (is_object($model->image)){$model->image->saveAs(Yii::app()->basePath."/../images/stories/".$model->image->name);}
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}


Btw, CkFinder itu extension kah? soalnya saya sekilas googling kurang jelas itu apaan :P
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

#4 User is offline   fastcrash 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 10-March 10
  • Location:South Jakarta

Posted 24 June 2010 - 04:32 AM

ya intinya sama saja..

bedanya mas junxiong menaruh code nya di controller, bukan di model.

func beforeSave()ini biasanya di taruh di model (lihat demo blog)
        protected function beforeSave()
        {
                if(parent::beforeSave())
                {

                        //ambil gambar
                        $image = CUploadedFile::getInstance($model,'image');
                        if (is_object($image) && get_class($image)==='CUploadedFile') $model->image = $image;
                        
                        $model->update_date = date("Y-n-j h:i:s");

                }
                else
                        return false;
        }




ckfinder plugin untuk upload file yang digunakan oleh ckeditor.
0

#5 User is offline   junxiong 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 543
  • Joined: 21-June 10

Posted 24 June 2010 - 06:24 AM

oow... gak gitu masalah harusnya yah..

Oow CkEditor yah...
btw, thanks yoo bro Fastcrash :)
“The most likely way for the world to be destroyed, most experts argue, is by accident. That’s where we come in; we’re computer professionals. We cause accidents.” - Nathaniel Borenstein

Yii Playground : Collaborative demo apps. You can join to improve it too!
My Team's Blog: In Indonesian.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users