duanpo2813 2014-10-23 07:35
浏览 79

Yii2模型初始化和会话处理

Here's a pickle :)
In the old Yii you could instantiate models anywhere in protected. Regardless of the module you were in. Then the Yii team decided to go and mess everything up :) and decided to change the structure and code and now, I'm a bit lost...

1. HOW DO YOU INSTANTIATE MODELS? (anywhere in the project)

Old fashion way was $model = new Model(); where model could be in a totally different module and it would still work. How do we do this now? when I try to do it, it says: Class 'app\modules\somemodule\controllers\Model' not found which is funny because I want a model and it searches in controllers...

2. SESSIONS IN YII

Old fashion way was

Yii::app()->session['var'] = 'value';
echo Yii::app()->session['var']; // Prints "value"

How are they done now?

L.E: Found my answer to the second question :D and sessions are handled about the same: Yii::$app->session['var'] = 'value'; the difference being the $... It's all about the $ :)

Thank you!
Ares.

  • 写回答

1条回答 默认 最新

  • dtmbc1606 2014-10-23 14:23
    关注

    Funny how no one showed interest in my question...

    Anyways, nothing changed about it. If you need new fresh instance it's just "new Post()". If you need to get AR model with data it's "Post::find->where(...)->one()"

    BUT you have to: Either import class from another namespace with use:

    use app\modules\someModule\models\Post;
    
    // ...
    
    $post = new Post();
    

    or use fully qualified class name:

    $post = new \app\modules\someModule\models\Post();
    

    Hope this helps others as well as it did me :D

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入