In my previous article about the Yii 2.0, I introduced the ActiveRecord implementation. Once you have some data in your database you’ll want to be able to show it. Yii uses DataProviders to interact with data sources and it provides some widgets to output the data. Of these, the ListView and GridView provide the most functionality.
The example
I’m going to work with the database example that I introduced in my previous article again. Not very original, but quite useful as an example; a blog like SitePoint.
A small wrap-up of this database design and its Yii models which I’ll be using in this example:
- The
Authors
model has one relation method callgetArticles()
that provides anArticles[]
attribute to the model. - The
Articles
model has one relation method callgetAuthor()
that provides theAuthor
and a methodgetTags()
that provides theTags[]
. - The
Tags
model has one relation method callgetArticles
that providesArticles[]
I won’t be using the ArticlesTags table since it is just used for an N:M relation. Of course, there is a model available for it which is used in the relation definition of the other models.
DataProviders
There are three different types:
- The
ActiveDataProvider
is fed through an ActiveQuery instance and usually holds an array of models. You’ll normally use it to hold and render data that can be built from normal ActiveRecord instances with their relations. - The
ArrayDataProvider
is built using an array of data. This is quite useful when building all sorts of reports based on multiple models and/or custom aggregations. - The
SqlDataProvider
is a very useful one for the SQL masters among us. It is an easy way to get the data of those complex queries.
They don’t have huge differences, except slightly in views. The data provider itself is a base component that holds data and handles pagination and sorting.
Continue reading %Rendering Data in Yii 2 with GridView and ListView%
by Arno Slatius via SitePoint
No comments:
Post a Comment