Saving associated hasOne - not working ):

Just try to build a new setup and use only two or three tables then bake it. It will be a bit faster to test it. Btw if you will follow proper conventions all will work fine.

I will do this and give feedback afterwards. Thanks so far!

I think I came a step closer to resolve my problem. I pluralized my tables and followed the cakephp conventions. Next, i baked those tables and append to the add.ctp of “channels” template with the add.ctp of its child-table “channel_contacts”. (somehow field-validation is now also working for associated form fields :slight_smile:)

But … it would be too easy if it would work now :slight_smile: --> faced out the same problem … maybe CakePHP does not support such table-structure (extending one table to pretend overkill of columns?)

output of request->getData()

[
	'user_id' => '2',
	'password' => 'asdasd',
	'description' => 'asdads',
	'mode' => '12',
	'offline_duration' => '12',
	'active' => '1',
	'fsk' => '1',
	'volume' => '123',
	'deleted' => '1',
	'channel_contacts' => [
		'street' => 'asd',
		'zip_code' => 'asd',
		'place' => 'asd',
		'country_id' => '1',
		'contact_person_one' => 'asd',
		'contact_person_two' => 'asd',
		'contact_person_position_one' => 'asd',
		'contact_person_position_two' => 'asd',
		'phone_one' => 'asd',
		'phone_two' => 'asd',
		'email_one' => 'asd',
		'email_two' => 'asd',
		'date_create' => [
			'year' => '2017',
			'month' => '02',
			'day' => '24',
			'hour' => '10',
			'minute' => '31'
		],
		'date_change' => [
			'year' => '2017',
			'month' => '02',
			'day' => '24',
			'hour' => '10',
			'minute' => '31'
		]
	],
	'date_create' => [
		'year' => '2017',
		'month' => '02',
		'day' => '24',
		'hour' => '10',
		'minute' => '31'
	],
	'date_change' => [
		'year' => '2017',
		'month' => '02',
		'day' => '24',
		'hour' => '10',
		'minute' => '31'
	]
]

output after patching $channel = $this->Channels->patchEntity($channel, $this->request->getData(), [‘associations’ => [‘channel_contacts’]]);

object(App\Model\Entity\Channel) {

	'user_id' => (int) 2,
	'password' => 'asdasd',
	'description' => 'asdads',
	'mode' => '12',
	'offline_duration' => (int) 12,
	'active' => true,
	'fsk' => true,
	'volume' => (float) 123,
	'deleted' => true,
	'channel_contacts' => [
		'street' => 'asd',
		'zip_code' => 'asd',
		'place' => 'asd',
		'country_id' => '1',
		'contact_person_one' => 'asd',
		'contact_person_two' => 'asd',
		'contact_person_position_one' => 'asd',
		'contact_person_position_two' => 'asd',
		'phone_one' => 'asd',
		'phone_two' => 'asd',
		'email_one' => 'asd',
		'email_two' => 'asd',
		'date_create' => [
			'year' => '2017',
			'month' => '02',
			'day' => '24',
			'hour' => '10',
			'minute' => '31'
		],
		'date_change' => [
			'year' => '2017',
			'month' => '02',
			'day' => '24',
			'hour' => '10',
			'minute' => '31'
		]
	],
	'date_create' => object(Cake\I18n\FrozenTime) {

		'time' => '2017-02-24T10:31:00+00:00',
		'timezone' => 'UTC',
		'fixedNowTime' => false
	
	},
	'date_change' => object(Cake\I18n\FrozenTime) {

		'time' => '2017-02-24T10:31:00+00:00',
		'timezone' => 'UTC',
		'fixedNowTime' => false
	
	},
	'id' => (int) 5,
	'[new]' => false,
	'[accessible]' => [
		'*' => true,
		'id' => false
	],
	'[dirty]' => [],
	'[original]' => [],
	'[virtual]' => [],
	'[errors]' => [],
	'[invalid]' => [],
	'[repository]' => 'Channels'

}

If I would configure the association between the ChannelsTable and ChannelContactsTable from “hasOne” to “hasMany” (as cake\bake did), cakephp would recognize the form fields as “ChannelContacts”-Entity. :frowning:

UPDATE: i finally got it working. the problem was the naming of the child-table form field’s.
Wrong: channel_contacts.street (only if the association would by “hasMany”)
Correct: channel_contact.street (as the association is “hasOne”)

Thanks for you help!

1 Like

Good to know that finally the changes solved your problem.

1 Like

Just one more question …

I have a table called “metadata” - what would be the pluralized form of it, since data has no plural form? How would cakephp react to it?

Sometimes we stuck in this situation but if at-least you are making it smaller case the table will work fine. Btw for this the table name will be metadata itself.

alright - changed it to meta_data now. thanks so far! i am coming from the .net world (asp mvc) - therefore i was accustomed to use such naming conventions. :slight_smile:

No problem always happy to help :slight_smile:

i use polish names for tables and add ‘s’ on the end of name. Looks weird in my language but works :wink:

1 Like