Ardent + Laravel,自动水合物关系 [英] Ardent + Laravel, auto hydrate relation

查看:131
本文介绍了Ardent + Laravel,自动水合物关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我想更新一行时,我有一些问题。



我有两个模型的客户端和地址通过morphone关系相关。



这个关系很好,当我想要一个客户端这行返回预期的结果:

  Client :: with('address') - > find($ id); 

但是我无法理解如何使用干净的解决方案更新客户端。有人可以回答这些问题:


  1. 与Ardent如何autoHydrate相关模型?

  2. 你更新一些数据,lavarel最好的做法是什么?使用更新modod?使用保存?用推?填满所有型号?使用自动水合物?

当我在我的更新方法中记录Input :: all()时,我得到:

  [2014-05-31 15:52:56] production.INFO:{id:983,firstName:Susanne,姓氏 : 亚当  生日: 18\ / 06\ / 1982, inscriptionDate: 08\ / 09\ / 2013, 状态:3, created_at: 2014-05-31 14:26:25,updated_at:2014-05-31 14:26:25,email:bernard.alix@free.fr,address:{id :983,地址:大街艾蒂安,地址2:,ville:Cordierboeuf,cp:25 10,电话:0403983157 +33(0)3 0,addressable_id:983,addressable_type:客户端,created_at:2014-05-31 14:27:58,updated_at:2014-05-31 14:27:58}} [] [] 

如你所见,地址数据在客户端数据中。



3.当我使用更新,保存或推送(雄辩的方法)时,雄辩不明白他应该更新地址模型,然后更新相关的客户端模型。我的数据格式不是很好吗?



谢谢。



更新:



当我做Log :: info(Input :: all())时,我在控制器中得到以下json数据:

  [2014-06-01 18:10:46] production.INFO:{id:284,firstName:Andr\\\ée,姓氏 : 亚当  生日: 23\ / 07\ / 1944, inscriptionDate: 22\ / 11\ / 2013, 状态:2 created_at: 2014-06-01 15:41:22,updated_at:2014-06-01 18:06:44,电子邮件:monique17@normand.com,地址:{id 284,streetAddress:93,avenue Lefort,streetAddress2:,city:Boulay-sur-Leger,zipCode:14054,phone:09 51 03 1 ,mobile:+ 33 6 00 6,addressable_id:284,addressable_type:客户端,created_at:2014-06-01 15:42:50,updated_at -06-01 18:06:44}} [] [] 

用热忱的自动水合不工作...客户自动水合成功,但地址不,可能是由于多态相关n(一对一)他们之间。



我尝试以这种方式填充我的模型:

  $ client = Client :: with('address') - > find($ id); 
$ client-> update(Input :: except('address'));
$ client-> address-> update(Input :: only('address'));

但这不起作用,因为Input :: only('address')给出错误的数据,当我登录时,我得到:

  Log :: info(Input :: except('address')); 
Log :: info(Input :: only('address'));

//输出

[2014-06-01 18:20:34] production.INFO:{id:284,firstName:Andr\ u00e9e 姓氏: 亚当, 生日: 23\ / 07\ / 1944, inscriptionDate: 22\ / 11\ / 2013, 状态:2, created_at:2014-06-01 15:41:22,updated_at:2014-06-01 18:10:46,电子邮件:monique17@normand.com} [] []
[2014-06-01 18:20:34] production.INFO:{address:{id:284,streetAddress:93,avenue Lefort,streetAddress2:,城市:Boleay-sur-Leger,zipCode:14054,phone:09 51 03 1,mobile:+ 33 6 00 6,addressable_id:284,addressable_type :Client,created_at:2014-06-01 15:42:50,updated_at:2014-06-01 18:06:44}} [] []

所以我混合两种方法:

  $ inputs = Input :: except('_ method'); 
$ client = Client :: with('address') - > find($ id);

$ client-> update(Input :: except('address'));
$ client-> address-> update($ inputs ['address']);

这个工作很好!



但是我不明白为什么热心自动化失败...



谢谢。

解决方案>

没有什么是 clean 最佳做法,但情况需求。您可以根据自己的需要尝试以下方式:

  //获取客户端
$ client = Client ::与( '地址') - >找到($ ID);

//填写并保存客户端及其相关模型地址
$ client-> fill(array(...)) - > address-> fill(array(。 ..)) - >推();

有一件事你应该知道,在模型这里(客户端地址)你需要提供一个 protected $ fillable = [ 'propeertyName'] 数组或一个空数组来处理大量分配。在这种情况下,如果您在客户端表和您的地址 c中的名称 code>表如果你有一个字段,那么你可能只是使用;

 code> $ client-> fill(array('name'=>'Jhon'))
- > address-> fill(array('block'=>'d' )
- > push();

在这种情况下,两个字段都将在两个表中更新。如果您使用包含客户端地址的属性的表单提交所有字段,则必须选择适当的来自输入数组的项目/表单域。所以例如:

  $ inputs = Input :: except('_ method'); // $输入中的两个表的所有字段
//获取客户端模型的字段,获取所有
//但不包含属于地址表的字段
$ addressData = array('road ','阻止','城市');
$ clientData = Input :: except($ addressArray); //获取地址的所有字段

//填写并保存客户端及其相关模型地址
$ client-> fill($ clientData) - > address-> fill ($ addressdata) - >推();

还可以使用:

  $ client = Client :: with('address') - > find($ id); 
$ client-> fill(Input :: all()); //或者可能是Input :: only([...])或except([...])
$ client-> address-> city ='someNewCity';
$ client-> push();

您可以在这两种模式中单独使用 save 但是 push 为您保存。另外你可以尝试

  $ client = Client :: with('address') - > find($ id); 
$ client-> update(Input :: except('city'));
$ client-> address-> update(Input :: only('city'));

其实,保存 push 可以应用,如果模型使用其字段/属性填充,并且 create / update 首先使用填写方法,然后保存,就是这样。



BTW,不确定 Ardent


I use lavarel with ardent package.

I have some problem when i want to update a row.

I have 2 model Client and Address related by morphone relation.

This relation work well, when i want to get a client this line return expected result :

Client::with('address')->find($id);

But i can't understand how to update a client with a clean solution. Somebody can answer to these questions :

  1. With Ardent how could you autoHydrate related model ?
  2. When you update some data, what is the best practice in lavarel ? Use update methdod ? Use save ? Use push ? Fill all model ? Use auto hydrate ?

When i log Input::all() in my update method i get that :

[2014-05-31 15:52:56] production.INFO: {"id":983,"firstName":"Susanne","lastName":"Adam","birthDate":"18\/06\/1982","inscriptionDate":"08\/09\/2013","status":3,"created_at":"2014-05-31 14:26:25","updated_at":"2014-05-31 14:26:25","email":"bernard.alix@free.fr","address":{"id":983,"address":"avenue Etienne","address2":"","ville":"Cordierboeuf","cp":"25 10","phone":"0403983157","mobile":"+33 (0)3 0","addressable_id":983,"addressable_type":"Client","created_at":"2014-05-31 14:27:58","updated_at":"2014-05-31 14:27:58"}} [] []

As you see address data are inside client data.

3.When I use update, save or push (eloquent's method) eloquent does not understand that he should update Address model then update related Client model. My data's format isn't well formed ?

Thanks.

UPDATE :

When i do Log::info(Input::all()), i get the following json data in my controller :

[2014-06-01 18:10:46] production.INFO: {"id":284,"firstName":"Andr\u00e9e","lastName":"Adam","birthDate":"23\/07\/1944","inscriptionDate":"22\/11\/2013","status":2,"created_at":"2014-06-01 15:41:22","updated_at":"2014-06-01 18:06:44","email":"monique17@normand.com","address":{"id":284,"streetAddress":"93, avenue Lefort","streetAddress2":"","city":"Boulay-sur-Leger","zipCode":"14054","phone":"09 51 03 1","mobile":"+33 6 00 6","addressable_id":284,"addressable_type":"Client","created_at":"2014-06-01 15:42:50","updated_at":"2014-06-01 18:06:44"}} [] []

With ardent's autohydratation that doesn't work... Client autohydrate successfully but Address does not, maybe due to the polymorphic relation (one-to-one) between them.

I try to fill my models this way :

$client = Client::with('address')->find($id);
$client->update(Input::except('address'));
$client->address->update(Input::only('address'));

but this doesn't work because Input::only('address') gives wrong formed data, when i log this i get that :

Log::info(Input::except('address'));
Log::info(Input::only('address'));

//output 

[2014-06-01 18:20:34] production.INFO: {"id":284,"firstName":"Andr\u00e9e","lastName":"Adam","birthDate":"23\/07\/1944","inscriptionDate":"22\/11\/2013","status":2,"created_at":"2014-06-01 15:41:22","updated_at":"2014-06-01 18:10:46","email":"monique17@normand.com"} [] []
[2014-06-01 18:20:34] production.INFO: {"address":{"id":284,"streetAddress":"93, avenue Lefort","streetAddress2":"","city":"Boulay-sur-Leger","zipCode":"14054","phone":"09 51 03 1","mobile":"+33 6 00 6","addressable_id":284,"addressable_type":"Client","created_at":"2014-06-01 15:42:50","updated_at":"2014-06-01 18:06:44"}} [] []

So i mix two methods :

$inputs = Input::except('_method');
$client = Client::with('address')->find($id);

$client->update(Input::except('address'));
$client->address->update($inputs['address']);

This work pretty well !

But i can't understand why ardent's autohydration fails...

Thanks.

解决方案

There is nothing as clean or best practice but the situation demand. You may try something like these depending on your need:

// Get the Client
$client = Client::with('address')->find($id);

// Fill and save both Client and it's related model Address
$client->fill(array(...))->address->fill(array(...))->push();

One thing you should know that, in both models here (Client and Address) you need to provide a protected $fillable = ['propeertyName'] array or an empty array to handle mass assignment. In this case if you have a name field in clients table and in your addresses table if you have a block field then you may just use;

$client->fill(array('name' => 'Jhon'))
       ->address->fill(array('block' => 'd'))
       ->push();

In this case both fields will be updated in both tables. if you submit all the fields using a form that contains properties for both Client and Address then you have to pick the appropriate items/form fields from the input array. So for example:

$inputs = Input::except('_method'); // All fields for both tables in $inputs
// Get the fields for Client model, get all
// but not fields that belongs to address table
$addressData = array('road', 'block', 'city');
$clientData = Input::except($addressArray); // get all but fields of Address

// Fill and save both Client and it's related model Address
$client->fill($clientData)->address->fill($addressdata)->push();

Also you may use:

$client = Client::with('address')->find($id);
$client->fill(Input::all()); // Or maybe Input::only([...]) or except([...])
$client->address->city = 'someNewCity';
$client->push();

You may use save in both models individually but push saves both for you. Also you may try

$client = Client::with('address')->find($id);
$client->update(Input::except('city'));
$client->address->update(Input::only('city'));

Actually, save or push could be applied if the models are populated with their fields/properties and create/update fill the models first using fill method and then save them, that's it.

BTW, not sure about Ardent.

这篇关于Ardent + Laravel,自动水合物关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆