I have several associations on a table. When I query with containment one of them shows up with a capitalized property name and the data is coming in as an array, not an entity.
I’ve tried specifically setting its property but it remains capitalized.
I can’t find anything in the entity or elsewhere in the table that is any different than any other table setup. But this one property remains capitalized and in array form.
I can accommodate the detail in code, but the behavior is not normal and I’d prefer not to have chaos cascade through my system…
The only thing I notice is that you’re using a non-standard naming structure for that association, Tenant instead of Tenants. So that’s why I thought of the classes. Is there a reason for that difference? The association should end up being called tenant even if you use Tenants as the association, I believe.
yes. I typically change the association names to match my intended use of the data rather than the defaults. It helps me keep my thinking straight. In this case an Item only has one Tenant so I name it singular.
I often change alias names in other ways to make the property more descriptive of my use in a specific situation. So this is not unusual code for me.
In the case of hasOne or belongsTo associations, though, Cake already knows there’s only going to be one, so it uses singular for the property name, even though it’s a plural in the association name. For me, it’s always easier to consistently use the plural name there, because there’s zero thought required about it when I’m doing my containment. I would just always contain Tenants, and no worry myself over whether it’s supposed to be Tenant in this particular situation.
Did you add the code of the query in afterwards, or was it there from the start and I missed it? Does it always work for you using lower case containment like that? I always use ->contain(['Tenants', 'Locations']), and I never have this sort of issue…
Yep, Cake does what it can to make things make sense, so I generally let it do its thing and try to stay out of the way. I guess it’s got something in there somewhere to try when you give it lower case association names, and that’s maybe conflicting with something else in this case to get the wrong result. If you give it what it’s expecting, then it knows exactly what to do instead of having to muddle around in murkier waters.