• ThinkPHP5杂技(一)


    Thinkphp5 assign 传递 " 时 ,前台收到的是 " 和ThinkPHP3.2不一样,3.2收到的是 ”,传递给js时

    用的data.replace(new RegExp('"','g'),'"')替换回来
    貌似不好,获取值的时候可以用 {$data.abc|raw} 解决传入参数"替换&quot问题
    也就是如果前台传入的是json数据 后台用 { |raw}解决

    Thinkphp5.1
    在部分Base.php 控制器中 hinkRequest::instance()->controller()根本调用不到
    需要 hinkfacadeRequest::instance()->controller() 可以,

    前台url链接指向其他控制器方法时可用
    {foreach name="vo" item="val"}
        <li id="{$val.title}">
        <a href=" {:url($val.model.'/'.$val.action)}">{$val.title}</a>
        </li>
    {/foreach}
    

      

    当前台输出输出数据时

    输出的效果是

    网页源代码可以看到因果

    想要的效果是

    解决方式

    浏览器开发者工具中

    网页源代码中

    总结:模板动态输出含标签的html代码,或者双引号时,默认输出结果会经过htmlentities()转义,结果不是我们想要的,解决办法时{XX|raw}

      

    模板继承的时候第二种模板标签方式 replace="[XXX]" 不要用replace="{XXX}" 否则会意外多出 “/>

    Thinkphp5.1用Model类时,return数据
    在控制器dump返回的数据时显示是一个数组————坑
    array_merge()总是提示不是数组
    用var_dump()打印Model返回值时发现,那是一个对象
    想要Model返回array对象增加一个->toArray()

    Coin::where('userid',$data)->find()->toArray();

    get与param区别

    Thinkphp5.1遇到的坑

    某两个相似的时间戳前台用,已提交issure,已改进 githubIssure  githubCode

    输出

    一个输出的是

    有的输出就不正确,打印出变量查看时间戳是正确的

    产看缓存中的输出

    再追究

    $time = 1508982277; //2017/10/26 9:44:37
    var_dump(strtotime($time));
    echo '<br>';
    $time = 1508489607; //2017/10/20 16:53:27
    var_dump(strtotime($time));

    这结果!!

    3.2是这么干的

    模型关联删除问题

    A hanMany B

        public function test()
        {
            $user = A::get(1);
            echo '<pre>';
            $temp = $user->products;
    
    var_dump($temp);  //得到的是thinkmodelCollection对象
    var_dump($user->products()->select()); //得到的是thinkmodelCollection对象,$user->products()得到的是hasMany对象
    var_dump($user->products()->where('id', 28)->find()); //得到B对象

    var_dump($user->products()->where('id', 28)->delete()); //数据库中数据已删除
    var_dump($user->products()->select());  //id = 28 的数据已删除
    var_dump($temp); //id = 28 的数据依旧存在

    }
    object(thinkmodelCollection)#99 (1) {
      ["items":protected]=>
      array(3) {
        [0]=>
        object(appusermodelShopCar)#102 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(27)
            ["产品id"]=>
            int(2)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757081)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [1]=>
        object(appusermodelShopCar)#101 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(28)
            ["产品id"]=>
            int(5)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757086)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [2]=>
        object(appusermodelShopCar)#100 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(29)
            ["产品id"]=>
            int(6)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757861)
          }
          ["relation"]=>
          array(0) {
          }
        }
      }
    }
    object(thinkmodelCollection)#95 (1) {
      ["items":protected]=>
      array(3) {
        [0]=>
        object(appusermodelShopCar)#16 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(27)
            ["产品id"]=>
            int(2)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757081)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [1]=>
        object(appusermodelShopCar)#96 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(28)
            ["产品id"]=>
            int(5)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757086)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [2]=>
        object(appusermodelShopCar)#97 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(29)
            ["产品id"]=>
            int(6)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757861)
          }
          ["relation"]=>
          array(0) {
          }
        }
      }
    }
    object(appusermodelShopCar)#16 (2) {
      ["data"]=>
      array(5) {
        ["id"]=>
        int(28)
        ["产品id"]=>
        int(5)
        ["数量"]=>
        int(1)
        ["编号"]=>
        string(3) "gao"
        ["操作时间"]=>
        int(1509757086)
      }
      ["relation"]=>
      array(0) {
      }
    }
    int(1)
    object(thinkmodelCollection)#87 (1) {
      ["items":protected]=>
      array(2) {
        [0]=>
        object(appusermodelShopCar)#90 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(27)
            ["产品id"]=>
            int(2)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757081)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [1]=>
        object(appusermodelShopCar)#89 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(29)
            ["产品id"]=>
            int(6)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757861)
          }
          ["relation"]=>
          array(0) {
          }
        }
      }
    }
    object(thinkmodelCollection)#99 (1) {
      ["items":protected]=>
      array(3) {
        [0]=>
        object(appusermodelShopCar)#102 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(27)
            ["产品id"]=>
            int(2)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757081)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [1]=>
        object(appusermodelShopCar)#101 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(28)
            ["产品id"]=>
            int(5)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757086)
          }
          ["relation"]=>
          array(0) {
          }
        }
        [2]=>
        object(appusermodelShopCar)#100 (2) {
          ["data"]=>
          array(5) {
            ["id"]=>
            int(29)
            ["产品id"]=>
            int(6)
            ["数量"]=>
            int(1)
            ["编号"]=>
            string(3) "gao"
            ["操作时间"]=>
            int(1509757861)
          }
          ["relation"]=>
          array(0) {
          }
        }
      }
    }
    View Code

    模型可以直接当成普通方式查询数据,当join()后返回的不再是模型对象,而是array数据

    ShopCar::where([['a.编号', '=', $userName], ['b.状态', '=', '使用']])->alias('a')
                ->join(['商城产品' => 'b'], 'a.产品id = b.id', 'LEFT')->column('a.产品id,a.数量,b.价格,b.pv,b.可订购数量,b.名称,b.重量,(b.可订购数量-a.数量) as numLeft', 'a.id');

    行锁lock(true) 或者lock(false)什么也不做 

    ThinkPHP5.1

    thinkphplibrary hink outedispatchModule.php P108 用到 is_callable()方法

    复制代码
            if (is_callable([$instance, $action])) {
                // 执行操作方法
                $call = [$instance, $action];
                // 自动获取请求变量
                $vars = $this->app->config('app.url_param_type')
                ? $this->app['request']->route()
                : $this->app['request']->param();
            } elseif (is_callable([$instance, '_empty'])) {
                // 空操作
                $call = [$instance, '_empty'];
                $vars = [$actionName];
            } else {
                // 操作不存在
                throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
            }
    
            $this->app['hook']->listen('action_begin', $call);
    
            return Container::getInstance()->invokeMethod($call, $vars);
        }
    复制代码

    但是当 $instance中有 __call 方法时,is_callable([$instance, $action]) 返回的总为true,需要注意,貌似Thinkphp5中反射导致的__call方法无效(个人临时理解)

    复制代码
    colin 03-Oct-2010 08:30  //代码块来自PHP手册
    
     I haven't seen anyone note this before, but is_callable will correctly determine the existence of methods made with __call. The method_exists function will not.
    
     Example:
    <?php
    
    class Test {
    
         public function testing($not = false) {
             $not = $not ? 'true' : 'false';
             echo "testing - not: $not<br/>";
         }
         
         public function __call($name, $args) {
             if(preg_match('/^not([A-Z]w+)$/', $name, $matches)) {
                 $fn_name = strtolower($matches[1]);
                 if(method_exists($this, $fn_name)) {
                     $args[] = true; // add NOT boolean to args
                     return call_user_func_array(array($this, $matches[1]), $args);
                 }
             }
             die("No method with name: $name<br/>");
         }
    
     }
    
    $t = new Test();
    $t->testing();
    $t->notTesting();
    
     echo "exists: ".method_exists($t, 'notTesting').'<br/>';
     echo "callable: ".is_callable(array($t, 'notTesting'));
    
    ?>
    
     Output:
    
     testing - not: false
     testing - not: true
     exists:
     callable: 1 
    复制代码

     Thinphp5手册上说让用中文表名,中文字段,没有详细说明

    个人可以想到的地方,

    一是 pdo操作占位符的时候没特殊处理,

    二是 中文表名得在model主动声明我这个model对应的是哪个表,英文表明一般可省略

    三是Model返回的全是对象,字段实际上是对象的属性 ->id来调用,如果把id换成中文名称,怪怪的,还没见过谁的代码真用中文做变量。虽然也可以用 $obj['id’]查看属性的值


    支付宝验证规则 邮箱/手机号/淘宝会员名
    //        邮箱/手机号/淘宝会员名
            'aliPay|c' => 'require|length:3,25',
            'aliPay|d' => ['regex'=>'/^w+((@w{1,4}.w{1,4})|(w*))$/'],

    觉得还比较理想

    20171110

    开发的项目中有这么个需求,对数据库中获取的数据进行加工。3.2的时候用的在控制器中获取数据库,然后用控制器中的方法修改数据。现在5.1尝试用模型的获取器。但是发现

    中的$data是一个数组,也就是不能用模型关联中的数据。那可不行。

    尝试后用$this对象访问便可

    数据库/模型 返回类型

    (new AModel)->saveAll($list) 返回的是插入的条数,ep:int(1)

  • 相关阅读:
    深入了解css的行高Line Height属性
    【C++】函数指针
    【C++】常用知识点
    将数字转化为液晶显示屏的样子
    【多媒体】PCM
    【Android】网络下载图片&SD卡文件存储
    CPU 缓存(Cache)
    【C++】typename
    【多媒体】音频格式
    【Android】图片的异步加载
  • 原文地址:https://www.cnblogs.com/8000cabbage/p/7457193.html
Copyright © 2020-2023  润新知