How to add table prefixes in phalcon database configuration

Source: Internet
Author: User
Tags mixed

Phalcon cannot set the data table prefix by default. Common frameworks support this feature.

Method 1:

Create a basic model, and then all models can be inherited on this class.

The code is as follows:

<? Php
Class BaseModel extends \ Phalcon \ Mvc \ Model {
Public function getSource ()
    {
Return 'gw _ '. strtolower (get_class ($ this ));
    }
}

However, this method uses the phalcon devtools command line mode to generate a model file with a table prefix.

Method 2:

2.1 add the database prefix to the app/config. php configuration file to configure tablePrefix.

The code is as follows:

'Database' => array (
'Adapter '=> 'mysql ',
'Host' => 'localhost ',
'Username' => 'root ',
'Password' => '',
'Dbname' => 'test ',
'Charset' => 'utf8 ',
'Port' => '123 ',
'Tableprefix' => 'gw _'
),

2.2 modify phalcon devtools

Code: phalcon \ devtools \ scripts \ Phalcon \ Builder \ Model. php
Add the code after $ table = $ this-> options-> get ('name') in row 220.

The code is as follows:


$ Table = $ this-> options-> get ('name ');
If (isset ($ config-> database-> tablePrefix )){
$ Table = $ config-> database-> tablePrefix. $ table;
}


In row 480

The code is as follows:

$ MethodRawCode [] = $ this-> snippet-> getModelSource ($ this-> options-> get ('name '));
Modify the code:

$ MethodRawCode [] = $ this-> snippet-> getModelSource ($ table );

Use the tool command

Phalcon model user

Generate model:

App/models/User. php

The following content does not prompt that the table does not exist:

PHP

The code is as follows:

<? Php
 
Use Phalcon \ Mvc \ Model \ Validator \ Email as Email;
 
Class User extends \ Phalcon \ Mvc \ Model
{
 
/**
     *
* @ Var integer
*/
Public $ id;
 
/**
     *
* @ Var string
*/
Public $ username;
 
/**
     *
* @ Var string
*/
Public $ password;
 
/**
     *
* @ Var integer
*/
Public $ status;
 
/**
     *
* @ Var string
*/
Public $ real_name;
 
/**
     *
* @ Var string
*/
Public $ mobile;
 
/**
     *
* @ Var string
*/
Public $ email;
 
/**
     *
* @ Var integer
*/
Public $ sex;
 
/**
     *
* @ Var integer
*/
Public $ logins;
 
/**
     *
* @ Var integer
*/
Public $ create_time;
 
/**
* Validations and business logic
     *
* @ Return boolean
*/
Public function validation ()
    {
$ This-> validate (
New Email (
Array (
'Field' => 'email ',
'Requestred' => true,
                )
            )
);
 
If ($ this-> validationHasFailed () = true ){
Return false;
        }
 
Return true;
    }
 
/**
* Initialize method for model.
*/
Public function initialize ()
    {
$ This-> setSource ("gw_user ");
    }
 
/**
* Returns table name mapped in the model.
     *
* @ Return string
*/
Public function getSource ()
    {
Return 'gw _ user ';
    }
 
/**
* Allows to query a set of records that match the specified conditions
     *
* @ Param mixed $ parameters
* @ Return User []
*/
Public static function find ($ parameters = null)
    {
Return parent: find ($ parameters );
    }
 
/**
* Allows to query the first record that match the specified conditions
     *
* @ Param mixed $ parameters
* @ Return User
*/
Public static function findFirst ($ parameters = null)
    {
Return parent: findFirst ($ parameters );
    }
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.