PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] cakephp 3.* save belongsTo data

 February 21, 2022     cakephp-3.0, php, saving-data     No comments   

Issue

I have a frontend formular to add a new User. In addition it should add the Users Company to the Companies table and set the company_id of the new User to the new Companys id. On submit I get an error 'Unable to add the user.'

(its a User belongsTo Company association)

In UsersController.php:

public function add()
{
    $this->viewBuilder()->layout('login');
    $user = $this->Users->newEntity($this->request->data,['associated' => ['Companies']]);
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user,['associated' => ['Companies']]);
        if ($this->Users->save($user) ) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'login']);
        }
        $this->Flash->error(__('Unable to add the user.'));
    }
    $this->set('user', $user);

    $this->loadModel('Companies');
    $this->set('companies', $this->Companies->find('all',array('fields'=>array('name','id'))));
}

The debug of the $this->request->data returns:

    Array
(
   [username] => the name
   [email] => dat@goas.uups
   [password] => test
   [company] => Array
       (
           [name] => comanysnametest
           [iban] => DE02 520934 5  02384
           [bic] => Genoht 034
           [kontoinhaber] => the name here
           [straße] => alphastr
           [hausnummer] => 1337
           [plz] => 46467
           [ort] => Supp
           [geschäftsführung] => another name 
           [vat] => 038u pwdf8a spdf
       )

)

EDIT:

The UsersTable::validationDefault :

public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->add('email', 'valid', ['rule' => 'email'])
        ->requirePresence('email', 'create')
        ->notEmpty('email')
        ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

    $validator
        ->requirePresence('password', 'create')
        ->notEmpty('password');

    $validator
        ->requirePresence('username', 'create')
        ->notEmpty('username')
        ->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

    $validator
        ->allowEmpty('role');

    return $validator;
}

The CompaniesTable::validationDefault

public function validationDefault(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->allowEmpty('id', 'create');

    $validator
        ->requirePresence('name', 'create')
        ->notEmpty('name')
        ->add('name', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

    $validator
        ->requirePresence('modul', 'create')
        ->notEmpty('modul');

    $validator
        ->allowEmpty('iban');

    $validator
        ->allowEmpty('bic');

    $validator
        ->allowEmpty('kontoinhaber');

    $validator
        ->requirePresence('straße', 'create')
        ->notEmpty('straße');

    $validator
        ->requirePresence('hausnummer', 'create')
        ->notEmpty('hausnummer');

    $validator
        ->add('plz', 'valid', ['rule' => 'numeric'])
        ->requirePresence('plz', 'create')
        ->notEmpty('plz');

    $validator
        ->requirePresence('ort', 'create')
        ->notEmpty('ort');

    $validator
        ->requirePresence('geschäftsführung', 'create')
        ->notEmpty('geschäftsführung');

    $validator
        ->allowEmpty('vat');

    return $validator;
}

Solution

Use this debug($user->errors()); to see whether validation have gone wrong. This will print out array validation errors of each column if any.



Answered By - N Nem
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing