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 );
}
} |