CakePHP 将一个模型与其他三个模型中的一个唯一关联?

发布于 2025-01-07 10:38:14 字数 1679 浏览 0 评论 0原文

我在设计数据库和围绕它的 CakePHP 代码时遇到了问题。

我有 4 个模型,

  • ServerName
  • VirtualMachine
  • ServerHousing
  • ManagedServer

在 ServerName 中,我想保存所有 ServerName,然后可以在其他三个模型之一中使用它们。我怎样才能实现只能将一个 ServerName 链接到其他模型之一?

提前谢谢你们了。

编辑:

我现在做得有点不同。首先,它需要在模型本身中完成。 我在 cakePHP 模型中使用了 validate 选项。

代码是这样的:

public $validate = array(
    'server_name_id' => array(
        'rule' => 'serverNameTaken',
        'message' => 'This Servername has already been taken.'
    )
);

public function serverNameTaken()
{
    $this->ManagedServer = ClassRegistry::init("ManagedServer");
    // Assuming the server_name_id was passed from the form...
    $server_name_id = $this->data['VirtualMachine']['server_name_id'];

    // Check if this servername_id is already saved in virtual_machines
    if ($this->ManagedServer->find('count', array(
        'conditions' => array(
            'ManagedServer.server_name_id' => $server_name_id
        )
    )) > 0
    ) {
        // Found the server_name in the VirtualMachine model!
        return false; // Prohibit saving the data
    }

    if ($this->find('count', array(
        'conditions' => array(
            'VirtualMachine.server_name_id' => $server_name_id
        )
    )) > 0
    ) {
        // Found the server_name in the VirtualMachine model!
        return false; // Prohibit saving the data
    }


    // Do this for the other models too. If a return false is not hit by now,
    // everything should be fine and you can...
    return true;
}

我在其他模型中使用了相同的代码,只有代码需要更改。

再次感谢您的解答!!!

i got a problem with designing my Database and the CakePHP code around it.

I have 4 Models,

  • ServerName
  • VirtualMachine
  • ServerHousing
  • ManagedServer

In ServerName, i want to save all ServerNames which then can be used in either one of the three other models. How can i achieve that i am only able to link one ServerName to either one of the other models?

Thank you guys in advance.

EDIT:

I now did it a little bit different. First of all it need to be done in the Model itself.
I used the validate option in cakePHP's models.

The code is like this:

public $validate = array(
    'server_name_id' => array(
        'rule' => 'serverNameTaken',
        'message' => 'This Servername has already been taken.'
    )
);

public function serverNameTaken()
{
    $this->ManagedServer = ClassRegistry::init("ManagedServer");
    // Assuming the server_name_id was passed from the form...
    $server_name_id = $this->data['VirtualMachine']['server_name_id'];

    // Check if this servername_id is already saved in virtual_machines
    if ($this->ManagedServer->find('count', array(
        'conditions' => array(
            'ManagedServer.server_name_id' => $server_name_id
        )
    )) > 0
    ) {
        // Found the server_name in the VirtualMachine model!
        return false; // Prohibit saving the data
    }

    if ($this->find('count', array(
        'conditions' => array(
            'VirtualMachine.server_name_id' => $server_name_id
        )
    )) > 0
    ) {
        // Found the server_name in the VirtualMachine model!
        return false; // Prohibit saving the data
    }


    // Do this for the other models too. If a return false is not hit by now,
    // everything should be fine and you can...
    return true;
}

Same code I used in the other models, only the code had to be altered.

Thanks again for you answer!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一个人练习一个人 2025-01-14 10:38:14

您不能在模型层上执行此操作。模型要么关联,要么不关联。您可能正在寻找控制器中的一些逻辑,该逻辑在从另一个模型保存之前检查 ServerName X 的行是否已保存在任何其他模型中。

beforeSave 函数对此很有用。例如,在您的控制器中,输入:

public $uses = array('VirtualMachine', 'ServerHousing', 'ManagedServer');

public function beforeSave() {
    // Assuming the servername_id was passed from the form...
    $servername_id = $this->request->data['servername_id'];

    // Check if this servername_id is already saved in virtual_machines
    if($this->VirtualMachine->find('count', array(
        'conditions' => array(
            'VirtualMachine.servername_id' => $servername_id
        )
    )) > 0) {
        // Found the servername in the VirtualMachine model!
        return false; // Prohibit saving the data
    }

    // Do this for the other models too. If a return false is not hit by now,
    // everything should be fine and you can...
    return true;
}

希望这会让您朝着正确的方向前进。

You can't do this on the Model layer. Models are either associated or they are not. What you are probably looking for is some logic in your Controller that checks if a row for ServerName X has already been saved in any of the other models, prior to saving it from another model.

The beforeSave function is useful for this. For example, in your Controller, put this:

public $uses = array('VirtualMachine', 'ServerHousing', 'ManagedServer');

public function beforeSave() {
    // Assuming the servername_id was passed from the form...
    $servername_id = $this->request->data['servername_id'];

    // Check if this servername_id is already saved in virtual_machines
    if($this->VirtualMachine->find('count', array(
        'conditions' => array(
            'VirtualMachine.servername_id' => $servername_id
        )
    )) > 0) {
        // Found the servername in the VirtualMachine model!
        return false; // Prohibit saving the data
    }

    // Do this for the other models too. If a return false is not hit by now,
    // everything should be fine and you can...
    return true;
}

Hope this will get you going in the right direction.

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