How can I display "arrendatario_nombre" using a dataprovider in a Clistview
$dataProviderContratos = new CActiveDataProvider(ZfContratos::model(), array(
'keyAttribute'=>'zf_contrato_id',
'criteria'=>array(
'order' => 'contrato_fecha_ini',
'select' => 't.*, arr.arrendatario_nombre as arrendatario_nombre',
'join' => 'LEFT JOIN zf_arrendatarios arr ON arr.arrendatario_id = t.zf_arrendatarios_arrendatario_id',
'condition'=>'zf_inmuebles_inmueble_id=-1',
),
));
I tried:
$data->arrendatario_nombre
Thanks!
You can use Yii relations for this purpose,
http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship
In your ZfContratos model file use the following and then you can call $data->arrendatario_nombre
public function relations() {
return array(
'arrendatario_nombre' => array(self::BELONGS_TO, 'zf_arrendatarios', 'zf_arrendatarios_arrendatario_id'),
);
}
Once this is declared in the model file you can use $data->arrendatario_nombre and also in any other place place you load the Zfcontratos model
Related
I have model with relations:
public function relations()
{
return array(
'fidistr' => array(self::BELONGS_TO, 'Distributors', 'fidistr_id', 'order'=>'fsname'),
'fitown' => array(self::BELONGS_TO, 'Town', 'fitown_id'),
'distributorsPointMails' => array(self::HAS_MANY, 'DistributorsPointMail', 'fidistr_point_id'),
'distributorsPointPhones' => array(self::HAS_MANY, 'DistributorsPointPhones', 'fidistr_point_id'),
);
}
I would like to order data by "fsname" from related table. I tried this:
$models = DistributorsPoint::model()->findAll('fitown_id=:id', array('id' => $_POST['city_id']));
but it still returns unsorted data. Help please.
The order on the relation is only used when lazy loading the relation. From the Yii guide on relations:
Note: when using eager loading such relation options as 'order',
'group', 'having', 'limit' and 'offset' will be ignored. You should
setup such parameters at the main model criteria level if you wish
them to be applied.
You can pass a criteria array instead of a string as the first parameter of findAll(). This will be used to initialize a CDbCriteria object. To order by a relation you need to eager load the relation using with():
$models = DistributorsPoint::model()
->with('fidistr')
->findAll(array(
'condition' => 'fitown_id=:id',
'order' => 'fidistr.fsname',
'params' => array(':id' => $_POST['city_id'])
));
I'm getting foreign key relation based display ids in the gridviews. How to get values instead of ids? Code in my gridview is following:
$criteria->compare('education.UniversityNameid',$this->UniversityName, true);
my gridviews inside code
array(
'name' => 'UniversityName',
'type' => 'raw',
'value'=>'(empty($data->education->UniversityNameid))? "" : Yii::app()->params["currencySymbol"]." ".$data->education->UniversityNameid',
),
You have to set up a relation in your model "University" like this
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'UniversityName' => array(self::BELONGS_TO, 'University', 'UniversityNameid'),
);
}
thank you can access the Name
$data->education->UniversityName
Set the relation in your model like
'u' => array(self::BELONGS_TO, 'University', 'UniversityNameid'),
and acess it like
'attributes'=>array(
array('name'=>'u.UniversityName',
'label'=>'University',),
),
I want to export data to excel using 2 models. HasilPutusan model and DetilSaksi model.
When I used the CActiveDataProvider with Model HasilPutusan, I managed to get all the data together with the data relationships that are interrelated with this HasilPutusan models.
And how do I combine the data both by using Model DetilSaksi?
My controller :
public function actionCetakjalanperistiwa()
{
if(isset($_POST['PrintJaper'])){
$data = $_POST['id_kecelakaan'];
if($data==''){
return false;
}else{
$dataProvider=new CActiveDataProvider('HasilPutusan', array(
'criteria'=>array(
'condition'=>'id_kecelakaan=:id',
'params'=>array(':id'=>$data),
),
));
}
$this->widget('ext.EExcelView', array(
'grid_mode'=>'export',
'title' => 'Data Jalan Peristiwa',
'dataProvider' => $dataProvider,
'exportType'=>'Excel2007',
'columns'=>array(
'id_hasil_putusan',
'id_detil',
'id_kapal',
'idKapal.nama_kapal',
'idKapal.tahun_pembuatan',
'idKapal.konstruksi',
'idKapal.isi_kotor',
'idKapal.tenaga_penggerak_utama',
'idKapal.pemilik',
'idKapal.nakhoda',
'idKapal.awak_kapal',
'idKapal.surat_kapal',
),
));
}else{
$this->render('_japer');
}
}
In my tbl_hasil_putusan table, i have a id_detil field which I will use to re-do the query to the second table using the DetilSaksi model.
My database :
How do I combine the two tables that can be joined so that I can call related (blue arrow) data in the second table?
Thanks
You can use join in the criteria array. Something like this:
$dataProvider=new CActiveDataProvider('HasilPutusan', array(
'criteria'=>array(
'condition'=>'id_kecelakaan=:id',
'select' => 't.*, ds.*'
'join' => 'LEFT JOIN tbl_detil_saksi ds on ds.id_detil = t.id_detil',
'params'=>array(
':id'=>$data),
),
));
hi guys i am new to cakephp
now i'm facing a little big problem
here is the situation
i hava a shop that hasMany Catalogs which is related to many products each product has a category
i want to fetch them all just by getting the shop
i dont know how to do it
trying to use hasMAny gives me just the ids
instead is there any way to get shop inside it array of catalogs each catalog has Product's array which has one array of category
thank you
Ok, I'm on my computer now :).
In ShopModel:
class ShopModel extends AppModel {
public $hasMany = array(
'Catalog' => array(
// binding params here...
),
);
}
In CatalogModel:
class CatalogModel extends AppModel {
public $hasMany = array(
'Product' => array(
// binding params...
),
),
}
... and this goes on...
If you don't want to get excessive data in all actions, you should set in AppModel:
class AppModel extends Model {
public $recursive = -1;
}
In the controller action where you call the find function with associations:
$this->Shop->Behaviors->load('Containable');
$big_array = $this->Shop->find('all', array(
'conditions' => array(
//...
),
'contain' => array(
'Catalog' => array(
'Product' => array(
// etc, you get the point :)
),
),
),
));
It is also nice to declare the $belongsTo associations too, so you can access anything from anywhere, something like this:
$this->Catalog->Behaviors->load('Containable');
$big_array = $this->Catalog->find('all', array(
'conditions' => array(
//...
),
'contain' => array(
'Product' => array(
// ...
),
'Shop' => array(
// ...
),
),
));
EDIT
I see you have a Product->Category relation that i guess would be defined with $belongsTo. If you do a query like the one above, you will get a lot of duplicate queries (same category in many products). You can use $this->Category->find('list') but very often I find this inappropriate as it is returning only one field (I would be grateful if someone knows a way how can I get more fields with the list type). For this purpose, my workaround is making a custom function in the Category model like this:
class Category extends AppModel {
public function getSorted ($options = array()) {
$temp= $this->find('all', $options);
$output = array();
foreach ($temp[$this->alias] as $row) {
$output[$this->alias][$row['id']] = $row;
}
unset($temp);
return $output;
}
}
Then in the controller I would declare two arrays, the big one without category association and the category list one:
$this->loadModel('Category');
$this->set('categories', $this->Category->getSorted());
This way, I can get the needed category row by category id wherever i need it in the view.
Do not use CakePHP association they are not good handling complex relationships, you will later face problems with....Instead create all your join queries on the fly...I am giving you one example below:
Create one function inside Shop model and join catalog and product as shown below:
$options = array(
'conditions' => array('Product.id'=>9),
'joins' => array(
array(
'alias' => 'Catalog',
'table' => 'catalogs',
'type' => 'LEFT',
'conditions' => array(
'Catalog.product_id = Product.id',
),
),
array(
'alias' => 'Product',
'table' => 'products',
'type' => 'LEFT',
'conditions' => array(
'Shop.id = Product.shop_id',
),
)
),
'fields' => array('Product.*'),
'group' => array('Product.id')
);
$returnData = $this->find('all',$options);
This will make coding little easier and you can escape from associations!
Suppose I have model User which have many to many relation to itself named as friends.
so $user->friends (or $model->friends in view) gives me an array of User objects. I wanted to display the friends as gridview. But CGridView data as dataProvider object. Googling for it found the way to convert array of model objects to dataProvider object as given below.
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' => new CArrayDataProvider($model->friends, array()),
));
Now using this I get an error
Property "User.id" is not defined.
UPDATE
public function relations()
{
return array(
'friends' => array(self::MANY_MANY, 'User', 'friendship(user_id, friend_id)'),
);
}
I use two stage building the provider shown below. But I found that it gives you trouble in terms of Pagination. I have not bothered to resolve that problem since am doing other things
$dataProvider = new CArrayDataProvider('User');
$dataProvider->setData($model->friends);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' =>$dataProvider,
));
That being said, your code should work (see the example below from API docs). I suspect there is wrong attribute in your relations than the provided code. Re-check the relation definition if it is ok
From Yii docs:
$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
// or using: $rawData=User::model()->findAll(); <--this better represents your question
$dataProvider=new CArrayDataProvider($rawData, array(
'id'=>'user',
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
Got it :) , i can use CActiveDataProvider instead of CArrayDataProvider as given below
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'gridUser',
'dataProvider' => new CActiveDataProvider('User', array(
'data'=>$model->friends,
)),
//...... columns display list.....
));
Anyways thanks for the reply #Stefano