0

我有这个代码

 $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]);
4

2 回答 2

0

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

您可以将局部变量包裹在花括号中以评估变量。

$this->{$model}->id = $id;
于 2013-07-24T00:30:12.460 回答
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();
}

?>
于 2013-07-24T05:06:07.620 回答