Sunday, June 21, 2015

Saving 4 model level by one form and controller

Hi,

There are 4 tables:

User, Seller, Shopkeeper and Address. The relations are: one User has one Seller, one Seller has one Shopkeeper and one User has many Address.

I baked to create all the code I am using. To save the Shopkeeper I use the Shopkeeper Controller and the add.ctp of the Shopkeeper template.

I am able to save the Shopkeeper and the Seller data just saving the Shopkeeper entity: $this->Shopkeepers->save($shopkeeper)

But the problem is that it does not save the User data and the Address and I always have the constraint error:

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`sellers`, CONSTRAINT `sellers_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)


This is the Shopkeeper Controller add function:

    public function add()
    {
        $shopkeeper = $this->Shopkeepers->newEntity();
        $user = $this->Shopkeepers->Sellers->Users->newEntity();
        if ($this->request->is('post'))
        {
            $this->request->data['user']['status']         = 'Inativo';
            $this->request->data['user']['session']     = $this->request->session()->id();
            $this->request->data['user']['ip']             = $this->request->clientIp();
            $this->request->data['user']['role']         = 'Lojista';
            $this->request->data['user']['login_error'] = 0;
            $this->request->data['seller']['type']         = 'M';

            $user = $this->Shopkeepers->Sellers->Users->patchEntity($user, $this->request->data);

            $shopkeeper = $this->Shopkeepers->patchEntity($shopkeeper, $this->request->data);
           
           
            if ($this->Shopkeepers->save($shopkeeper))
            {
                $this->Flash->success('The shopkeeper has been saved.');
                return $this->redirect(['action' => 'index']);
            }
            else
            {
                $this->Flash->error('The shopkeeper could not be saved. Please, try again.');
            }
        }

        $surveys = $this->Shopkeepers->Sellers->Users->Surveys->find('list');
        $this->set(compact('users', 'surveys'));

        $plans = $this->Shopkeepers->Sellers->Plans->find('list');
        $this->set(compact('sellers', 'plans'));

        $this->set(compact('shopkeeper'));
        $this->set('_serialize', ['shopkeeper']);
    }

This is the add.ctp

<div class="actions columns col-lg-2 col-md-3">
</div>
<div class="shopkeepers form col-lg-7 col-md-9 columns">
    <?= $this->Form->create($shopkeeper);?>
    <fieldset>
        <legend><?= __('Add Shopkeeper') ?></legend>
        <?php
            echo $this->Form->input('user.name',['label' => __('Nome Fantasia')]);
            echo $this->Form->input('cnpj',['label' => __('CNPJ')]);
            echo $this->Form->input('seller.contact',['label' => __('Pessoa de contato')]);
            echo $this->Form->input('user.username',['label' => __('Email')]);
            echo $this->Form->input('user.password',['label' => __('Senha')]);
            echo $this->Form->input('user.password_confirmation',['type' => 'password', 'label' => __('Confirmação de Senha')]);
            echo $this->Form->input('user.survey_id', ['options' => $surveys,'label' => __('Como conheceu'),'empty' => __('--Selecione--')]);
            echo $this->Form->input('seller.plan_id', ['options' => $plans,'label' => __('Selecione um Plano'),'empty' => __('--Selecione--')]);
            echo $this->Form->input('seller.website',['label' => __('Página na internet')]);

            echo $this->Form->label(__('Link dos seus discos no Prefiro Vinil'));
            echo "<div class='input-group'>";
            echo "<span class='input-group-addon' id='basic-addon1'>www.prefirovinil.com.br/</span>";
            echo $this->Form->input('seller.link',['aria-describedby' => 'basic-addon1', 'label' => false]);
            echo "</div>";
            echo $this->Form->input('token_bcash');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Salvar'), ['class' => 'btn-success']) ?>
    <?= $this->Form->end() ?>
</div>


Can anyone help me?

Thanks.

Paulo




--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: