为什么一些 PHP 框架,比如 thinkphp3 都有表结构缓存功能 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
894021573
V2EX    PHP

为什么一些 PHP 框架,比如 thinkphp3 都有表结构缓存功能

  •  
  •   894021573 2018 年 10 月 31 日 3748 次点击
    这是一个创建于 2732 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以 thinkphp3 为例,非 debug 模式。 假如把 a1,a2,a3 字段插入表 A。后来表 A 新增 a4 字段,然后把 a1,a2,a3,a4 字段插入表 A。 因为表 A 事先只缓存了 a1,a2,a3 三个字段,所以会自动过滤 a4。只能清除表缓存来解决。

    Yii2 也有类似的设计。Yii2 中,自动生成模型那里,会用到表字段信息,因为有字段验证规则,其他地方,貌似也没需要用到表字段的地方了。

    总结:因为表结构缓存踩了几次坑,对一些新增的字段,修改无效(不如直接数据库报错来的好)。而增删改查其实不需要知道表结构。所以请教下,这表结构缓存主要是用来做啥的?

    7 条回复    2018-11-21 14:46:33 +08:00
    chnyang
        1
    chnyang  
       2018 年 10 月 31 日
    show columns from table?
    doyouhaobaby
        2
    doyouhaobaby  
       2018 年 11 月 1 日   1
    这种设计有缺陷,很容易采坑的,TP3 在公司中用的时候自动过滤,比如字段单词拼写错误造成了很多隐晦的 bug,代码太依赖数据库了,我现在基本放弃这种写法了。把字段放到 model 层或者实体,用 getter setter 来做比较好,字段校验不依赖数据库。

    ```
    <?php

    declare(strict_types=1);

    /*
    * This file is part of the ************************ package.
    * _____________ _______________
    * ______/ \__ _____ ____ ______ / /_ _________
    * ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
    * __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
    * \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
    * \_\ /_/_/ /_/
    *
    * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
    * (c) 2010-2018 http://queryphp.com All rights reserved.
    *
    * For the full copyright and license information, please view the LICENSE
    * file that was distributed with this source code.
    */

    namespace Tests\Database\Ddd\Entity\Relation;

    use Leevel\Database\Ddd\Entity;

    /**
    * post.
    *
    * @author Xiangmin Liu <[email protected]>
    *
    * @since 2018.10.13
    *
    * @version 1.0
    */
    class Post extends Entity
    {
    const TABLE = 'post';

    const ID = 'id';

    const AUTO = 'id';

    const STRUCT = [
    'id' => [
    'readonly' => true,
    ],
    'title' => [],
    'user_id' => [],
    'summary' => [],
    'create_at' => [],
    'delete_at' => [],
    'user' => [
    self::BELONGS_TO => User::class,
    'source_key' => 'user_id',
    'target_key' => 'id',
    ],
    'comment' => [
    self::HAS_MANY => Comment::class,
    'source_key' => 'id',
    'target_key' => 'post_id',
    self::SCOPE => 'comment',
    ],
    'post_content' => [
    self::HAS_OnE=> PostContent::class,
    'source_key' => 'id',
    'target_key' => 'post_id',
    ],
    ];

    const DELETE_AT = 'delete_at';

    private $id;

    private $title;

    private $userId;

    private $summary;

    private $createAt;

    private $deleteAt;

    private $user;

    private $comment;

    private $postContent;

    public function setter(string $prop, $value)
    {
    $this->{$this->prop($prop)} = $value;

    return $this;
    }

    public function getter(string $prop)
    {
    return $this->{$this->prop($prop)};
    }

    public function scopeComment($select)
    {
    $select->where('id', '>', 4);
    }

    public function scopeTest($select)
    {
    $select->where('id', '>', 4);
    }

    public function scopeTest2($select)
    {
    $select->where('id', '<', 10);
    }

    public function scopeTest3($select)
    {
    $select->where('id', 5);
    }
    }
    ```

    https://github.com/hunzhiwange/framework/blob/master/tests/Database/Ddd/Entity/Relation/Post.php
    894021573
        3
    894021573  
    OP
       2018 年 11 月 2 日
    @doyouhaobaby 我们的观点比较一致。还想听听其他伙伴的看法。
    894021573
        4
    894021573  
    OP
       2018 年 11 月 2 日
    @chnyang 对啊,框架做了。
    hefish
        5
    hefish  
       2018 年 11 月 11 日
    我还是不适应 tp,小项目还是自己用 composer 搭个架子,自己写一个简单的 controller 路由。 操作数据库,感觉还是 eloquent 顺手一些。
    Joyboo
    &nbp;   6
    Joyboo  
       2018 年 11 月 19 日
    @hefish tp5 的模型类,很强大的
    xiaoxiaoan317
        7
    xiaoxiaoan317  
       2018 年 11 月 21 日
    推荐 tp5 吧,比 tp3 好多了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2983 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 06:50 PVG 14:50 LAX 23:50 JFK 02:50
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86