Ardent + Laravel,自动水合物关系

时间:2014-05-31 16:01:34

标签: php laravel eloquent ardent

我使用lavarel和热情的包裹。

当我想要更新一行时,我遇到了一些问题。

我有2个与morphone关系相关的模型客户端和地址。

这种关系很有效,当我想得到一个客户端时,这一行返回预期的结果:

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

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

  1. 有了Ardent你怎么能自动修复相关模型?
  2. 当您更新某些数据时,lavarel的最佳做法是什么?使用更新方法吗?使用保存?用推?填写所有型号?使用自动水合物?
  3. 当我在我的更新方法中记录Input :: all()时,我得到了:

    [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"}} [] []
    

    如您所见,地址数据位于客户端数据中。

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

    感谢。

    更新:

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

    [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"}} [] []
    

    随着ardent的自动水化不起作用......客户端成功自动水合但地址不成功,可能是由于它们之间的多态关系(一对一)。

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

    $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'));
    
    //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"}} [] []
    

    所以我混合了两种方法:

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

    这项工作非常好!

    但我无法理解为什么热心的自动水化会失败......

    感谢。

1 个答案:

答案 0 :(得分:5)

没有cleanbest practice,但情况需要。你可以根据自己的需要尝试这些:

// 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();

您应该知道,在models这里(ClientAddress),您需要提供一个protected $fillable = ['propeertyName']数组或一个空数组来处理{{} 3}}。在这种情况下,如果name表格中有clients字段,并且addresses表格中有block字段,那么您可以使用;

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

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

$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();

您也可以使用:

$client = Client::with('address')->find($id);
$client->fill(Input::all()); // Or maybe Input::only([...]) or 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'));

实际上,如果使用其字段/属性填充模型并save首先使用push方法填充模型,则可以应用create/updatefill,然后保存它们就是这样。

BTW,不确定Ardent