PHP Yii框架 1.1框架增删改查

在工作室项目基础上练习yii增删改查

1建立数据库

2连接数据库

 

找到main.php修改数据库名 路径如上

3定义AR类(model)

在如上目录新建php文件,继承CActiveRecord以访问一个数据表,每个AR类代表一个单独的数据表,AR实例代表表中的一行。

写两个方法,tablename中的return应该为自己数据库中表的名称。

class Book extends CActiveRecord
{
    public static function  model($className=__CLASS__)
    {
        return parent::model($className);
    }
    public function tableName(){
        return '{{bookshare}}';
    }
}

注:默认情况下,AR 类的名字和数据表的名字相同。如果不同,请覆盖 tableName() 方法。

引入的 表前缀功能 AR类的 tableName() 方法可以通过如下方式覆盖


public function tableName(){
return '{{news}}'; //return 't_news'; 表的名字
}

参考https://blog.csdn.net/chenfang0529/article/details/80322221

4编写controller

class BookController extends Controller
{
    public function actionGetBooks(){
        $list = Book::model()->findAll();
        $this->smarty->assign('list',$list);
        $this->smarty->display('book/list_book.html');
    }

    public function actionGetBookById(){
        $id = $_GET['id'];
        $book = Book::model()->find('id=:id',array(':id'=>$id));
        $this->smarty->assign('book',$book);
        $this->smarty->display('book/ser_result.html');
    }

    public function actionAddBook(){
        $book = new Book();
        $bookname = $_POST['bookname'];
        $author = $_POST['author'];
        $introduction = $_POST['introduction'];
        $country = $_POST['country'];
        $publisher = $_POST['publisher'];
        $book->bookname=$bookname;
        $book->author=$author;
        $book->introduction=$introduction;
        $book->publisher=$publisher;
        $book->country=$country;
        $book->save();
        $this->redirect(array(GetBooks));
    }

    public function actionDelBookById(){
        $id = $_GET['id'];
        $book = Book::model()->findByPk($id);
        $book->delete();
        $this->redirect(array(GetBooks));
    }

    public  function actionToAdd(){
        $this->smarty->display('book/add.html');
    }

    public function actionToUpdate(){
        $id=$_GET['id'];
        $book=Book::model()->find('id=:id',array(':id'=>$id));
        $this->smarty->assign('book',$book);
        $this->smarty->display('book/update.html');
    }

    public function actionUpdate(){
        $id = $_POST['id'];
        $book=Book::model()->find('id=:id',array(':id'=>$id));
        $book->bookname=$_POST['bookname'];
        $book->author=$_POST['author'];
        $book->introduction=$_POST['introduction'];
        $book->publisher=$_POST['publisher'];
        $book->country=$_POST['country'];
        $book->save();
        $this->redirect(array(GetBooks));
    }

    public function actionSer(){
        $par = $_POST['ser'];
        $criteria = new CDbCriteria;
        $sql = "select *  
                 from t_bookshare
                 where bookname like '%" . $par . "%'
            ";
        $selectList = Yii::app()->db->createCommand($sql)->queryAll();
        $this->smarty->assign('selectList',$selectList);
        $this->smarty->display('book/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> <th>时间</th>
    </tr>

    </thead>
    <tr>
    <tbody>
    <{foreach from=$list item=bookshare}>
    <td><{$bookshare.id}></td>
    <td><{$bookshare.bookname}></td> <td><{$bookshare.author}></td>
    <td>
        <a href="/dsjyw0725/dsjyw0725/books/Book/DelBookById?id=<{$bookshare.id}>">删除</a>
        <a href="/dsjyw0725/dsjyw0725/books/Book/GetBookById?id=<{$bookshare.id}>">查看详情</a>
        <a href="/dsjyw0725/dsjyw0725/books/Book/ToUpdate?id=<{$bookshare.id}>">修改</a>
        <a href="/dsjyw0725/dsjyw0725/books/Book/toAdd">添加</a>
    </td> </tr> <{/foreach}> <tr> <td colspan="4" style="text-align: center" > </td> </tr>
    </tbody>
</table>
<form action="/dsjyw0725/dsjyw0725/books/Book/Ser" method="post">
    <input name="ser" type="text">
    <input type="submit" value="模糊搜索">
</form>
</body>
</html>

运行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值