Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Perrito extends CActiveRecord {
- // $id, $name
- }
- class PerritoCorrea extends CActiveRecord {
- // $perrito_id, $correa_id
- }
- class Correa extends CActiveRecord {
- // $id, $color
- public function listarPorPerrito($nombre_perrito){
- $c = new CDbCriteria();
- // queremos buscar las correas del "Perrito.name" igual a "KIM",
- // pero pasando por toda la relacion:
- //
- // select * from Correa 't' <-- CDbCriteria pone la 't' automaticamente
- // left join PerritoCorrea PXC on PXC.correa_id = t.id
- // left join Perrito PX on PX.id = PXC.perrito_id
- // where
- // PX.name like '%KIM%'
- $px = Perrito::model()->tablename();
- $pxc = PerritoCorrea::model()->tablename();
- $c->join =
- 'left join '.$pxc.' PXC on PXC.correa_id = t.id'
- .'left join '.$px.' PX on PXC.perrito_id = PX.id'
- ;
- $c->compare('px.name',$nombre_perrito,true); // <-- 'true' indica "Like"
- return new CActiveDataProvider($this,array(
- 'criteria'=>$c,
- 'pagination'=>array('pageSize'=>5),
- // el sort no lo pongo para no sacar de foco el ejemplo, pero considera
- // que tu criteria $c dispone de PX.id, PX.name (PX es Perrito), por
- // tanto puedes usar esos nombres para ordernar. experimentar con eso.
- ));
- }
- }
- [COMO USAR]
- // protected/views/xxx/correas.php
- $this->widget('zii.widgets.grid.CGridView', array(
- 'dataProvider'=>$dataProvider,
- ));
- // protected/controller/xxxController.php
- public function actionListarCorreas($perrito){
- $dp = Correa::model()->listarPorPerrito($perrito);
- $this->render('correas',array('dataProvider'=>$dp));
- }
- // ejemplo:
- index.php?r=/xxx/listarcorreas&perrito=kim
- // con ajax:
- $.ajax({
- url: 'index.php?r=/xxx/listarcorreas&perrito=kim',
- cache: false, type: 'get',
- success: function(html){
- // $.fancybox(html); <- si usas fancybox
- // o a rin pelao usando un <div id='ventanita'></div>
- $('#ventanita').html(html);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement