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

Friday, March 18, 2022

[FIXED] Cant save data in multiple tables using cakePHP 3.7

 March 18, 2022     associations, cakephp, php, postgresql     No comments   

Issue

Been trying to save data into other table when saving Fisica entity to the Database, since I am new to cakephp it seems impossible right now, have seen multiple posts, videos, documentations and none seems to work. Please somebody help me figure out what is wrong with that.

I found a huge mistake in my relations between the entitys, now it works like that: Fisica belongsto Pessoa

associations at FisicasTable.php

$this->belongsTo('Pessoa')
        ->setForeignKey('id_pessoa')
        ->setJoinType('INNER')
        ->setClassName('Pessoas');

Fisicas Controller

$fisica = $this->Fisicas->newEntity();
    if ($this->request->is('post')) {
        $fisica = $this->Fisicas->patchEntity($fisica, $this->request->getData(),['associated' => 'Pessoa']);
        if ($this->Fisicas->save($fisica))...

Fisica add.ctp

<legend><?= __('Add Fisica') ?></legend>
    <?php
        echo $this->Form->control('nr_cpf');
        echo $this->Form->control('dt_nascimento');
    ?>
    <legend><?= __('Add Pessoa') ?></legend>
    <?php
        echo $this->Form->control('pessoa.vr_nome');
        echo $this->Form->control('pessoa.nr_telefone');
        echo $this->Form->control('pessoa.vr_email');
    ?>

I am getting the following error: error

I expected this code to insert in the two tables when submitting a new register to Fisica add.ctp.


Solution

I found that i had an issue naming my tables in the database combined with the primary key naming pattern. Simply updating the names of the tables to "Fisicas" and "Pessoas" and changing their Primary and Foreign key's to fisica_id and pessoa_id and generating all the code with bake (cake bake all pessoas, for example) resolved the problem (and generated the associations automatically).



Answered By - Natan Riboli
  • 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