CakePHP 使用 3 个模型和 saveAll
我有以下情况:
我正在开发一个地址应用程序来存储客户的详细信息。
我有以下数据库结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以基本上你可以使用 saveAll() 客户端(作为主要模型)和地址进行保存。您可以通过从地址模型保存来完成电子邮件和电话,例如:
我还没有测试过它,但它应该以这种方式工作。 (假设您的字段遵循命名约定,例如:
我不确定它是否会直接“捕获”地址 ID,但您可以尝试。
另一种方法是仅保存模型
。这样所有相关模型都将被保存(根据到文档 - 请参阅第一个)。
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:
I haven't test it but it should work this way. (assuming that your fields follow the naming convention like:
I am not sure if it will "catch" the Address ID directly, but you could try.
Another way is to save model from
only. This way all related models will be saved (according to documentation - see first quite).