Updates from December, 2011 Toggle Comment Threads | Keyboard Shortcuts

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

    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: , , ,   

    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