YII-基础学习

<?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);
    }

}
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!