Yii2 - 如何在给定名称的情况下调用 ActiveRecord 方法
Yii2 - How to call ActiveRecord method given its name
我有一个 string
变量,其中包含 模型 的 class 名称,我想调用上述 [=24= 上的方法]model 使用那个变量,这可能吗??
我的代码:
foreach($tables as $tables)
{
$table = ArrayHelper::getValue($tables, 'table_name');
$model = \common\models$table::findAll();
var_dump($model);
}
更简单的版本:
$table = "DataAnalysis";
$model = \common\models$table::findAll();
var_dump($model);
当我 运行 该代码时,出现以下错误:
Exception 'ParseError' with message 'syntax error, unexpected '$table' (T_VARIABLE), expecting identifier (T_STRING)'
给定变量中包含的字符串,我可以做些什么来调用 model?
您可以使用 call_user_func()
。
// If you have the name of the ActiveRecord class, like in you comment
$class = 'DataAnalysis';
/** @var ActiveQuery $query */
$query = call_user_func($class . '::find');
// Then use the query as you want
foreach($query->each() as $model) {
// Do something with your model
}
如果您不确定变量中的值是否始终正确,请将其包装在 try/catch
.
上
如果您拥有的是 table 名称,而不是 class 名称,您可以尝试使用 Yii inflector camelize method.
进行转换
use yii\helpers\Inflector;
$tableName = 'some_snake_case_name';
$className = Inflector::camelize($tableName);
/** @var ActiveQuery $query */
$query = call_user_func($className . '::find');
// If you want to filter results
$query->where(...);
// Then use the query as you want
foreach($query->each() as $model) {
// Do something with your model
}
call_user_function
docs are here.
你应该简单地做,
$model="\common\models\DataAnalysis";
$model::findAll();
或
$table="DataAnalysis";
$model="\common\models\{$table}";
$model::findAll();
而不是 call_user_func()
对于一个简单的任务来说,代码太多了
编辑
如果您需要实例化 class 而不是静态调用,您可以简单地执行
$table="DataAnalysis";
$model="\common\models\{$table}";
new $model();
我有一个 string
变量,其中包含 模型 的 class 名称,我想调用上述 [=24= 上的方法]model 使用那个变量,这可能吗??
我的代码:
foreach($tables as $tables)
{
$table = ArrayHelper::getValue($tables, 'table_name');
$model = \common\models$table::findAll();
var_dump($model);
}
更简单的版本:
$table = "DataAnalysis";
$model = \common\models$table::findAll();
var_dump($model);
当我 运行 该代码时,出现以下错误:
Exception 'ParseError' with message 'syntax error, unexpected '$table' (T_VARIABLE), expecting identifier (T_STRING)'
给定变量中包含的字符串,我可以做些什么来调用 model?
您可以使用 call_user_func()
。
// If you have the name of the ActiveRecord class, like in you comment
$class = 'DataAnalysis';
/** @var ActiveQuery $query */
$query = call_user_func($class . '::find');
// Then use the query as you want
foreach($query->each() as $model) {
// Do something with your model
}
如果您不确定变量中的值是否始终正确,请将其包装在 try/catch
.
如果您拥有的是 table 名称,而不是 class 名称,您可以尝试使用 Yii inflector camelize method.
进行转换use yii\helpers\Inflector;
$tableName = 'some_snake_case_name';
$className = Inflector::camelize($tableName);
/** @var ActiveQuery $query */
$query = call_user_func($className . '::find');
// If you want to filter results
$query->where(...);
// Then use the query as you want
foreach($query->each() as $model) {
// Do something with your model
}
call_user_function
docs are here.
你应该简单地做,
$model="\common\models\DataAnalysis";
$model::findAll();
或
$table="DataAnalysis";
$model="\common\models\{$table}";
$model::findAll();
而不是 call_user_func()
对于一个简单的任务来说,代码太多了
编辑
如果您需要实例化 class 而不是静态调用,您可以简单地执行
$table="DataAnalysis";
$model="\common\models\{$table}";
new $model();