Cakephp:在运行时向 $uses 添加模型

发布于 2024-12-18 16:53:03 字数 291 浏览 0 评论 0原文

我需要编写一个可以与任何类型的控制器一起使用的模块化函数。我需要在运行时做类似的事情。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你爱我像她 2024-12-25 16:53:03

我认为你需要的是 loadModel 方法。

$tools=array("a","b","c");
foreach($tools as $tool) {
   // ...
   $this->loadModel($tool);
   $this->{$tool}->find();
}

更多信息请参见 CakePHP 的书

I think what you need is loadModel method.

$tools=array("a","b","c");
foreach($tools as $tool) {
   // ...
   $this->loadModel($tool);
   $this->{$tool}->find();
}

More info in CakePHP's Book

瑶笙 2024-12-25 16:53:03

我不知道在运行时将模型加载到控制器的方法。基本上,您需要将可能使用的所有模型添加到控制器的 $uses 数组中。

但是,如果模型与您的uses数组中的另一个模型相关联,那么您可以在运行时在控制器中绑定它们:

$this->Model1->bind(
    'hasOne' => array(
        'Model2'
    )
);

您可以将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:

$this->Model1->bind(
    'hasOne' => array(
        'Model2'
    )
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文