Yii Framework Forum: Basic question on extending methods - Yii Framework Forum

Jump to content

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

Basic question on extending methods Rate Topic: -----

#1 User is offline   chuntley 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 23-April 10

Posted 26 April 2010 - 05:40 PM

I'm working with the tutorial posted about file uploads: http://www.yiiframew...doc/cookbook/2/

I have the code working that I placed into actionCreate(), however, I have to duplicate that code in actionUpdate()

I tried making another method above those two to prevent code duplication called: paperUpload(), but I have no idea how to call it using Yii (or if I'm even doing this correctly)

Here is the code:

public function paperUpload()
        {
            $model=new Paper;
            
            $file=CUploadedFile::getInstance($model, 'filename');
            $model->image->saveAs('C:/Users/chuntley/ElementDesign/www/dev/files/test.jpg');
        }

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Paper;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Paper']))
		{
			$model->attributes=$_POST['Paper'];

                        if($model->save())
                        {
                            paperUpload();

                            $this->redirect(array('view','id'=>$model->papid));
                        }
		}

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

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel();

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Paper']))
		{
			$model->attributes=$_POST['Paper'];
			if($model->save())
                        {
                            paperUpload();


                            $this->redirect(array('view','id'=>$model->papid));
                        }
				
		}

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

0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 26 April 2010 - 06:35 PM

instead of making this call:

paperUpload();

you should make this call:

$this->paperUpload();
0

#3 User is offline   chuntley 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 23-April 10

Posted 27 April 2010 - 01:08 PM

View PostOnman, on 26 April 2010 - 06:35 PM, said:

instead of making this call:

paperUpload();

you should make this call:

$this->paperUpload();


I made that change, but now I'm getting the error: Property "Paper.image" is not defined.
0

#4 User is offline   Spyros 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 461
  • Joined: 14-April 09
  • Location:Athens - Greece

Posted 27 April 2010 - 10:51 PM

Does your Paper model have an image attribute (an image column in Paper tabe or define "public $image;" in the model class)?
0

#5 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 28 April 2010 - 03:05 AM

View Postskyer2000, on 27 April 2010 - 01:08 PM, said:

I made that change, but now I'm getting the error: Property "Paper.image" is not defined.


Instead of $model->image->saveAs(...); your should do:

$file->saveAs(...);
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