YII2 数据库配置&&数据模型分配视图
1.打开 config/db.php
<pre name="code" class="html"><?php return [ "class" => "yiidbConnection", "dsn" => "mysql:host=localhost;dbname=yii2", "username" => "root", "password" => "", "charset" => "utf8", "tablePrefix"=>"t_" ];2.创建模型类
<?php /* 数据模型 */ namespace appmodels; use yiidbActiveRecord; class Admin extends ActiveRecord { public static function tableName() { return "{{%admin}}"; } }
3.控制器引入模型类 查询一条数据并分配视图
<?php namespace appcontrollers; use yiiaseController; use appmodelsAdmin; //引入Model类 class IndexController extends Controller { public function actionIndex() { $model = new Admin; $data = $model->find()->one(); //查询一条数据 return $this->render("index",array("row"=>$data)); //数据分配模板 } }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: TP5 更新操作