Pergunta

I am learning YII framework and I am at a beginners level.

I created a CRUD option and I found the following script my view file

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
)); ?>

But I don't understand How $dataprovider got the data from data base, I didn't see any related function on the Model class, also how can I update the data is $dataprovide, ( I have to replace one foreign key with correct data ie family_id with family_name)

Foi útil?

Solução

Yii Data providers (there are many, the easiest way is to use CActiveDataProvider) are just data containers that can be used, for example, to populate a grid (CGridView widget, for example) or

The controller is the part of your application that feeds data into your views. You probably have a line or two of code in your controller where you create this data provider so you can send it to your view.

You can create a Data Provider by yourself (documentation of class CActiveDataProvider), just by instantiating it, but if you used gii or some simlitar device to generate models, then your model class should have a search() method. This is a helper method created for you so that you don't have to instantiate the data provider yourself. (when this method is called, a new CActiveDataProvider is returned)

As I said, a data provider is just a container of models. In your case, (as you are creating a CListView), it probably contains just one Model (the model whose details you are trying to display). Data providers can also be used for grids and in that case one data provider will hold not one but many models (in general, one per grid row).

p.s.: If you need to change things like foreign keys and/or attribute names, you need to do it in your Model class (that is, your ActiveRecord), not in the data provider. The data provider - and any widgets you use - are only used for displaying purposes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top