Translation, components and other questions

Hello everybody

Sorry for my bad english.

I started a new website with Yii. It's a pretty cool framework with a lot of possibility. I previously worked with Zend Framework, Code Igniter or Cake PHP but Yii are my favorite.

I have some questions.

  1. I do not really understand how working Events with onSomething function. Where could I find a complete exemple ?

  2. I want to creat a global control of my application. In each controller I have the same 2 methods: actionAdd and actionList. Only authorized user could use this methods. I want to use "Role-Based Access Control" but the controll need to be execute before or after each routing because I don't want to write the same filter in each controller. In Cake Php you need to creat a file called app_controller.php to the root of your application with two methods: beforeFilter() and afterFilter(). In Zend Framework you could creat Plugin with preDispatch() and postDispatch method. This plugin are include in the workflow in the bootstrap. My question are: how I could do that with Yii: I must create a global controller (with filter) which inherits the controller of my application or there is an other way ?

  3. My last question. I use CHtml::activeLabel to creat my form. The problem are the following: my models are in english but my website are in french. My label are also display in english and my error message are a mix beetwen the both ("Name ne peut être vide" with "Name" in englsih and "ne peut être vide" in french) How I could translate model attribute ?

I try to post in english because the french topic are not realy alive. But I will translate my post and your response to resurrect it.

Thanks a lot.

Auto-response

  1. Using attributeLabels() method  ;)
  1. Events are mainly used to change the execution of an existing component. Please check http://www.yiiframew…component-event for more details.

  2. You can create a base controller class. All your other controllers extend from this base class. In the base class, you specify the security filters.

Thanks a lot for your responses.

I have an other question about Event: it's possible to declare event in configuration like that ?

// application components


	'components'=>array(


		'db'=>array(


			//uncomment the following to use MySQL as database


			'connectionString'=>'mysql:host=localhost;dbname=mydb',


			'username'=>'root',


			'password'=>'',


			'charset'=>'UTF8',


		),


		'urlManager'=>array(


		    'urlFormat'=>'path',


		    'urlSuffix'=>'.html',


		),


		'activeRecord'=>array(


			'class'=>'CActiveRecord',


			'onBeforeSave' => array('MyObject', 'MyMethod'),


		),	


	),


My goal is before  each insert or update add the date in two fields. MyMethod could be like that.

	


public function MyMethod(CEvent $event) 


	{


		$record = $event->sender;


		


		if($record->hasAttribute('created') && $record->hasAttribute('updated')) {


			$date =date('Y-m-d h:i:s');


			if($record->isNewRecord) {


				$record->created = $date;


			}


			$record->updated = $date;


		}


		


		$this->raiseEvent('onBeforeSave', $event);


	}

This method works if I put it derectly in the Model class and if I call it onBeforeSave. But I could be better if it become more generic.

No, you can't because there is no 'activeRecord' app component.

You may consider creating a base AR class and override its beforeSave() method. Your other AR classes then extend from this base class.

Thanks a lot.