Tagged: php RSS Toggle Comment Threads | Keyboard Shortcuts

  • davi 12:57 am on January 13, 2012 Permalink | Reply
    Tags: active record, , extension, php, ,   

    AttributesBackupBehavior extension for the Yii Framework 

    Sometimes, when using ActiveRecord, we need to know if some attribute value has changed after it was
    loaded from the database. To make things easier and avoid duplicated code, I created this behavior
    to help me with this task. Basically, what it does is to make a copy of all attributes values from
    the object, right after it is loaded from the database, so the behavior can work with this original
    values to know if any of them was changed.

    Usage
    The behavior usage is pretty simple. First, add the behavior to the ActiveRecord where you intend to
    use it:

    <?php
    class User extends ActiveRecord {
    	...
    	public function behaviors() {
    		return array(
    			'AttributesBackupBehavior' => 'ext.AttributesBackupBehavior',
    		);
    	}
    	...
    }
    ?>
    

    After this, 3 new methods will be added to your User class: attributesChanged(), attributeChanged()
    and getOldAttributeValue().

    Below is a description of it:

    attributesChanged()
    With this method you can check if any of the object attributes was changed after it was loaded from
    the database:

    <?php
    $user = User::model()->find(); // User->status == 'Active'
    var_dump($user->attributesChanged()); // FALSE
    $user->status = 'Inactive';
    var_dump($user->attributesChanged()); // TRUE
    ?>
    

    attributeChanged()
    This should be used when you want to know if a specific attribute value was changed:

    <?php
    $user = User::model()->find(); // User->status == 'Active'
    $user->status = 'Inactive';
    var_dump($user->attributeChanged('email')); // FALSE
    var_dump($user->attributeChanged('status')); // TRUE
    ?>
    

    getOldAttributeValue()
    In case you need to retrieve the original value loaded from the database, this is the method you gonna use:

    <?php
    $user = User::model()->find(); // User->status == 'Active'
    $user->status = 'Inactive';
    var_dump($user->getOldAttributeValue('status')); // 'Active'
    ?>
    

    Besides those three methods, there’s also one property that can be configured in this behavior:
    $reloadAfterSave.

    By default, after you call the ActiveRecord save method, the old attributes will be erased and filled
    with the just saved new ones, show it will reflect the stored record. If for some reason you want
    to keep the original loaded values, you need to set $reloadAfterSave as false:

    <?php
    class User extends ActiveRecord {
    	...
    	public function behaviors() {
    		return array(
    			'AttributesBackupBehavior' => array(
    				'class' => 'ext.AttributesBackupBehavior',
    				'reloadAfterSave' => false,
    		);
    	}
    	...
    }
    ?>
    

    You can donwload the extension from its page at the Yii Framework site: http://www.yiiframework.com/extension/attributesbackupbehavior/

    You can also fork the projet at Github: https://github.com/davialexandre/AttributesBackupBehavior

     
  • davi 1:36 pm on December 2, 2011 Permalink | Reply
    Tags: , php, , ,   

    Trabalhando com contadores utilizando o ActiveRecord no Yii Framework 

    Vamos supor, por exemplo, que você está desenvolvendo um blog ou algum outro tipo de CMS e quer registrar o número de vezes que cada post foi acessado (talvez para mostrar uma lista dos mais visitados).

    A maneira mais fácil de fazer isso é adicionar uma coluna na tabela de posts, que será utilizada para armazenar o número de visitas para aquele item. Cada vez que um post for exibido o valor dessa coluna será aumentado em 1. O código para fazer isso é algo parecido com isso:

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->visitas += 1;
        $post->save();
        $this->render('view', array('post' => $post));
    }
    

    Essa abordagem tem dois problemas. Queremos atualizar apenas a coluna visitas, mas todo o registro do post será atualizado. Além disse, caso o argumento false não seja passado para o método save, todo o processo de validação será executado.

    Desde a versão 1.1.8, a classe CActiveRecord tem um método que pode nos ajudar com isso. Trata-se do método CActiveRecord::saveCounters() e a sua utilização é muito simples:

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->saveCounters(array('visitas'=>1));
        $this->render('view', array('post' => $post));
    }
    

    Com isso, somente a coluna visitas será atualizada e a validação não será disparada.

    Caso você ainda não utilize a versão 1.1.8, você pode utilizar o método CActiveRecord::updateCounters():

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->updateCounters(
            array('visitas'=>1),
            array('condition' => "id = :id"),
            array(':id' => $this->id),
        );
        $this->render('view', array('post' => $post));
    }
    

    O trecho de código acima faz exatamente o mesmo que o saveCounters. Note que o método updateCounters tem dois parâmetros a mais. Depois de informar a coluna utilizada para contar, é preciso passar uma condição que diz qual registro deverá ser atualizado. Se você utilizou algum parâmetro nessa condição (como o “:id”), você também deve informar o valor dele, utilizando o terceiro argumento do método.

    Importante: Se você não informar nenhuma condição, todos os registros serão atualizados.

     
  • davi 11:04 pm on December 1, 2011 Permalink | Reply
    Tags: php, , ,   

    Working with counters using the ActiveRecord in Yii Framework 

    Let’s say, for example, that you are developing a blog or some kind of CMS and you want to track the number of times each post was viewed (maybe to show a list of the most viewed ones).

    The easier way to do that is by adding a column to the post table, which will be used to store the number of visits of that item. Each time the post is displayed the value of this column will be increased by 1. The code for this would be something like:

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->visits += 1;
        $post->save();
        $this->render('view', array('post' => $post));
    }
    

    We have two problems with this approach. All we want is to update the visits column, but the entire record will be updated. Also, if we didn’t pass the false argument to the save method, all the validation process will be executed.

    Since 1.1.8, the CActiveRecord class has a method that can help us with this task. It’s the CActiveRecord::saveCounters() method and its usage is pretty simple:

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->saveCounters(array('visits'=>1));
        $this->render('view', array('post' => $post));
    }
    

    With this, only the visits column will be updated and the validation will not be triggered.

    If you’re not using the 1.1.8 yet, there’s the CActiveRecord::updateCounters() method:

    public function actionView($id) {
        $post = Post::model()->findByPk($id);
        $post->updateCounters(
            array('visits'=>1),
            array('condition' => "id = :id"),
            array(':id' => $this->id),
        );
        $this->render('view', array('post' => $post));
    }
    

    The piece of code above does exactly the same that saveCounters does. Note that updateCounters has two additional parameters. After the column used to count, we pass a condition to tell which record(s) should be updated. If you used any paramater in this condition (like the “:id”), you should pass the parameter value in the third argument of the method.

    Important: If you do not pass a condition, all records will be updated.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel