My AddressController:
public function actionIndex() {
$searchModel = new AddressSearch;
$dataProvider = $searchModel->search($_GET);
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
My model AddressSearch:
class AddressSearch extends Model {
public function search($params) {
$dataProvider = new SqlDataProvider([
'sql' => '
SELECT
name
FROM...
View:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'name2',
I have an attribute called name
in this dataProvider
. I would like to create a new virtual attribute called name2
out of name
by preg_replace()
-ing a few parts of it.
The function itself is working. I have tried a lot of different things, but I still can't make it to fill the attribute name2
with data. name2
is always empty. Can you please point me to the right direction? Many thanks!
UPDATE: based on the brilliant ideas of @Imaginaroom and @rob006 I've done the following:
- moved
getName2()
and the attributes I'm filling withSqlDataProvider
from base modelAddress
toAddressSearch
- deleted the empty base model
Address
because I don't need it anyway. Less is more! in
search()
I've added:foreach ($dataProvider->getModels() as $row) { $model = new AddressSearch; $model->setAttributes($row, false); $models[] = $model; } $dataProvider->setModels($models);
It works! Many thanks guys! It's fantastic that you are there and help!!!