zoukankan      html  css  js  c++  java
  • php第五章

    yii框架

    指定控制器

    ?r=hello/index表示:index.php 跳转到HelloController控制器中actionIndex函数

     例子:

    http://localhost:63342/htdocs/yii_basic/web/index.php?r=hello/query

    HelloController.php

    <?php
    /**
     * Created by PhpStorm.
     * User: forr
     * Date: 15/10/2
     * Time: 12:02
     */
    namespace appcontrollers;
    
    use yiiwebController;
    use appmodelsFirstTest;
    
    class HelloController extends Controller
    {
        public function  actionIndex()
        {
            $request = YII::$app->request;
            echo $request->get('id');
    //        echo 'hello world 23333333';
            //跳转
    //        $this->redirect('https://www.baidu.com/');
            //下载文件
    //        $response = YII::$app->response;
    //        $response->sendFile('./favicon.ico');//路径web/favicon.ico
        }
        //查询数据
        public function  actionQuery()
        {
            echo 'my db';
            $sql = 'select * from first_test where id = :id';
            $list = FirstTest::findBySql($sql,array(':id'=>1))->all();
            print_r($list).'<br/>';
            //批量查询每次查询2条记录
            $sql = 'select * from first_test';
            foreach(FirstTest::find()->batch(2) as $test)
            {
                echo count($test);
            }
        }
        //删除数据
        public function actionDelete()
        {
    //        $sql = 'select * from first_test where id = :id';
    //        $list = FirstTest::findBySql($sql,array(':id'=>4))->all();
    //        $list[0]->delete();
            //或
             FirstTest::deleteAll('id = :id',array(':id'=>3));
        }
        //添加数据
        public function actionAdd()
        {
             $test1 = new FirstTest();
             $test1->name = '张杰';
             $test1->title = '逆战';
             $test1->age = 32;
            //启动校验规则
            $test1->validate();
            if($test1->hasErrors())
            {
                echo '名称过长,不符合规则';
                die;
            }
            $test1->save();
        }
    
    }

    FirstTest.php

    <?php
    /**
     * Created by PhpStorm.
     * User: forr
     * Date: 15/10/2
     * Time: 16:40
     */
    namespace appmodels;
    
    use yiidbActiveRecord;
    
    class FirstTest extends ActiveRecord{
    
        public function rules()
        {
            return [
                // 有个有效的名字
                ['name', 'string','length'=>[0,10]],
            ];
        }
    }

     Orders.php

    <?php
    /**
     * Created by PhpStorm.
     * User: forr
     * Date: 15/10/2
     * Time: 16:40
     */
    namespace appmodels;
    
    use yiidbActiveRecord;
    
    class Orders extends ActiveRecord{
        public function getCustomer()
        {
            $customer = $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray();
            return $customer;
        }
    }

    Customer.php

    <?php
    /**
     * Created by PhpStorm.
     * User: forr
     * Date: 15/10/2
     * Time: 16:40
     */
    namespace appmodels;
    
    use yiidbActiveRecord;
    
    class Customer extends ActiveRecord{
        public function getOrders()
        {
            $order =  $this->hasMany(Orders::className(),['customer_id'=>'id'])->asArray();
            return $order;
        }
    
    }
  • 相关阅读:
    【转】Python爬虫(5)_性能相关
    【转】Python爬虫(4)_selenium模块
    【转】Python爬虫(3)_Beautifulsoup模块
    【转】Python爬虫_示例2
    【转】Python爬虫_示例
    linux开机自启动tomcat
    Linux下Tomcat启动,停止命令
    Linux下tomcat启动Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of
    centos7开启关闭80端口
    centos安装JDK
  • 原文地址:https://www.cnblogs.com/huen/p/4852160.html
Copyright © 2011-2022 走看看