cakephp-fixture-factories icon indicating copy to clipboard operation
cakephp-fixture-factories copied to clipboard

Association names plural vs singular

Open dereuromark opened this issue 1 year ago • 4 comments

The docs are a bit confusing to me https://github.com/vierge-noire/cakephp-fixture-factories/blob/main/docs/associations.md

They link to https://book.cakephp.org/4/en/orm/associations.html (v4 btw, not v5) - in main. As in Cake, I would assume all relations to be plural

$this->belongsTo('Authors');

But in this plugin, some things are plural, others are singular.

Shouldnt

ArticleFactory::make()->with('Authors.Address.City.Country', ['name' => 'Kenya'])

be

ArticleFactory::make()->with('Authors.Addresses.Cities.Countries', ['name' => 'Kenya'])

?

At least the contain() and other relation chaining in Cake would work this way, afaik?

dereuromark avatar Nov 26 '23 03:11 dereuromark

The first parameter in ->with() should be the name of the association as defined in the models.

Weither the association is plural or not depends on the association name.

If Cities has one Countries, then CityFactory::make()->with('Countries');

pabloelcolombiano avatar Nov 28 '23 19:11 pabloelcolombiano

Under HasOne Associations there are examples where asociations are singular, like HomeAddress.

I think it is ok to leave the examples as such, mixed plural and singular.

pabloelcolombiano avatar Nov 28 '23 19:11 pabloelcolombiano

Yeah, the Cake docs are bit confusing here. Usually the relation is always plural still, so I thought this could be a bit confusing.

$this->belongsTo('Countries', [
    'propertyName' => 'country'
]);

I opened a PR for discussion as this shouldnt be the case in default docs and conventions: https://github.com/cakephp/docs/pull/7776

dereuromark avatar Nov 28 '23 20:11 dereuromark

The 5.x docs have been fixed up to be consistent: https://book.cakephp.org/5/en/orm/associations.html#hasone-associations

Feel free to close if you wanna keep things as they are.

dereuromark avatar Nov 29 '23 02:11 dereuromark