Recently, I faced a task, for solving which it is convenient to use a PHP framework. After examining the available information, description, capabilities, the
survey on Habré (in which, by the way, the sum of the respondents is not 100%, but 143% (?)) Was chosen Yii. This framework gave me the presence of the necessary features, such as authorization and captcha. The
official site says:
Yii is a highly efficient, component-based PHP framework for the rapid development of large web applications. It allows you to maximize the use of code reuse and can significantly speed up web development.
However, my acquaintance with Yii, which began with beautiful forms for generating code, quickly turned to digging into the source code.
After installation, everything went smoothly. Following the instructions, I tried to create a test site, generated a model and a CRUD using
Gii for the user table. Clicked - it works.
It's time to do the same thing, but for MySQL. As before, I enter the model generator, enter the name of the tablet and see: “Table 'user' does not exist.”. Strange. Probably something wrong. About an hour, I checked with the manuals and examples - everything is like everyone else, but the tablets are not visible. I tried to connect to the database in the code:
$connection=Yii::app()->db; $sql="SELECT * FROM user"; $dataReader=$connection->createCommand($sql)->query(); while(($row=$dataReader->read())!==false) { echo var_dump($row); }
Everything worked. Although Gii did not see the table. There is nothing on the Internet except how to check once again the configuration of the site and the availability of the table in MySQL did not offer. I have already performed these two items several times and therefore decided to see how Gii is trying to find a table in the source code.
')
The starting point was discovered by chance. When I tested with different lines of connection to the database, the place where the connection to the database was made was displayed in the debug information. In the neighborhood there was a function that checked the name of the tablet and in case of its absence produced my error. After spending several hours inserting var_dumps and tracking execution paths, I did the following:
File name | Function |
/yii/framework/gii/generators/model/ModelCode.php | validateTableName |
/yii/framework/gii/generators/model/ModelCode.php | getTableSchema |
/yii/framework/db/schema/CDbSchema.php | getTable |
/yii/framework/db/schema/mysql/CMysqlSchema.php | loadTable |
/yii/framework/db/schema/mysql/CMysqlSchema.php | findColumns |
As a result, the code found in the findColumns function led me to solve the problem:
protected function findColumns($table) { $sql='SHOW FULL COLUMNS FROM '.$table->rawName; try { $columns=$this->getDbConnection()->createCommand($sql)->queryAll(); } catch(Exception $e) { return false;
Yes Yes! No handling of possible exceptions, no information about them, just return false (which, by the way, turned into null in the calling function). Having added the regular var_dump before returning false, I managed to find out that MySQL cannot create a temporary file: Can't create / write to file / var / tmp / mysql. (Errcode: 2). With this information it was already possible to act further. For example, it helped me.
By the way, I forgot to mention that I am new to PHP. I don’t know how the problem was solved by more experienced colleagues. I will be glad to hear the advice. For myself, I plan in the near future in the source code of Yii to add a check of the debugging mode and display information about the exceptions. I went to generate models for my plates =)