php - 为何laravel5的orm中setTable没有效果

查看:326
本文介绍了php - 为何laravel5的orm中setTable没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

use Illuminate\Database\Eloquent\Model as Eloquent;
class MessageModel extends Eloquent{
    public $table = 'message';
    
    public function getModel($tableName){
    
       return $this->setTable($tableName);
    }
}

报错:

[2016-12-18 18:27:33] production.ERROR: PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'message' doesn't exist in vendor/laravel/framework/src/Illuminate/Database/Connection.php:457

因为消息表示分表的所以想通过一个model操作所有的分表的表

解决方案

今天发现这个是自己使用的问题因为我使用了create静态方法我们可以看到基类的定义

/**
     * Save a new model and return the instance.
     *
     * @param  array  $attributes
     * @return static
     */
    public static function create(array $attributes = [])
    {
        $model = new static($attributes);

        $model->save();

        return $model;
    }
    

所以由此看出凡是改过表名的model都不能再使用静态方法。只能通过其他方法实现

这篇关于php - 为何laravel5的orm中setTable没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆