0

i'm trying to save or update, when retrivieng mutliple records, but can't get it work..

When doing this, at first it look like it worked:

$c = Customer::select('Name', 'City');
$c[0]->update(array('Name' =>  'new Name'));
var_dump($c[0]['Name']);

But after a refresh and test it like that, i still see the oldname:

$c = Customer::select('Name', 'City');
var_dump($c[0]['Name']);

Somebody knows what i'm doing wrog?

i Have retrieve a json thing:

 data[0][Name]:Afrifield Corporation
 data[0][City]:Maidstone
 data[1][Name]:Amann Informatik AG
 data[1][City]:Reinach BL
 data[2][Name]:Antarcticopy
 data[2][City]:Antwerpen

if also have the columns Name and City which i want to update with this retrieved array..

so last question there is no faster solution than:

    $custNo = Customer::select('Name', 'No_')->get();

    for ($i = 0; $i < count($custNo); $i++) {
        $c = Customer::select('Name', 'City')->where('No_','=',$custNo[$i]->No_);
        $c->update(array('Name' =>  $input['data'][$i]['Name']));
    }

No_ is my PrimaryKey i also set this in the model class..

4

2 回答 2

0
$c[0]->name = "New name";
$c[0]->save();
于 2013-10-02T09:14:37.257 回答
0

代替get()and $c[0],尝试使用first(). 您还需要在更新后重新获取。它是对象,而不是数组。因此$c['Name'],除非您将其更改为数组,否则将无法使用。

$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get();
foreach ($customers as $customer) {
    $customer->update(array('Name' =>  'new Name'));
    //you need to re-fetch for the updated data, assuming you have an id field
    $currentCustomer = Customer::find($customer->id);
    echo var_dump($currentCustomer->Name);
}

编辑:对于大规模更新,您可以尝试以下操作:

$update = Customer::where('No_','=','DKT000142')->update(array('Name' =>  'new Name'));

Edit2:为了获得大规模更新和更新记录,这是我能想到的最好方法:

//First, we mass-update
$update = Customer::where('No_','=','DKT000142')->update(array('Name' =>  'new Name'));

//Then we get the updated data and loop:
$customers = Customer::select('Name', 'City')->where('No_','=','DKT000142')->get();
foreach ($customers as $customer) {
    echo var_dump($currentCustomer->Name);
}
于 2013-10-02T09:09:55.037 回答