In YII2, the model gets the table field as a property by using "SHOW full COLUMNS from TABLE" instead of writing directly in the model. In this way, it is not necessary to read the database information, and the result of the table is not changed frequently. What are the reasons for using this approach?
protected function loadTableSchema($name) { $table = new TableSchema; $this->resolveTableNames($table, $name); if ($this->findColumns($table)) { $this->findConstraints($table); return $table; } else { return null; } }
protected function findColumns($table) { $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName); try { $columns = $this->db->createCommand($sql)->queryAll(); } ......... }
Reply content:
In YII2, the model gets the table field as a property by using "SHOW full COLUMNS from TABLE" instead of writing directly in the model. In this way, it is not necessary to read the database information, and the result of the table is not changed frequently. What are the reasons for using this approach?
protected function loadTableSchema($name) { $table = new TableSchema; $this->resolveTableNames($table, $name); if ($this->findColumns($table)) { $this->findConstraints($table); return $table; } else { return null; } }
protected function findColumns($table) { $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteTableName($table->fullName); try { $columns = $this->db->createCommand($sql)->queryAll(); } ......... }