moka同学
- moka同学 2016-08-02 已签到连续签到1天,获得了5个金钱
- moka同学 2016-07-28 已签到连续签到3天,获得了15个金钱
- moka同学 回复了 return19931112 的回答
model里面关系都定义好了,干嘛还要在控制器里面写joinwith呢,如果ArticleCategory这个表里面有个字段是name,应该可以直接在视图层里面写'articleCategory.name'就可以了啊
才学yii2不知道,怎么使用model中的那个关系,所以用joinwith了。
$query = (new Query())->select('a.*,c.category as category') ->form('yii2_article as a') ->leftJoin('yii2_article_category as c', 'a.category_id = c.id') ->all();
谢谢你了。这样可以了。可能是我哪里不对。我又试出了另一种方法。
$sql = 'SELECT a.*,c.category as category FROM yii2_article a,yii2_article_category c WHERE a.category_id=c.id';$list = Yii::$app->db->createCommand($sql)->queryAll();
配置
[php] view plain copy
1. 'components' => [ 2. 'cache' => [ 3. 'class' => 'yii\caching\FileCache', 4. ],
数据缓存
[php] view plain copy
1. $cache = \Yii::$app->cache; 2. $cache['aa'] = '123'; 3. echo $cache['aa'];
片段缓存
[php] view plain copy
1. <h1>朝代</h1> 2. <?php if($this->beginCache($id, ['duration' => 3600])) { ?> 3. <?php 4. echo ListView::widget([ 5. 'dataProvider' => $dataProvider, 6. 'itemView' => '_item', 7. ]); 8. ?> 9. <?php $this->endCache(); } ?> 10. 11. $dependency = [ 12. 'class' => 'yii\caching\DbDependency', 13. 'sql' => 'SELECT MAX(updated_at) FROM post', 14. ]; 15. 16. if ($this->beginCache($id, ['dependency' => $dependency])) { 17. 18. // ... generate content here ... 19. 20. $this->endCache(); 21. } 22. 23. if ($this->beginCache($id, ['variations' => [Yii::$app->language]])) { 24. 25. // ... generate content here ... 26. 27. $this->endCache(); 28. } 29. 30. if ($this->beginCache($id1)) { 31. 32. // ...content generation logic... 33. 34. if ($this->beginCache($id2, $options2)) { 35. 36. // ...content generation logic... 37. 38. $this->endCache(); 39. } 40. 41. // ...content generation logic... 42. 43. $this->endCache(); 44. }
页面缓存
[php] view plain copy
1. class TestController extends Controller 2. { 3. public function actionIndex() 4. { 5. //$db = \Yii::$app->db; 6. sleep(2); 7. $query = Dynasty::find(); 8. $dataProvider = new ActiveDataProvider([ 9. 'query' => $query, 10. 'pagination' => [ 11. 'pageSize' => 15, 12. ], 13. ]); 14. return $this->render('index', [ 15. 'dataProvider' => $dataProvider 16. ]); 17. } 18. public function behaviors() 19. { 20. return [ 21. [ 22. 'class' => 'yii\filters\PageCache', 23. 'duration' => 60, 24. 'variations'=> [$_GET['page']], 25. ], 26. ]; 27. } 28. }
动态内容
[php] view plain copy
1. ...别的HTML内容... 2. <?php if($this->beginCache($id)) { ?> 3. ...被缓存的片段内容... 4. <?php $this->renderDynamic($callback); ?> 5. ...被缓存的片段内容... 6. <?php $this->endCache(); } ?> 7. ...别的HTML内容...
$query = new Query();
$query->select('*') //查询字段 ->from(Article::tableName.'a') //a 是表的别名 ->leftJoin(articleCategory::tableName.'ac','ac.category_id = a.id'); $data= $query->all();
sql是这样的$sql = 'SELECT a.*,c.category as category FROM yii2_article a,yii2_article_category c WHERE a.category_id=c.id';
,你上面这个我的还是不正确。- moka同学 2016-07-27 已签到连续签到2天,获得了10个金钱