我可以在变量设置中使用动态模型吗?

时间:2013-07-23 21:42:04

标签: php cakephp cakephp-2.0 cakephp-appmodel

我有这段代码

 $this->loadModel($model);
 $this->Task->id = $id;
 $this->Task->save($this->data[$model]);

如何设置“任务”模型,以便我可以将其设置为动态,因为这不起作用:

 $this->loadModel($model);
 $this->$model->id = $id;
 $this->$model->save($this->data[$model]);

我也试过这个没有运气:

 $this->loadModel($model);
 $this->currentModel = $model;
 $this->currentModel->id = $id;
 $this->currentModel->save($this->data[$model]);

2 个答案:

答案 0 :(得分:0)

查看http://grahamwideman.wordpress.com/2009/08/05/php-grammar-notes/

您可以用花括号包装局部变量来评估变量。

$this->{$model}->id = $id;

答案 1 :(得分:0)

尝试这个...

<?php

// THE DYNAMIC MODEL
App::Import('Model', $model);
$this->DynamicModel = new $model;

// USAGE EXAMPLE

function getModelColumns($model){
App::Import('Model', $model);
$this->DynamicModel = new $model;
print_r($this->DynamicModel->getColumnTypes());exit();
}

?>