雄辩的ORM动态表,通过模型创建无法选择表

时间:2018-07-24 06:21:38

标签: php eloquent

在基于slim3构建的REST API上,我使用路由参数来动态选择租户以对其执行某些操作。

模型:

class Customer extends Model {
    protected $table = '';
    protected $fillable = ['some', 'fields'];

    public function setTable($table) {
        $this->table = $table;
    }
    public function getTable() {
        return $this->table;
    }
}

选择和更新条目的方式(正确)是这样的:

// selects properly from $tableName table
$kd = new Customer;
$kd->setTable($tableName);
$data = $kd->whereEmail($args['email'])->firstOrFail();

使用相同的方法创建失败:

$kdc = new Customer;
$kdc->setTable($tableName); // it should use dbname.prefix_$tableName same as above
$kdc->create($customerData);
// fails with error Base table or view not found: 1146 Table 'dbname.prefix_' doesn't exist 

我已经检查过create()从模型成功调用了getTable()并获得了适当的值。但是似乎不使用它...

0 个答案:

没有答案