开发者

CakePHP Blank Foreign Key

开发者 https://www.devze.com 2023-02-18 14:03 出处:网络
I have a situation where a user is asked to fill out a questionnaire about a building. The Building model could hasMany Questionnaire though, at least for now, it\'s sufficient that the Building hasOn

I have a situation where a user is asked to fill out a questionnaire about a building. The Building model could hasMany Questionnaire though, at least for now, it's sufficient that the Building hasOne Questionnaire. What's bothering me is that the former doesn't work while the latter does and I can't figure out why.

The keys follow convention: buildings.id is referenced in a foreign key from questionnaires.building_id. In the hasMany scenario, the foreign key just gets ignored when attempting to insert the questionnaires record:

INSERT INTO `questionnaires` (`deleted`, `modified`, `created`, `id`) VALUES (1, '2011-03-17 10:32:24', '2011-03-17 10:32:24', '4d821b78-7314-4ab4-a532-226f6e891b5e')

No mention of the building_id at all. As soon as I move Questionnaire from the hasMany array to the hasOne array, though, it all works perfectly.

Any insight into the inner magic would be great. I can't imagine that I'm the first person to bump into this, but I don't ever remember running into it before.

Thanks.

UPDATE

So I think I found the answer. In my form, since I'm really only defining one questionnaire (although I could support multiple), I was defining my input as Questionnaire.deleted which works fine for hasOne, but not so much for hasMany. For the latter, I have to create the input for Questionnaire.0.deleted. I've created forms for countless hasMany relationships where I was only intended to create one at a time and I swear I can't remember ever having to do that before. Has that always been a requirement? Maybe I j开发者_运维知识库ust need to put down the hard stuff for a while. Sheesh.


Rob Wilkerson,

this could mean that it always has been a requirement. I guess the reason for bumping into this issue is a mixture of hand-written and generated (cake bake) code, which occurs naturally.

So if one does not change the requirements during development and starts out with the generated code, it is possible to never look closely at the hasMany related 0s and everything stays fine.

Imho the framework could remove this source of error by handling hasOne as a subset of hasMany when it comes to forms. This way updating views could be omitted when relationships have to be changed.

Maybe filing an issue for the cake devs is indicated?

0

精彩评论

暂无评论...
验证码 换一张
取 消