Summary of some issues about thinkphp frame view model invocation
For the call of the view model in the TP frame is more commonly used in the project, this graduation design has encountered some problems to write the summary:
?
I. The model definition of a custom DAO ORM exception occurs when a view is called
?
When the model definition is shaped like
Class Usermodel extends Model {private $ormObj;/** * Constructor */function __construct () {$this->ormobj=m (' User ');}}
Then there is the definition of the view model:
Class Mentionviewmodel extends ViewModel {public $viewFields = Array (' mention ' =>array (' id ', ' tid ', ' uid '), ' Topic ' = = Array (' Create_time ', ' from ' = ' topic_from ', ' content ', ' status ', ' _on ' = ') ' Mention.tid=topic.id '), ' User ' = = Array (' nickname ', ' homepage ', ' Avatar ', ' _on ' = ' topic.uid= ') User.ID ') ); }
and the output SQL statement is actually:
SELECT mention.id as id,mention.uid as uid,mention.tid as tid,topic.create_time as create_time,topic.from as Topic_from,T Opic.content as Content,user.avatar as avatar,user.nickname as nickname,user.homepage as homepage from fl_mention mention Join Topic on Mention.tid=topic.id join User on topic.uid=user.id WHERE Topic.status=1 and mention.uid=1 ORDER by mention . ID desc LIMIT 0,20
If the table name to be aliased is missing, this DAO-style ORM call will appear in the view model where the table name cannot be found.
?
There is a function in the parent class ViewModel that extracts the table name:
public function Gettablename () {if (Empty ($this->truetablename)) {$t Ablename = "; foreach ($this->viewfields as $key = + $view) {//Get data table name $class = $key. ' Model '; $Model = class_exists ($class)? New $class (): M ($key); $tableName. = $Model->gettablename (); Table alias definition $tableName. =!empty ($view [' _as '])? ' '. $view [' _as ']: '. $key; Support for the on condition definition $tableName. =!empty ($view [' _on '])? ' On '. $view [' _on ']: '; Specify a join type such as right INNER left next table valid $type =!empty ($view [' _type '])? $view [' _type ']: '; $tableName. = ". Strtoupper ($type). ' JOIN '; $len = strlen ($type. ' _join '); } $tableName = substr ($tableName, 0,-$len); $this->truetablename = $tableName; } return $this->truetablename; }
Here the function sees the $Model->gettablename () that will call the superclass Model of the class:
?
Public Function Gettablename () { if (empty ($this->truetablename)) { $tableName =!empty ($this- >tableprefix)? $this->tableprefix: "; if (!empty ($this->tablename)) { $tableName. = $this->tablename; } else{ $tableName. = Parse_name ($this->name); } $tableName. =!empty ($this->tablesuffix)? $this->tablesuffix: "; if (!empty ($this->dbname)) $tableName = $this->dbname. '. ' $tableName; $this->truetablename = strtolower ($tableName); } return $this->truetablename; }
This function in this statement empty ($this->truetablename) is the name of the extraction table
The solution one is given below:
Class Usermodel extends Model {protected $trueTableName = ' fl_user '; private $ORMOBJ;/** * Constructor */function __construct () {$this->ormobj=m (' User ');}}
In this class, add protected $trueTableName = ' fl_user '; This property enables the corresponding table name to be found when the Gettablename function is triggered;
Solution Two:
function __construct () {parent::__construct (); $this->ormobj=m (' User ');}
? The overridden parent class constructor is reintroduced in subclasses
?
?
?
?
Second, the view model call from the connected table:
?
Shaped like:
Class Topicviewmodel extends ViewModel {public $viewFields = Array (' topic ' =>array (' id ' = ' root_id ', ' Content ' = ' + ' root_content '), ' Topic ' =>array (' id ' = ' topic_id ', ' create_time ', ' from ' = ' topic_ ') From ', ' content ', ' status ', ' _on ' = ' topic.id=topic.rootid ') );
The above statement executes the following SQL:
SELECT topic.id as root_id,topic.id as topic_id from Fl_topic topic joins Fl_topic topic on Topic.id=topic.rootid
The error of this SQL statement is obvious: Because SQL is case-insensitive, it will be reported directly. Not unique Table/alias: ' Topic ' error.
?
Keep looking at this function:
Public Function Gettablename () {if (Empty ($this->truetablename)) {$tableName = '; foreach ($this->viewfields as $key = + $view) {//Get data table name $class = $key. ' Model '; $Model = class_exists ($class)? New $class (): M ($key); $tableName. = $Model->gettablename (); Table alias definition $tableName. =!empty ($view [' _as '])? ' '. $view [' _as ']: '. $key; Support for the on condition definition $tableName. =!empty ($view [' _on '])? ' On '. $view [' _on ']: '; Specify a join type such as right INNER left next table valid $type =!empty ($view [' _type '])? $view [' _type ']: '; $tableName. = ". Strtoupper ($type). ' JOIN '; $len = strlen ($type. ' _join '); } $tableName = substr ($tableName, 0,-$len); $this->truetablename = $tableName; } return $this->truetablename; }
The above function for the previous ' topic ' Index just made a factory-style model generation. While the class_exists are case-insensitive, and the index of the array is case-sensitive, we can use this feature to represent a reference to the same table in a different case-only index
Then follow the _as property in the back:
?
The following code:
Public $viewFields = Array (' topic ' =>array (' id ' = ' root_id ', ' content ' = ' root_content ', ' _as ' = ' root ') _topic '), ' topic ' = = Array (' id ' = ' = ' topic_id ', ' create_time ', ' from ' = ' topic_from ', ' content ', ' Status ', ' _on ' = ' root_topic.id=topic.rootid ') ;
? This will give you the correct SQL statement.
SELECT root_topic.id as root_id,topic.id as topic_id from Fl_topic Root_topic joins Fl_topic topic on ROOT_TOPIC.ID=TOPIC.R Ootid
?
TP is indeed a rare mature framework of the domestic ~ ~ But some places still need to use some alternative techniques, self-connected table view model use always feel more awkward, and look forward to a better way to achieve the purpose of self-connection
?