I’ve just started with Yii 2 weeks ago. And Yes It Is awesome ! I’m allmost done with the blog tutorial (nice one) and a lot of other tutorials and youtube video’s. Already I see there are a lot of benefits using this framework.
Now I’m building a website and I doubt what a good approach would be. Simplefied the design is like this:
Except for the homepage, it all works fine. I’ve used the gii to create models and controllers and customized them later. But on the homepage, I want to show the last 10 items by date. So the last 10 Blogs, Articles and Photobooks together. What would be a good approach to do this?
I can create a table like Content en have a record for each Blog, Article and Photobook and in these 3 tables a foreign key ‘content_id’. Or is their a more simple way with Yii? I saw that it is possible to make relations with the relations() method.
I’m not asking for sourcecode, I would like that to do myself to keep learning, but a designation how this can be done in Yii would be nice.
One possible drawbacks of Gii CRUD is that people tend to misunderstand the MVC structure of Yii. In gii-generated CRUD the model is only used in the corresponding views and the controller. But it’s not neccessarily like that. You may use any models in any controllers and views. So if you want some page that uses multiple models, then just do it.
And having done some tutorials, I think it’s a very good time for you to read through the “Definitive Guide” once again.
@softark: Indeed, i’m reading the “Definitive Guide” once again and maybe one or two times after that. It makes much more sense now having some tutorials done.
@johnsnails: I try to read as much as I can and maybe will order the ebook you suggested.
@Pathfinder: this approach I would use before I saw Yii and other frameworks. The power of the framework in my opinion is that it’s full OOP and you don’t have to mess around with all kind of loose queries.
@waterloomatt: That’s more what I was looking for. I understand it, but it don’t cover it all. It will give me some lists beside eachother.
Please let me elaborate this:
Say I got the folowing records:
blog 1 - 01-01-2013
blog 2 - 05-01-2013
blog 3 - 20-01-2013
article 1 - 02-01-2013
article 2 - 03-01-2013
article 3 - 30-01-2013
photobook 1 - 04-01-2013
photobook 2 - 04-01-2013
photobook 3 - 29-01-2013
On the homepage I want to show the last 10 items, with title, description, read more url and depending on the type of content a images preview. And in order it would be like this:
Ok, i’ve been working on other Yii stuff lately, but today I want to take a look on this item.
So I came up with this approach after all:
First a Union query as suggested. In that query I add a ‘type’ column for every table.
I tried to create it with the querybuilder, but I can’t add an identity column as far as I could see:
->select("id,'blog' AS table_type")
So i skipped the querybuilder part and use the createCommand instead.
Then I loop through the results and build an Array with the object depending on the ‘type’. In the view I’ve got an switch again and it works fine like this.
public function actionIndex()
$recent_items = Yii::app()->db->createCommand("SELECT id,update_time,'article' AS type FROM articles
UNION SELECT id,update_time,'blog' AS type FROM blogs
UNION SELECT id,update_time,'photobook' AS type FROM photobooks
ORDER BY update_time
$homepage_items = array();
foreach($recent_items as $item)
$item_obj = Articles::model()->findByPk($item['id']);
$item_obj = Blogs::model()->findByPk($item['id']);
$item_obj = Photobooks::model()->findByPk($item['id']);
$homepage_items = array('type'=>$item['type'],'obj'=>$item_obj);
To me it seems to be to much the old way I used PHP. I thought it could be done better in Yii, but maybe it don’t has to be more complicated than this?
Probably we could do some performance optimization by retrieving the data all at once, doing something like this:
article_content as content,
NULL as image_file
'article' AS type
blog_content as content,
NULL as image_file
'blog' AS type
NULL as content,
'photobook' AS type
ORDER BY update_time
And passing not an array of objects but an array of records to the view …
But there are pros and cons about this kind of optimization.
I think the current solution of yours is not bad at all.
I understand your suggestion for passing the records directly to the view, but I can’t do that because the Objects like Blog and Photobooks also contains a lot of extra info, like attached images and so. The only thing in common is a Title and Description.