CakePHP 使用 3 个模型和 saveAll

发布于 2024-09-13 07:40:27 字数 1972 浏览 6 评论 0原文

我有以下情况:

我正在开发一个地址应用程序来存储客户的详细信息。

我有以下数据库结构:

Clients hasMany Addresses
Addresses belongsTo Client
Addresses hasMany AddressEmails and AddressPhones

如果有人现在添加一个新客户,则应保存他的主要地址以及 1 个电子邮件和 1 个电话号码。

如果我使用以下代码在客户端的添加视图中执行此操作并在客户端控制器中调用 saveAll 方法,那么效果部分非常好:

 <?php
  echo $this->Form->input('client_group_id');
  echo $this->Form->input('company');
  echo $this->Form->input('firstname');
  echo $this->Form->input('lastname');
  echo $this->Form->input('www');
  echo $this->Form->input('Address.0.is_standard', array('type' => 'hidden', 'value' => '1'));
  echo $this->Form->input('Address.0.street');
  echo $this->Form->input('Address.0.zip');
  echo $this->Form->input('Address.0.city');
 ?>

但我不知道如何在此视图上保存电子邮件和电话号码。尝试过像 AddressEmail.0.email 这样的想法,但这不起作用。


明白了:

使用 Nik 发布的解决方案,它工作正常,我现在正在调用 $this->Client->Address->saveAll($this->data) 但必须像这样调整我的添加视图

<?php
    echo $this->Form->input('client_group_id');
    echo $this->Form->input('company');
    echo $this->Form->input('firstname');
    echo $this->Form->input('lastname');
    echo $this->Form->input('www');
    echo $this->Form->input('Address.is_standard', array('type' => 'hidden', 'value' => '1'));
    echo $this->Form->input('Address.street');
    echo $this->Form->input('Address.zip');
    echo $this->Form->input('Address.city');
    echo $this->Form->input('AddressEmail.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressEmail.0.email');
    echo $this->Form->input('AddressPhone.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressPhone.0.number');
?>

:我现在从地址模型调用 saveAll() 方法,不再需要零了!

I have following situation:

I'm developing an address-application to store the details of our clients.

I've got the following db-structure:

Clients hasMany Addresses
Addresses belongsTo Client
Addresses hasMany AddressEmails and AddressPhones

If someone now adds a new client, his primary address with 1 email and 1 phonenumber should be saved.

That works partly very good if I do this in the add-view of the client with the following code and call the saveAll-method in the client-controller:

 <?php
  echo $this->Form->input('client_group_id');
  echo $this->Form->input('company');
  echo $this->Form->input('firstname');
  echo $this->Form->input('lastname');
  echo $this->Form->input('www');
  echo $this->Form->input('Address.0.is_standard', array('type' => 'hidden', 'value' => '1'));
  echo $this->Form->input('Address.0.street');
  echo $this->Form->input('Address.0.zip');
  echo $this->Form->input('Address.0.city');
 ?>

But I have no idea how to save the email and the phone number over this view. Tried thinks like AddressEmail.0.email but that didn't work.


Got it:

With Nik's posted solution it's working fine, I'm now calling $this->Client->Address->saveAll($this->data) but had to adjust my add-view like this:

<?php
    echo $this->Form->input('client_group_id');
    echo $this->Form->input('company');
    echo $this->Form->input('firstname');
    echo $this->Form->input('lastname');
    echo $this->Form->input('www');
    echo $this->Form->input('Address.is_standard', array('type' => 'hidden', 'value' => '1'));
    echo $this->Form->input('Address.street');
    echo $this->Form->input('Address.zip');
    echo $this->Form->input('Address.city');
    echo $this->Form->input('AddressEmail.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressEmail.0.email');
    echo $this->Form->input('AddressPhone.0.type', array('type' => 'hidden', 'value' => 'Standard'));
    echo $this->Form->input('AddressPhone.0.number');
?>

Since I'm now calling the saveAll()-method from the Address-Model the zeros are not needed anymore!

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

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

发布评论

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

评论(1

旧城烟雨 2024-09-20 07:40:27

使用 saveAll() 保存相关数据仅适用于直接关联的模型。

所以基本上你可以使用 saveAll() 客户端(作为主要模型)和地址进行保存。您可以通过从地址模型保存来完成电子邮件和电话,例如:

$this->Client->saveAll($data); //This saves Client and Address
$this->Client->Address->saveAll($data); //This saves Email and Phone

我还没有测试过它,但它应该以这种方式工作。 (假设您的字段遵循命名约定,例如:

echo $this->Form->input('Email.0.field');
echo $this->Form->input('Email.1.field');
echo $this->Form->input('Email.2.field');
echo $this->Form->input('Phone.0.number');
echo $this->Form->input('Phone.1.number');
echo $this->Form->input('Phone.2.number');

我不确定它是否会直接“捕获”地址 ID,但您可以尝试。

另一种方法是仅保存模型

$this->Client-Address->saveAll($data);

。这样所有相关模型都将被保存(根据到文档 - 请参阅第一个)。

Saving related data with saveAll() will only work for directly associated models.

So basically you could save with saveAll() Client (as primary model) and Addresses. Emails and Phones you could do by saving from the Adress model like:

$this->Client->saveAll($data); //This saves Client and Address
$this->Client->Address->saveAll($data); //This saves Email and Phone

I haven't test it but it should work this way. (assuming that your fields follow the naming convention like:

echo $this->Form->input('Email.0.field');
echo $this->Form->input('Email.1.field');
echo $this->Form->input('Email.2.field');
echo $this->Form->input('Phone.0.number');
echo $this->Form->input('Phone.1.number');
echo $this->Form->input('Phone.2.number');

I am not sure if it will "catch" the Address ID directly, but you could try.

Another way is to save model from

$this->Client-Address->saveAll($data);

only. This way all related models will be saved (according to documentation - see first quite).

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