Cakephp:在运行时向 $uses 添加模型
我需要编写一个可以与任何类型的控制器一起使用的模块化函数。我需要在运行时做类似的事情。
$tools=array("a","b","c");
foreach($tools as $tool{
...
//here there should be something like add_to_$uses($tool)
$this->{$tool}->find();
显然,简单地将项目添加到 $this->uses
是行不通的。我该怎么做?
I need to write a modular function that could work with any kind of controller. I need, at runtime, to do something like.
$tools=array("a","b","c");
foreach($tools as $tool{
...
//here there should be something like add_to_$uses($tool)
$this->{$tool}->find();
Obviously simply adding the item to $this->uses
doesn't work. How am I supposed to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要的是
loadModel
方法。更多信息请参见 CakePHP 的书
I think what you need is
loadModel
method.More info in CakePHP's Book
我不知道在运行时将模型加载到控制器的方法。基本上,您需要将可能使用的所有模型添加到控制器的 $uses 数组中。
但是,如果模型与您的uses数组中的另一个模型相关联,那么您可以在运行时在控制器中绑定它们:
您可以将
hasOne
更改为您的模型的关系类型。您还可以根据需要添加任意数量的模型。与此相反的是
解除绑定
。I'm not aware of a way to load a model to a controller at runtime. Basically, you need to add all the models you might ever use to the controller's
$uses
array.However, if the models are associated to another model in your uses array, then you can bind them at runtime in the controller:
You can change the
hasOne
to the type of relationship for your model. You can also add as many models as you want.The converse of this is
unbind
.