error on save referenced table with transactions

Hi,

I got this error

Value of field "cod_modulo" does not exist on referenced table

on save a related models.

My models (all generated by dev tools):

Acao.php

<?php

namespace App\Models\Sicop;

class Acao extends ModelBamble
{

    /**
    * fields, getters and setters, etc
    */

    //...

    /**
     * Initialize method for model.
     */
    public function initialize()
    {

        $this->setSchema("sch_permissao");

        $this->hasMany('id_acao', 'App\Models\Sicop\ModuloAcao', 'cod_acao', [
            'alias' => 'ModuloAcao',
        ]);

        $this->hasMany('id_acao', 'App\Models\Sicop\Permissao', 'p_cod_acao', [
            'alias' => 'Permissao',
        ]);

        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'acao';
    }

    //...

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return [
            'id_acao'         => 'id_acao',
            'acao'            => 'acao',
            'acao_human_name' => 'acao_human_name',
            'deletado'        => 'deletado',
        ];
    }

}

Modulo.php

<?php

namespace App\Models\Sicop;

class Modulo extends ModelBamble
{

    /**
    * fields, getters and setters, etc
    */

    //...

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("sch_permissao");

        $this->hasMany('id_modulo', 'App\Models\Sicop\ModuloAcao', 'cod_modulo', [
            'alias' => 'ModuloAcao',
        ]);

        $this->hasMany('id_modulo', 'App\Models\Sicop\Permissao', 'p_cod_modulo', [
            'alias' => 'Permissao',
        ]);

        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'modulo';
    }

    //...

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return [
            'id_modulo' => 'id_modulo',
            'modulo'    => 'modulo',
            'descricao' => 'descricao',
            'deletado'  => 'deletado',
        ];
    }

}

ModuloAcao.php

<?php

namespace App\Models\Sicop;

class ModuloAcao extends ModelBamble
{

    /**
    * fields, getters and setters, etc
    */

    //...

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("sch_permissao");
        $this->hasMany('id_modulo_acao', 'App\Models\Sicop\Permissao', 'cod_modulo_acao', [
            'alias' => 'Permissao'
        ]);
        $this->belongsTo('cod_acao', 'App\Models\Sicop\Acao', 'id_acao', [
            'foreignKey' => true,
            'alias'      => 'Acao'
        ]);
        $this->belongsTo('cod_modulo', 'App\Models\Sicop\Modulo', 'id_modulo', [
            'foreignKey' => true,
            'alias'      => 'Modulo'
        ]);

        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'modulo_acao';
    }

    //...

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return [
            'id_modulo_acao' => 'id_modulo_acao',
            'cod_modulo'     => 'cod_modulo',
            'cod_acao'       => 'cod_acao',
            'deletado'       => 'deletado',
        ];
    }

}

the code that generates the error:

        $success = true;
        try {

            // in the transaction, $modulo is saved
            if (!$modulo->save()) {

                $msgLog = '';
                foreach ($modulo->getMessages() as $message) {
                    $msgLog .= $message . PHP_EOL;
                }

                $this->logger->error('Erro ao salvar módulo.' . PHP_EOL . $msgLog);

                $transaction->rollback("Não foi possível salvar o módulo.");

            }



                // $codAcoesMarcados is a $_POST array
                foreach ($codAcoesMarcados as $uidAcao) {

                    // at this point, I can get the IdModulo, returned by the auto increment DB sequence
                    // in database, the sequence is incremented
                    $this->logger->debug('IdModulo: ' . $modulo->getIdModulo());

                    $moduloAcao = new ModuloAcao();
                    $moduloAcao->setTransaction($transaction);
                    $moduloAcao->setCodModulo($modulo->getIdModulo());
                    $moduloAcao->setCodAcao($uidAcao);

                    // don't save!!!
                    // the error is
                    // Value of field "cod_modulo" does not exist on referenced table
                    // but, I can got by $modulo->getIdModulo()
                    if (!$moduloAcao->save()) {

                        $msgLog = '';
                        foreach ($moduloAcao->getMessages() as $message) {
                            $msgLog .= $message . PHP_EOL;
                        }

                        $this->logger->error('Erro ao salvar ações do módulo.' . PHP_EOL . $msgLog);

                        $transaction->rollback("Não foi possível salvar as ações do módulo.");

                        break;
                    }
                }// /foreach ($codAcoesMarcados as $uidAcao) {


            // commit
            $transaction->commit();

        // capturando a exceção
        } catch (TxFailed $exc) {
            $this->flash->error('Erro ao salvar!');
            $this->logger->error('Erro ao salvar módulo.' . PHP_EOL . $exc->getTraceAsString());
            $success = false;
        }

php 5.5.9

postgres 9.3

apache 2.4

what it can be?

thanks

sorry for my english.



61.1k

probably this

$this->hasMany('id_modulo', 'App\Models\Sicop\ModuloAcao', 'cod_modulo', [
            'alias' => 'ModuloAcao',
        ]);

do you have cod_modulo in Sicop\ModuloAcao DataBase ?

yes

CREATE TABLE sch_permissao.modulo_acao
(
  id_modulo_acao bigserial NOT NULL,
  cod_modulo integer,
  cod_acao integer,
  deletado smallint DEFAULT 0,
  CONSTRAINT modulo_acao_pk PRIMARY KEY (id_modulo_acao),
  CONSTRAINT acao_fk FOREIGN KEY (cod_acao)
      REFERENCES sch_permissao.acao (id_acao) MATCH FULL
      ON UPDATE CASCADE ON DELETE SET NULL,
  CONSTRAINT modulo_fk FOREIGN KEY (cod_modulo)
      REFERENCES sch_permissao.modulo (id_modulo) MATCH FULL
      ON UPDATE CASCADE ON DELETE SET NULL
)

note that the error occurs because it does not find the value of cod_modulo in the Modulo table, but it is incremented in the database

thanks