Yii框架-增刪改查

一、新建數據庫(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));

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章