一、新建数据库(db_movie1-t_movie)
二.连接数据库
目录(protected-config-main.php)
//本地的数据库
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=db_movie1', //mysql:host=125.222.222.73 本地数据库
'emulatePrepare' => true,
'username' => 'root',
'password' => 'mysql',
'charset' => 'utf8',
'tablePrefix' =>'t_',
),
记得修改本地数据库代码-(数据库名+用户名+密码)
三、定义 AR 类(创建model)
Active Record (AR) 是一种面向对象风格的,用于抽象数据库访问的设计模式.
要访问一个数据表,我们首先需要通过集成 CActiveRecord 定义一个 AR 类。
每个 AR 类代表一个单独的数据表,一个 AR 实例则代表那个表中的一行。
在models下新建Movie.php并继承CActiveRecord
<?php
/**
* Created by PhpStorm.
* User: DELL
* Date: 2018/8/1
* Time: 18:00
*/
class Movie extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 't_movie'; //return '{{movie}}'
}
}
tableName的方法记得要重写,有两种写法
四、控制器(Controller)
新建Movie的文件夹,在里面新建MovieController.php的文件
class NewsController extends Controller{
}
五、增删改查方法
列出所有信息
public function actionGetMovie(){
$list = Movie::model()->findAll(); //将t_movie表中全部信息存到 $list中
$this->smarty->assign('list',$list);
$this->smarty->display('movie/listAll.html');//跳到对应的html页面,内容展示
}
对应的HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" style="margin-top: 10px;font-size: x-large">
<thead> <tr> <th>电影</th>
<th>内容</th> </tr>
<tr>
</thead> <tbody> <{foreach from=$list item=movie}>
<td><{$movie.name}></td><td><{$movie.director}></td> <td>
<a href="/movie/Movie/GetDeleteMovie?id=<{$movie.id}>">删除</a>
<a href="/movie/Movie/GetMovieById?id=<{$movie.id}>">查看详情</a>
<a href="/movie/movie/ToUpdateMovie?id=<{$movie.id}>">修改</a>
<a href="/movie/Movie/ToAddMovie">添加</a>
</td> </tr> <{/foreach}> <tr> <td colspan="2" style="text-align: center" > </td> </tr> </tbody>
</table>
<form name="form" action="/movie/Movie/search" method="post">
<input type="text"style="width: 300px;height: 40px;border-radius:5px;border:1px;" name="keywords" placeholder="请输入您想查找的内容" autocomplete="off">
<input type="submit" value="搜索">
</form>
</body>
</html>
页面展示如下
增加
/**
* 跳转到增加电影页面
*/
public function actionToAddMovie(){
$this->smarty->display('movie/add.html');
}
/**
* 增加电影
*/
public function actionAddMovie2(){
$movie=new Movie;
$content=$_POST['content'];
$name=$_POST['name'];
$director=$_POST['director'];
$movie->name=$name;
$movie->content=$content;
$movie->director=$director;
$movie->save();
$this->redirect(array(getMovie));
}
HTML增加页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/movie/Movie/addMovie2" method="post">
<table style="margin-top: 10px;font-size: x-large" >
<tr>
<td>电影:</td>
<td><input type="text" name="name" style="width: 500px;height: 30px"></td>
</tr>
<tr>
<td>导演:</td>
<td><input type="text" name="director" style="width: 500px;height: 30px"></td>
</tr>
<tr>
<td>内容:</td>
<td><textarea name="content" cols="30" rows="10" style="width: 500px;"></textarea></td>
</tr>
<tr content="center">
<td colspan="2" style="text-align: center"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
删除
/**
* 删除电影
*/
public function actionGetDeleteMovie()
{
$id=$_GET['id'];
$movie=Movie::model()->findByPk($id);
$movie->delete(); // 从数据表中删除此行
$this->redirect(array(getMovie));
}
修改
/**
* 跳转到更新电影
*/
public function actionToUpdateMovie(){
$id=$_GET['id'];
$movie=Movie::model()->find('id=:id',array(':id'=>$id));
$this->smarty->assign('movie',$movie);
$this->smarty->display('movie/update.html');
}
/**
* 修改电影
*/
public function actionGetUpdateMovie(){
$id=$_POST['id'];
$movie=Movie::model()->find('id=:id',array(':id'=>$id));
$movie->name=$_POST['name'];
$movie->content=$_POST['content'];
$movie->director=$_POST['director'];
$movie->save(); // 将更改保存到数据库
$this->redirect(array(getMovie));
}
HTMl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/movie/Movie/getUpdateMovie" method="post">
<table border="1" style="margin-top: 100px;margin-left: 100px">
<tr >
<input type="text" name="id" value="<{$movie.id}>" hidden/>
</tr>
<tr>
<td>电影</td>
<td>
<textarea type="text" name="name" style="width: 1000px"><{$movie.name}></textarea>
</td>
</tr>
<tr>
<td>内容</td>
<td>
<textarea type="text" name="content" style="width: 1000px"><{$movie.content}></textarea>
</td>
</tr>
<tr>
<td>导演</td>
<td>
<textarea type="text" name="director" style="width: 1000px"><{$movie.director}></textarea>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
修改页面
模糊查询
需要写SQL语句
/**
* 模糊查询电影
*/
public function actionSearch(){
$keywords=$_POST['keywords'];
$sql = "select *
from t_movie
where t_movie.name like '%".$keywords."%'
or t_movie.director like '%".$keywords."%'
or t_movie.content like '%".$keywords."%'
";
$list = Yii::app()->db->createCommand($sql)->queryAll();
$this->smarty->assign('list',$list);
$this->smarty->display('movie/select.html');
}
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" style="margin-top: 10px;font-size: x-large">
<thead> <tr> <th>电影</th>
<th>导演</th>
<th>内容</th> </tr>
<tr>
</thead> <tbody> <{foreach from=$list item=movie}>
<td><{$movie.name}></td><td><{$movie.director}></td><td><{$movie.content}></td> <td>
</td> </tr> <{/foreach}> <tr> <td colspan="2" style="text-align: center" > </td> </tr> </tbody>
</table>
</body>
</html>
跳转
$this->redirect(array(getMovie)); //跳转到同一controller的方法
$this->redirect($this->createUrl('/movie/movie/GetMovie/')); //跳转到任意controller的方法。可传参
$this->redirect($this->createUrl('/movie/movie/GetMovie/Id/'.$Id));