SkyPHP / skyphp

PHP5 Framework
http://switchbreak.com/skyphp
17 stars 38 forks source link

1-to-many nested objects do not update #307

Open will123195 opened 10 years ago

will123195 commented 10 years ago

Suppose we are submitting a form and changing the year of the second book to 1957.

// submit author object with 1-to-many books:
$_POST = [
    'id' => 1,
    'name' => 'Jack Kerouac',
    'books' => [
        [
            'id' => 1,
            'title' => 'The Town and the City',
            'year' => 1950
        ],
        [
            'id' => 2,
            'title' => 'On the Road',
            'year' => 1957  // this value is different than what is in the db
        ]
    ]
];

use \My\Model\author;

// this does not work as expected
// (the value is not updated in the db, however inserting a new book does work)
$author = author::get($_POST)->save();

// until this is fixed, there is a work-around
// save the books first, then save the author
$author = author::get($_POST);
foreach ($author->books as $book) {
    $book->save();
}
$author->save();