简体   繁体   中英

cakephp insert on different table

I'm trying to insert the data of the following form in a table of database that is not the "default" one. However, i don't know how to do it, i'm still adapting to cakephp. I guess the form is done correctly and the names of the specific table on database are the ones in the inputs. But i'm not saying anywhere which table he should insert right? Is that not necessary? Or in case it's needed, what am i missing to specify the table i want?

    <?php   echo $this->Form->create('Ficha', array('action' => 'index')); ?>
//echo $this->Form->input('id_fichas', array('label' => 'Id Ficha:'));
echo $this->Form->input('class_subst', array('label' => 'Class mistura:'));
echo $this->Form->input('simbolos_perigo', array('label' => 'Símbolos:'));
echo $this->Form->input('frases_r', array('label' => 'Frases:'));
echo $this->Form->end('Finalizar Ficha'); ?>

By convention table objects will use a table that matches the lower cased and underscored version of the class name. In your case 'Ficha' will be mapped to 'fichas' table. If you want a different table you need to define it, based on the CakePHP version:

CakePHP 3.x

You can specify the table using the setTable() method:

class Example extends Table {

    public function initialize(array $config) {
        $this->setTable('my_table');

        // Prior to 3.4.0 use 'table' method
        // $this->table('my_table');
    }
}

Link to 3.x docs

CakePHP 2.x

The attribute $useTable in your Model will do the magic:

<?php
App::uses('AppModel', 'Model');

class Example extends AppModel {
   public $useTable = 'exmp'; // This model uses a database table 'exmp'
} 

Link to 2.x docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM