Adding user and associated data CakePhp 3.x and receive validation errors for associated data -
i have scoured internet , tried figuring out docs, don't understand doing wrong.
i have users can have membership , many orders. membership , orders associated membership level. unsure if these associations correct, think are.
here associations:
users:
$this->hasone('memberships', [ 'foreignkey' => 'user_id', 'jointype' => 'inner' ]); $this->hasmany('membershiporders', [ 'foreignkey' => 'user_id', 'jointype' => 'inner' ]);
orders:
$this->belongsto('users', [ 'foreignkey' => 'user_id', 'jointype' => 'inner' ]); $this->hasone('membershiplevels', [ 'foreignkey' => 'membership_level_id', 'jointype' => 'inner' ]);
memberships:
$this->belongsto('users', [ 'foreignkey' => 'user_id', 'jointype' => 'inner' ]); $this->hasone('membershiplevels', [ 'foreignkey' => 'membership_level_id', 'jointype' => 'inner' ]);
membershiplevels:
$this->belongstomany('membershiporders', [ 'foreignkey' => 'membership_level_id', 'jointype' => 'inner' ]); $this->belongstomany('memberships', [ 'foreignkey' => 'membership_level_id', 'jointype' => 'inner' ]);
in users controller trying have function add user, membership record, , order user.
i trying make sure validation errors shown if associated data in form.
in controller function have:
public function checkout($id = null) { $user = $this->users->newentity($this->request->data(), [ 'associated' => [ 'membershiporders' => ['associated' => ['membershiplevels']] ] ]); $membershiplevel = $this->users->memberships->membershiplevels->get($id); if ( $this->request->is('post') ) { $user = $this->users->patchentity( $user, $this->request->data(), [ 'associated' => [ 'membershiporders' => ['associated' => ['membershiplevels']] ] ]); if ( $this->users->save($user, ['associated' => ['membershiporders' => ['associated' => ['membershiplevels']]]]) ) { $this->flash->success(__('user has been added.')); } else { $this->flash->error(__('user not added. please, try again.')); } } $this->set(compact('user', 'membershiplevel')); $this->set('_serialize', ['user', 'membershiplevel']); }
a simple form view is:
<?= $this->form->create($user, ['id' => 'userform']) ?> <fieldset> <legend><?= __('primary member') ?></legend> <?= $this->form->input('full_name', ['required' => false]) ?> <?= $this->form->input('username', ['required' => false]) ?> <?= $this->form->input('email', ['required' => false]) ?> <?= $this->form->input('password', ['required' => false]) ?> <?= $this->form->input('password_confirmation', ['type' => 'password', 'required' => false]) ?> <?= $this->form->input('specialty', ['required' => false]) ?> <?= $this->form->input('membership_orders.0.billing_name', ['label' => 'name', 'data-stripe' => 'name', 'required' => false] ) ?> <?= $this->form->input('membership_orders.0.billing_street_1', ['label' => 'address 1', 'required' => false] ) ?> <?= $this->form->input('membership_orders.0.billing_street_2', ['label' => 'address 2', 'required' => false] ) ?> <?= $this->form->input('membership_orders.0.billing_city', ['label' => 'city', 'data-stripe' => 'address_city', 'required' => false] ) ?> </fieldset> <?= $this->form->button(__('join'), ['id' => 'submitbtn', 'class' => 'btn']) ?> <?= $this->form->end() ?>
i have tried membershiporders, membershiporders, current membership_orders, , , without .0 in form , cannot validation errors order information. billing info added orders table , exact table name "membership_orders".
shouldn't cake sending associated validation errors form well?
Comments
Post a Comment