Posts Tagged with yii

Displaying 61-70 of 113 results.
Wiki: Yii on update, detect if a specific AR property has been changed on beforeSave()wiki
posted by admin on April 23, 2016
You need to store the old attributes in a local property in the AR class so that you can compare the current attributes to those old ones at any time.

Step 1. Add a new property to the AR class:

// Stores old attributes on afterFind() so we can compare
// against them before/after save
protected $oldAttributes;


Step 2. Override Yii's afterFind() and store the original attributes immediately after they are retrieved.

public function afterFind(){
$this->oldAttributes = $this->attributes;
return parent::afterFind();
}


Step 3. Compare the old and new attributes in beforeSave/afterSave or anywhere else you like inside the AR class. In the example below we are checking if the property called 'level' is changed.

public function beforeSave()
{
if(isset($this->oldAttributes['level']) && $this->level != $this->oldAttributes['level']){

// The attribute is changed. Do something here...

}

return parent::beforeSave();
}
Read more
Resolved: Yii isNewRecord is false in beforeSave?
posted by admin on April 23, 2016
I made a beforeSave method in my model (extends GXActiveRecord), the if isNewRecord never gets triggered. My beforeSave() gets called, though.

When I print the $this->isNewRecord variable, it's false. When is this variable set to false anyway? I'm very sure it's new

public function beforeSave(){


if(parent::beforeSave())
{
if($this->isNewRecord){
$this->setAttribute('doc_status','new');
print "something";
}else{
$this->setAttribute('doc_status','updated');
}
return true;
} else { return false;

}
Read more
Resolved: Yii CActiveDataProvider CListView pagination error
posted by admin on April 15, 2016
In Controller:

$dataProviderComments = new CActiveDataProvider('Comments',
array(
'criteria' => array(
'order' => 'id DESC',
),
'pagination' => array(
'pageSize' => 10,
'pageVar'=> 'page'
),
)
);

$this->render('/media/view', array('comments'=>$dataProviderComments));

In View:

<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$comments,
'itemView'=>'../comments/_comment',
'ajaxUpdate'=>false,
'template'=>'{sorter}{pager}{summary}{items}{pager}',
)); ?>


When my comments less than 10, all is right, but when will be pagination, start from 11, I see this error

Fatal error: Call to a member function getId() on null in C:\xampp\htdocs\avaim.ru\framework\web\CController.php on line 962
Read more
Resolved: Yii sum criteria select condition
posted by admin on April 15, 2016
I try to count my views sum count, by this way

$criteria=new CDbCriteria;
$criteria->select='SUM(visits) as SUMvisits';
$criteria->condition='media_id=:media_id';
$criteria->params=array(':media_id'=>$media_id);
$sBalance = Views::model()->find($criteria)->getAttribute('SUMvisits');
var_dump($sBalance);exit();


Something like this, but it is not workingRead more
Resolved: Yii check if user logged in of guest
posted by admin on April 14, 2016
I want to check my visitor type, guest or logged in user.Read more
Resolved: Yii CMenu - active class not rendering
posted by admin on April 12, 2016

'items'=>array(
array(
'label'=>'About',
'url'=>array('/contact')
),


My CMenu not generate active class.Read more
Wiki: Yii 1.1: Add id or class to CMenu itemswiki
posted by admin on April 12, 2016
CMenu comes with a lot of great customization options built in. One of the most used is the class 'active' being added to menu item. But, what if you want to add your own class or id to a menu item ?

It's very easy, you just need to use the 'itemOptions'.

If you want a menu like :

<ul id="myMenu">
<li id="first"><a href="#"><span>First</span></a></li>
<li id="second"><a href="#"><span>Second</span></a></li>
</ul>


Just do :

<?php $this->widget('application.components.MyMenu', array(
'id' => 'myMenu',
'items' => array(
array('label' => 'First', 'url' => array('#'), 'itemOptions'=>array('id' => 'first'), ),
array('label' => 'First', 'url' => array('#'), 'itemOptions'=>array('id' => 'second'), ),
),
));
?>
Read more
Resolved: Yii translation not working
posted by admin on April 12, 2016
My Yii app translation code is stop working

<?php echo Yii::t('t', 'Added'); ?>


My Messages folder and file:

- messages
- ru
- t.php
Read more
Resolved: Yii file save error Directory must be writable
posted by admin on April 11, 2016
I have form, and I create directory by mkdir

mkdir($mydirectory, 0777, true);


And got an error during save my file, which I try to upload from my _form.php

Directory must be writable
Read more
Resolved: Yii universal action in controller to missing actions methods
posted by admin on April 11, 2016
I have a controller in Yii:

class PageController extends Controller {
public function actionSOMETHING_MAGIC($pagename) {
// Commando will to rendering,etc from here
}
}


I need some magic method under Yii CController for controlling all subrequest under /page || Page controller.Read more