1

似乎这不应该那么难,但让我很适应。

在模型的 __construct 方法中初始化变量。

需要在 view.html.php 和 default.php 文件中访问它们。

在我的模型中:

$this->MyVar = 'somevalue';

在我的 view.html.php 中:

$model = $this->getModel('mymodelname');
print_r($model) //checking, yes - the model's being pulled in
$myvar = $model->__construct($this->MyVar);
echo $myvar; //empty

我做错了什么,我该如何解决?

谢谢!

==========================================

解决方案:

$model = $this->getModel('mymodelname');
echo $model->MyVar; // returns the variable in the model
4

2 回答 2

2

__construct() does not return any value, this is why $myvar remains null. If you want, you can read more about it here

According to the specification (in the link above) you should pass to __construct an associative array that could hold one or more of the following fields:

  • 'name'
  • 'state'
  • 'dbo'
  • 'table_path'

and according to what you say - you pass a parameter. Try:

$arr = array('name' => $this->MyVar);
$model->__construct($arr);
于 2012-08-30T19:31:48.890 回答
1

为什么要在实例化模型后使用构造,只需这样做:

$model = $this->getModel('mymodelname');
$model->MyVar = $myvar;
于 2012-08-31T08:16:01.887 回答