Blog

Custom Finds Behavior

Esse Behavior foi criado para economizar tempo com finds padrões da aplicaçao, vai ajudar a economizar algumas linhas de códigos e deixar sua aplicação mais elegante.
Vamos ao que interessa..

Instalação

Baixe o behavior no github http://github.com/mobvox/CustomFinds e coloque o arquivo custom_finds.php dentro do diretório behaviors.

Na Prática

Bom, não vou explicar muito como você pode usar o behavior, mas com os códigos abaixos já vai te dar uma boa ideia das possibilidades que o behavior te oferece.

Model

<?php
class Product extends AppModel{
    //...
    var $actsAs = array('CustomFinds');

    var $customFinds = array(
        'topSellers' => array(
            'fields' => array('Product.name','Product.price', ...),
            'contain' => array('ProductImage.source'),
            'conditions' => array('Product.countSeller >' => 20, 'Product.is_active' => 1),
            'recursive' => 1,
            //All others find options
        )
    );
    //...
}
?>

Controller

 'topSellers',
        //'conditions' => array(...),
        //...
    );
    function index(){
        $findAll = $this->Product->find('all', array('custom' => 'topSellers', 'conditions' => array('Product.category_id' => 2)));
        $findFirst = $this->Product->find('first', array('custom' => 'topSellers'));
        $findCount = $this->Product->find('count', array('custom' => 'topSellers'));
    }
    //...
}
?>

Bom, se tiver alguma dúvida ai da uma perguntado nos comentários ou aperece no canal do #cakephp-pt na rede freenode que eu to sempre por la.

Autor: Daniel L. Pakuschewski (eu) e Ariel Patschiki

No related posts.

Deixe seu Comentário

Let us know your thoughts on this post but remember to place nicely folks!