Yii Framework Forum: CBehavior, how to use? - Yii Framework Forum

Jump to content

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

CBehavior, how to use? CBehavior in real world Rate Topic: -----

#1 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 25 April 2010 - 04:57 PM

I am using behavior in model extending CActiveRecordBehavior and understand how it works, but in controller I don't understand how to add a behavior. Somebody can post one example from how to use it? I've read this cookbook http://www.yiiframew...oc/cookbook/44/ and it's like abstract to me!
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,778
  • Joined: 17-January 09
  • Location:Russia

Posted 26 April 2010 - 05:15 AM

You can do it two ways.

First, you can use AR's http://www.yiiframew...ehaviors-detail.

Second is:
$post = Post::model();
$post->attachbehavior('tags', new ETaggableBehavior());
$post->tags = 'Yii, PHP, behavior';
$post->save();

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 26 April 2010 - 07:56 AM

What's the diference between attachBehavior and
	public function behaviors()
	{
		return array(
			'UploadActiveRecordBehavior'=>'application.behaviors.UploadActiveRecordBehavior',
		);
	}


In this behavior I have...

  • beforeValidate event
  • afterDelete event
  • afterSave event


... to control upload and delete on save the model.

In this case I have two behaviors, one in model, that I typed above, and another that I'm typing a lot:

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

		$criteria=new CDbCriteria;
		$criteria->condition='dicaId='.$model->id;
		$criteria->order='ordem ASC';
		$midia=Midia::model()->findAll($criteria);

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

	public function actionUpload()
	{
		Yii::app()->session->sessionID=$_POST['PHPSESSID'];
		Yii::app()->session->init();

		$uploadedData=$_FILES['Filedata']['tmp_name'];

		$midia=new Midia;

		$pathFile=param('dicaPath');
		$midia->dicaId=$_GET['id'];
		$midia->ordem=Midia::model()->count('dicaId='.$midia->dicaId);

		// Pasta thumb
		$pathThumb=$pathFile.DS.param('thumbsFolder');

		// Nome do arquivo que serĂ¡ salvo
		$midia->url=md5(uniqid(microtime())).'.'.param('extDefault');

		if($midia->save())
		{
			// Fotos
			f($pathFile)->createDir();
			Yii::app()->thumb->setThumbsDirectory($pathFile);
			Yii::app()->thumb
			->load($uploadedData)
			->resize(param('photoWidth'), param('photoHeight'))
			->save($midia->url, param('extDefault'));

			// Thumbs
			f($pathThumb)->createDir();
			Yii::app()->thumb->setThumbsDirectory($pathThumb);
			Yii::app()->thumb
			->load($uploadedData)
			->resize(param('thumbWidth'), param('thumbHeight'))
			->save($midia->url, param('extDefault'));
		}

		$criteria=new CDbCriteria;
		$criteria->condition='dicaId='.$midia->dicaId;
		$criteria->order='ordem ASC';
		$model=Midia::model()->findAll($criteria);

		return $this->renderPartial('_ajaxMidia', array('model'=>$model));
	}

	public function actionOrdenarMidias()
	{
		$ordem=$_POST['ordem'];
		vardump($_POST);

		if(isset($ordem))
		{
			foreach($ordem as $i=>$id)
			{
				$model=Midia::model()->findByPk($id);
				$model->ordem=$i;
				$model->save();
			}
		}
	}

	public function actionApagarMidia()
	{
		$id=$_GET['id'];
		$itens=$_POST['ordem'];

		$criteria1=new CDbCriteria;
		$criteria1->addCondition('dicaId='.$id);

		if(not_null($itens))
		{
			$lista_itens=array();
			foreach($itens as $ordem=>$_id)
			{
				$lista_itens[]=$_id;
			}

			$criteria1->addNotInCondition('id',$lista_itens);
		}

		if(not_null($deleteItem=Midia::model()->find($criteria1)->id))
		{
			f(param('dicaPath').DS.Midia::model()->findByPk($deleteItem)->url)->delete();
			f(param('dicaPath').DS.param('thumbsFolder').DS.Midia::model()->findByPk($deleteItem)->url)->delete();

			Midia::model()->deleteByPk($deleteItem);
		}

		$criteria2=new CDbCriteria;
		$criteria2->condition='dicaId='.$id;
		$criteria2->order='ordem ASC';
		$model=Midia::model()->findAll($criteria2);

		return $this->renderPartial('_ajaxMidia', array('model'=>$model));
	}


How I want to reuse this code in several controllers, then I think that behavior will help.
Am I correct?
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,778
  • Joined: 17-January 09
  • Location:Russia

Posted 26 April 2010 - 11:52 AM

No. Check http://www.yiiframew...doc/api/CAction and http://www.yiiframew...pi/CController.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#5 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 26 April 2010 - 12:02 PM

View Postsamdark, on 26 April 2010 - 11:52 AM, said:



Humm... Now I understand that what I need is divide my controller in so many parts to reuse, thanks!
0

#6 User is offline   jgarberi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-December 13

Posted 07 January 2014 - 03:28 PM

great explanation, thanks!

View Postsamdark, on 26 April 2010 - 05:15 AM, said:

You can do it two ways.

First, you can use AR's http://www.yiiframew...haviors-detail.

Second is:
$post = Post::model();
$post->attachbehavior('tags', new ETaggableBehavior());
$post->tags = 'Yii, PHP, behavior';
$post->save();


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