添加用户和相关数据CakePhp 3.x并接收关联数据的验证错误

时间:2016-01-21 01:46:09

标签: cakephp cakephp-3.x

我已经浏览过互联网并尝试用文档解决这个问题,但我不明白我做错了什么。

我的用户可以拥有会员资格和许多订单。会员资格和订单也与会员级别相关联。我不确定这些关联是否正确,但我认为它们是正确的。

以下是我的协会:

用户:

    $this->hasOne('Memberships', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('MembershipOrders', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);

订单:

    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);
    $this->hasOne('MembershipLevels', [
        'foreignKey' => 'membership_level_id',
        'joinType' => 'INNER'
    ]);

成员:

    $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'
    ]);

在我的用户控制器中,我正在尝试使用一个功能来添加用户,会员记录和该用户的订单。

我正在尝试确保在我的表单中显示验证错误(如果有的话)。

在控制器功能中我有:

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 could not be added. Please, try again.'));
        }
    }
    $this->set(compact('user', 'membershipLevel'));
    $this->set('_serialize', ['user', 'membershipLevel']);
}

我的观点的一个简单形式是:

    <?= $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() ?>

我已经尝试过MembershipOrders,membershipOrders,当前的membership_orders,以及表格中有和没有.0的所有内容,并且无法获得订单信息的任何验证错误。计费信息将添加到订单表中,确切的表名称为&#34; membership_orders&#34;。

不应该将相关的验证错误发送到我的表单吗?

0 个答案:

没有答案