cfwheels icon indicating copy to clipboard operation
cfwheels copied to clipboard

AfterFind on outer joined model returns empty object

Open chapmandu opened this issue 10 years ago • 8 comments

Author.cfc Model

hasOne(name="Post", joinType="outer");

Controller

author = model("Author").findOne(include="Post");

When the controller queries the author, and the author has no post, a post object is returned containing all empty properties.

https://groups.google.com/forum/?fromgroups=#!topic/cfwheels/KSHSji3QwAs

  • Discovered by @lmah

chapmandu avatar Jun 19 '15 00:06 chapmandu

Will investigate for 1.4.2.

perdjurner avatar Aug 10 '15 08:08 perdjurner

Would be good to have a failing test for this one.

perdjurner avatar Aug 31 '15 11:08 perdjurner

If I do this with a hasMany association instead: author = model("Author").findOne(include="posts");

I get a "posts" array with no elements.

Given that it perhaps makes sense that a hasOne association returns an empty object?

If not, do I change both of the above to not even add the "post" / "posts" element to the returned base object?

perdjurner avatar Aug 31 '15 12:08 perdjurner

A compromise might be to return the empty object but with no properties added to it (as opposed to empty properties).

That would make it more similar to the empty "posts" array I guess.

perdjurner avatar Aug 31 '15 12:08 perdjurner

Thinking about this, if an object with no properties were returned, would this break nested objects in forms? http://docs.cfwheels.org/docs/nested-properties

chapmandu avatar Sep 01 '15 03:09 chapmandu

@chrisdpeters Any thoughts on this one?

perdjurner avatar Sep 01 '15 06:09 perdjurner

It likely would cause validation to fail when updating an object with nested properties (but in boundary cases where no data is set for that object).

I'm pretty sure that Rails returns nil in this case. Should we set the property to false to stay consistent with how findOne and findByKey behave?

chrisdpeters avatar Sep 01 '15 13:09 chrisdpeters

I think it makes sense to change the milestone on this one since it may cause backward compatibility issues.

perdjurner avatar Oct 10 '15 09:10 perdjurner