从控制器访问表中的成员函数
Accessing member function in YTable from XController
这是一个关于 Cake 的一般性问题PHP 3,我有丰富的 OOP 背景知识,但我是 PHP 的新手,并且在项目中一直使用 Cake。我想这与约定有关。
假设我有一些模型实体 Apple,具有匹配的 ApplesTable class。在 ApplesTable class 中,我实现了一种从数据库中查找内容的方法。如果我在 ApplesController 中,我的理解是我可以写:$this->Apples->method()
就可以了。
但是,如果我想访问 OrangesController 中的那个方法,只需输入相同的内容就会给我一个致命的错误提示 "Call to a member function method() on boolean." 根据我的研究,它可能不是能够加载模型元素,因此上面编写的方法调用只会产生 false,从而产生错误。
同样,我是 PHP 的新手,也是 Cake 的新手,所以框架的一些约定仍然有点模糊。希望有人能帮助解决这个问题 -- 谢谢!
尝试 loadModel()
,当您需要使用非控制器默认模型 table/collection 时。
// ApplesController
loadModel("Oranges");
$this->Oranges->makeJuice();
$orange_sugar = $this->Oranges->sugar;
或者如果您的模型是 associated、
// ApplesController
$this->Apples->Oranges->makeJuice();
$orange_sugar = $this->Apples->Oranges->sugar;
这是一个关于 Cake 的一般性问题PHP 3,我有丰富的 OOP 背景知识,但我是 PHP 的新手,并且在项目中一直使用 Cake。我想这与约定有关。
假设我有一些模型实体 Apple,具有匹配的 ApplesTable class。在 ApplesTable class 中,我实现了一种从数据库中查找内容的方法。如果我在 ApplesController 中,我的理解是我可以写:$this->Apples->method()
就可以了。
但是,如果我想访问 OrangesController 中的那个方法,只需输入相同的内容就会给我一个致命的错误提示 "Call to a member function method() on boolean." 根据我的研究,它可能不是能够加载模型元素,因此上面编写的方法调用只会产生 false,从而产生错误。
同样,我是 PHP 的新手,也是 Cake 的新手,所以框架的一些约定仍然有点模糊。希望有人能帮助解决这个问题 -- 谢谢!
尝试 loadModel()
,当您需要使用非控制器默认模型 table/collection 时。
// ApplesController
loadModel("Oranges");
$this->Oranges->makeJuice();
$orange_sugar = $this->Oranges->sugar;
或者如果您的模型是 associated、
// ApplesController
$this->Apples->Oranges->makeJuice();
$orange_sugar = $this->Apples->Oranges->sugar;