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

Troubles with BelongsTo::associate with unsaved Eloquent models #21990

Closed
zlodes opened this issue Nov 7, 2017 · 8 comments
Closed

Troubles with BelongsTo::associate with unsaved Eloquent models #21990

zlodes opened this issue Nov 7, 2017 · 8 comments

Comments

@zlodes
Copy link
Contributor

zlodes commented Nov 7, 2017

  • Laravel Version: 5.4.36
  • PHP Version: 7.1.2
  • Database Driver & Version: php-7.1-mysql, MySQL 5.7

Description:

I was trying to associate Eloquent model (unsaved) model with another (unsaved). After saving in database in table for first model no id of model from another table.
Seems like a bug.

Steps To Reproduce:

$people = new People();
$city = new City();

$people->city()->associate($city);

$city->save();
$people->save();

And... We have People without City.

@devcircus
Copy link
Contributor

IMO this is expected behavior. I wouldn't think you could create a relationship between 2 models that are not already represented in the database.

I believe at least one of the models must be saved in advance, in order to create the relationship.

@devcircus
Copy link
Contributor

related issue

@zlodes
Copy link
Contributor Author

zlodes commented Nov 8, 2017

@devcircus but we can access to related model before eloquent push ($people->city). Expected that $people->city and $city are same objects and when one of the models saved, another in moment of saving can access to key (id) of saved model.

@Dylan-DPC-zz
Copy link

Yeah i agree with @devcircus here, this looks like intended behaviour

@jekinney
Copy link

jekinney commented Nov 8, 2017

You can't associate a row from a different table if that row doesn't exist. You don't have an ID (or what ever unique key) to associate until you save it.

$people = new People();
$city = new City(); 

$people->city()->associate($city); // People has no ID, can't associate yet.

$city->save(); // saved City so now city has ID but people still doesn't.
$people->save(); // Now people has ID, Suppose to insert relationship how now? 

@zlodes
Copy link
Contributor Author

zlodes commented Nov 8, 2017

@jekinney, think about OOP and read my previous comment. At the moment of saving second second model (People) we have ID of first model (City).

And one database query is better than 2 anyway (we need to save two models and then make BelongsTo::associate and save again).

@sisve
Copy link
Contributor

sisve commented Nov 9, 2017

Could we at least add a CantDoThatYetException (or perhaps a TransientOwnerException) so that uses of the associate() method (and perhaps other method) are made of aware of the logic error?

@driesvints driesvints added bug and removed bug labels Nov 22, 2018
@driesvints
Copy link
Member

This is indeed the intended behavior for associate. If you want to propose a different way or add a new way to also persist relationships try to open up an issue at https://github.com/laravel/ideas

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

6 participants