CakePHP 3: save method is always returning false, but my entity is fine

Question

That's my add() method. When a customer is inserted I check if the email sent already exists on Users table and get its code. If not, the user is inserted:

public function add()
{
    $customer = $this->Customer->newEntity();
    if ($this->request->is('post')) {
        $email = $this->request->data['email'];
        $queryUser = $this->Customer->Users
            ->find('all')
            ->select(['id'])
            ->where(['username' => $email]);
        $findUser = $queryUser->first();

        if($findUser){
            $idUser = $findUser->id;
        } else {
            $user = $this->Customer->Users->newEntity();
            $newUser = [
                'username' => $email,
                'password' => '123',
                'role' => 'customer',
                'active' => true,
                'email' => $email
            ];               
            $user = $this->Customer->Users->patchEntity($user, $newUser);

            if ($this->Customer->Users->save($user)) {
                $idUser = $user->id;
            } else {
                throw new NotFoundException('Não foi possível salvar o usuário');
            }
        }

        $this->request->data['user_id'] = $idUser;
        $customer = $this->Customer->patchEntity($customer, $this->request->data);
        $this->debugDie($customer);

        if ($this->Customer->save($customer)) {
            $retorno = $this->preparaRetorno(true, $customer->id, 'Customer');                
        } else {
            $retorno = $this->preparaRetorno(false, 'O cliente não pôde ser salvo', 'Customer'); 
        }

        $this->set(compact('retorno'));
        $this->set('_serialize', 'retorno');
    }
}

The entity created is fine. That's its debug:

object(App\Model\Entity\Customer) {

    'nomecliente' => 'novo-cliente-sem-7',
    'cnpj' => '10252503627',
    'bairro_id' => (int) 1,
    'nomerua' => 'Contorno',
    'numero' => (int) 2905,
    'complemento' => '405',
    'pessoacontato' => 'José',
    'email' => 'novonovo@nn.lkj',
    'telefone1' => '3145555',
    'telefone2' => '988888',
    'cep' => '30110915',
    'país' => 'Brasil',
    'estado' => 'MG',
    'cidade' => 'Belo Horizonte',
    'user_id' => (int) 3,
    '[new]' => true,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'nomecliente' => true,
        'cnpj' => true,
        'bairro_id' => true,
        'nomerua' => true,
        'numero' => true,
        'complemento' => true,
        'pessoacontato' => true,
        'email' => true,
        'telefone1' => true,
        'telefone2' => true,
        'cep' => true,
        'país' => true,
        'estado' => true,
        'cidade' => true,
        'user_id' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Customers'

}

I activated the database log but there's no INSERT on the logs. This problem is driving me crazy! I don't understand why save() is always returning false, since my entity has no validation errors.

Thanks any help.


Show source
| cakephp   | cakephp-3.0   2017-01-06 20:01 0 Answers

Answers to CakePHP 3: save method is always returning false, but my entity is fine ( 0 )

Leave a reply to - CakePHP 3: save method is always returning false, but my entity is fine

◀ Go back