YII-基礎學習

zhangsen發表於2019-06-27
<?php
/**
 * Created by PhpStorm.
 * User: care
 * Date: 2018/3/16
 * Time: 下午6:46
 */

namespace app\controllers;

use app\models\Customer;
use app\models\Orders;
use app\models\Test;
use Yii;
use yii\web\Controller;
use yii\web\Cookie;

class HelloController extends Controller
{
    public $layout = 'common';
    public function actionIndex()
    {
        //獲取請求
//        $request = Yii::$app->request;
//        echo $request->get('id',20);
//        echo 'hello';

        //請求方式
//        if($request->isGet){
//            echo '1';
//        }
        //客戶的請求方式
//        echo $request->userHost;
//        echo '</br>';
//        echo $request->userIP;

        $res = Yii::$app->response;
        //返回狀態碼
//        $res -> statusCode = 404;
        //增加一個pragma頭
//        $res->headers->add('Pragma','no-cache');
        //設定一個pragma頭
//        $res->headers->set('Pragma','max-age=5');
        //刪除pragma頭
//        $res->headers->remove('Pragma');
        //跳轉
//        $res->headers->add('location','http://www.baidu.com');
        //跳轉
//        $res->redirect('http://www.baidu.com',302);

        //檔案下載
//        $res->headers->add('content-disposition','attachment;filename="a.jpg" ');
//          $res->sendFile('./robots.txt');

        //session
        $session = Yii::$app->session;
        //檢查是否開啟
//        if($session->isActive){
//            echo 'session';
//        }

        //設定一個session
//        $session->set('user','zhangsan');
        //獲取一個session
//        echo $session->get('user');
        //刪除session
//        $session->remove('user');

//        $session['user']  ='張三';
//        echo $session['user'];
//        unset($session['user']);

        //cookie
//        $cookies = Yii::$app->response->cookies;
//        $cookie_data = array('name'=>'user','value'=>'zhangsan');
//        $cookies->add(new Cookie($cookie_data));
//        $cookies->remove('user');

//        $cookies = Yii::$app->request->cookies;
//        echo $cookies->getValue('user');
//        echo $cookies->getValue('users',20);

//        $hello_str = 'hello world!<script>alert3;</script>';
//        $test_str = array(1,2);
//        $data = [];
//
//        $data['view_hello_str'] = $hello_str;
//        $data['view_test_str'] = $test_str;

//        return $this->render('index',$data);
//        return $this->render('about');

        //獲取快取元件
        $cache = Yii::$app->cache;
        //往快取中寫資料
//        $cache ->add('key1','hello world');
//        $cache ->add('key1','hello world2');

        //修改快取  可以設定
//        $cache->set('key1','hello world');
//        $cache->set('key1','hello world2');

        //讀取快取
//        $data = $cache ->get('key1');
        //刪除資料
//        $data = $cache->delete('key1');

        //清空資料
//        $cache->flush();

//        $data = $cache->get('key1');

        //快取有效期
//        $cache->add('key','hello world',5);
//        $cache->set('key','hello world',5);
//       echo $cache->get('key');
//        var_dump($data);

        //檔案依賴  修改檔案快取失效
//        $dependency = new \yii\caching\FileDependency(['fileName'=>'hw.txt']);
//        $cache->add('file_key2','hello world',3000, $dependency);

        //表示式依賴   view-source:http://www.blog.com/index.php?r=hello/index&name=zhangsan  //修改引數失效
//        $dependency = new \yii\caching\ExpressionDependency(['expression'=>'Yii::$app->request->get("name")']);
//        $cache->add('expression_key','hello world',3000,$dependency);
//        var_dump($cache->get('expression_key'));

        //DB依賴
//        $dependency = new \yii\caching\DbDependency(['sql'=>'SELECT count(*) from yii.orders']);
//        $cache->add('db_key','hello world',3000,$dependency);
//        var_dump($cache->get('db_key'));

        return  $this->renderPartial('index');
    }

    public function actionIndexss()
    {
        //查詢資料
//        $sql = " select * from test where id =1";
        $model = new Test;
//        $res = $model::findBySql($sql)->all();

            //id = 1
//        $res = $model::find()->where(['id'=>1])->all();

            //id > 0
//        $res = $model::find()->where(['>','id',0])->all();

            //id >= 1 且 id <= 2
//        $res = $model::find()->where(['between','id',1,2])->all();

            //title like '%title1%'
//        $res = $model::find()->where(['like', 'title', 'title'])->all();

        //查詢結果轉為陣列  將陣列轉為物件  記憶體佔有量比較高  需要轉為陣列 asArray()
        //$res = $model::find()->where(['between','id',1,2])->asArray()->all();

        //批量查詢  //每次拿多少 batch
        /*oreach ($model::find()->batch(1) as $tests){
            print_r(count($tests));
        }*/

        //刪除資料
//        $res = $model::find()->where(['id'=>1])->all();
//        $res[0] ->delete();

//        $model::deleteAll('id>:id',array(':id'=>0));

        //增加資料
//        $model->id = 4;
//        $model->title = 'title3';
//        $model->validate();
//        if($model->hasErrors()){
//            echo 'data is error';
//            die;
//        }
//        $model->save();

        //修改資料
//        $res = $model::find()->where(['id'=>4])->one();
//        $res->title = 'title4';
//        $res->save();

        //
//        print_r($res);
    }

    public function actionIndexs()
    {
        //根據顧客查出他的訂單
        $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); //以物件形式返回
//        $orders = $customer->hasMany('app\models\Orders',['customer_id'=>'id'])->asArray() ->all();
//        $orders = $customer->hasMany(Orders::className(),['customer_id'=>'id'])->asArray() ->all();
//        $orders = $customer ->getOrders();
//        $orders = $customer ->orders;

        //根據訂單查詢顧客的資訊
        $order = Orders::find()->where(['id'=>1])->one();
        $customer = $order -> customer;

        //關聯查詢結果換成
        $customer = Customer::find()->where(['name'=>'zhangsan'])->one();
        $orderes = $customer ->orders;
        unset($customer ->orders);

        //關聯查詢的多次查詢  首先select * from customer  然後每次遍歷再查一次
        $customers = Customer::find()->all();
        foreach ($customers as $customer){
            $orderes = $customer ->orders;
        }

        //優化 首先 select * from customer  然後select * from order where customer_id in ();
        $customers = Customer::find()->with('orders')->all();
        foreach ($customers as $customer){
            $orderes = $customer ->orders;
        }
        print_r($customer);
    }

}