Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

original_relations populated with non-existent IDs of new Models #327

Closed
andrewwheal opened this issue Dec 5, 2013 · 1 comment
Closed

Comments

@andrewwheal
Copy link
Contributor

I have Model_Product which has_many Model_Images and relates to Model_Category in a many_to_many

When editing a Model_Product the original_relations entry for it's Model_Images is getting set to an array of empty strings.

$product = Model_Product::find(1);

// $product->images == [ 1, 2, 3, 4, 5, 6 ]

unset($product->images);
$product->image = Model_Image::query()->where('id', 'IN', [ 20, 21, 22 ])->get();

...

// Within another method which is only passed the product's id
Model_Category::query()
    ->related('products')
    ->where('products.id', $product_id)
    ->get();

...

// $product->_original_relations['images'] == [ '', '', '' ]

$product->save();

// $product->images == [ 1, 2, 3, 4, 5, 6, 20, 21, 22 ]

After stepping through the execution of ORM I have isolated the problem to be the call to Orm\Model::_update_original_relations() from Orm\Query::hydrate() which is run as part of executing the Model_Category::query().

I think that #312 might solve this issue in my instance because the relations are already loaded when Orm\Query::hydrate() is run. However, in a more general case I think it might be necessary to check within Orm\Model::_update_original_relations() that the data the "original" IDs are being retrieved from is not new.

@andrewwheal
Copy link
Contributor Author

Added a pull request (#328) which solves this for me, not sure if it is the best solution but stops the above from being able to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant