Laravel 4:使用setTable()的动态表名 [英] Laravel 4: Dynamic table names using setTable()

查看:1830
本文介绍了Laravel 4:使用setTable()的动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我需要做什么才能使动态表名工作。



考虑以下模型(表测试不存在):

 <?php 

// app / models / Test.php

class Test扩展Eloquent {

}

然后如果我做('fields'表确实存在):

 <?php 

// app / routes.php

$ test = new \Test;
$ test-> setTable('fields');
$ data = $ test-> find(1);
dd($ data);

我收到错误:

  SQLSTATE [42S02]:未找到基表或视图:1146表'test.tests'不存在(SQL:select * from`tests`其中`id` =?limit 1)绑定:数组(0 => 1,))

请注意,测试模型工作正常。



L4实际上使用了setTable()方法,非常的方式,我想要在供应商/ laravel / framework / src / Illuminate / Database /Eloquent/Relations.Pivot.php构造函数,虽然我根本无法让它按照这个例子工作。



感谢您的帮助。

解决方案

也许这对你有用: http://laravel.io/forum/08-01-2014-defining-models-in-runtime



更确切地说,从这里: http://laravel.io/forum/08-01-2014-defining-models-in-runtime?page=1#reply-11779

  class ModelBuilder extends Eloquent {

protected static $ _table;


public static function fromTable($ table,$ parms = Array()){
$ ret = null;
if(class_exists($ table)){
$ ret = new $ table($ parms);
} else {
$ ret = new static($ parms);
$ ret-> setTable($ table);
}
return $ ret;
}

public function setTable($ table)
{
static :: $ _ table = $ table;
}

public function getTable()
{
return static :: $ _ table;
}
}

$ user = ModelBuilder :: fromTable(users) - > find(1);

这不是我的最终实现,这比我的用例复杂得多(而且更脏) 。但我想这个例子可能会导致你所需要的。


I'm not sure what do I need to do to get dynamic table names working.

Consider following Model (table 'test' does not exist):

<?php

// app/models/Test.php

class Test extends Eloquent {

}

and then if I do ('fields' table does exist):

<?php

// app/routes.php

$test = new \Test;
$test->setTable('fields');
$data = $test->find(1);
dd($data);

I get an error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.tests' doesn't exist (SQL: select * from `tests` where `id` = ? limit 1) (Bindings: array ( 0 => 1, ))"

Note that setting the table name from the Test model works just fine.

L4 actually uses setTable() method, very much the way I would want to, in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations.Pivot.php constructor, though I just couldn't get it to work by following that example.

Thanks for help.

解决方案

Perhaps this is useful for you: http://laravel.io/forum/08-01-2014-defining-models-in-runtime

More exactly, from here: http://laravel.io/forum/08-01-2014-defining-models-in-runtime?page=1#reply-11779

class ModelBuilder extends Eloquent {

    protected static $_table;


    public static function fromTable($table, $parms = Array()){
        $ret = null;
        if (class_exists($table)){
            $ret = new $table($parms);
        } else {
            $ret = new static($parms);
            $ret->setTable($table);
        }
        return $ret;
    }

    public function setTable($table)
    {
        static::$_table = $table;
    }

    public function getTable()
    {
        return static::$_table;
    }
}

$user = ModelBuilder::fromTable("users")->find(1);

That's not my final implementation, which's much more complex (and dirtier) than that because of my use cases. But i guess the example may lead you to what you need.

这篇关于Laravel 4:使用setTable()的动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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