jeudi 12 mars 2015

How to apply custom fields' names in related data in Yii2?


Vote count:

0




Inside model (Products for example) I have public function fields to mask DB fields



public function fields()
{
$fields = [];
$fields["name"] = "p_name";
$fields['price'] = "p_price";
return $fields;
}


This works great. When I request data from model I always get name and price attributes only. However this products are related via relation table and I get theme like this (still inside this model):



public function getUserFriends()
{
return $this->hasMany(Products::className(), ['id' => 'id_pr_2'])->viaTable("product_rel", ['id_pr_1' => 'id'])->where->asArray();
}


Which works great but does not apply fields function. I know that there could be solution to call function fields and set select for those fields only.



$select = [];
foreach($this->fields() as $out_field => $db_field) {
$select[] = $db_field." AS ".$out_field
}
$selectString = implode(", ", $select);


As far as I know YII2 this is so not OK! I believe there should be generic solution. Any idea?



asked 1 min ago

M.V.

717






How to apply custom fields' names in related data in Yii2?

Aucun commentaire:

Enregistrer un commentaire