php - yii2 sqlite 无法解析表名

标签 php sqlite yii2 generator gii

我在 sqlite-conf 中发现了一些错误,我不知道,但它似乎没有按预期工作。 我有以下 db.php 文件

return [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:somedb',
'charset' => 'utf8',

];

我可以创建和运行迁移

    use yii\db\Migration;
    use yii\db\sqlite\Schema;

class m160222_083002_create_some_table extends Migration
{
    public function up()
    {
        $this->createTable('some_table', [
            'id' => Schema::TYPE_PK,
            'name' => Schema::TYPE_STRING
        ]);
    }

    public function down()
    {
        $this->dropTable('some_table');
    }
}

这些工作正常,创建表格等。

    $ ./yii migrate
Yii Migration Tool (based on Yii v2.0.7)

Total 1 new migration to be applied:
        m160222_083002_create_some_table

Apply the above migration? (yes|no) [no]:y
*** applying m160222_083002_create_some_table
    > create table some_table ... done (time: 0.109s)
*** applied m160222_083002_create_some_table (time: 0.170s)


1 migration was applied.

Migrated up successfully.

但随之而来的是一些神奇的东西。 我不能使用本地服务器上的 gii 生成器,例如

somelocal.loc/gii/model

当我输入表名时

some_table

答案是

Table 'some_table' does not exist.

但是从 CLI 运行 gii 模型生成器运行完美

    $ ./yii gii/model --tableName=some_table --modelClass=someTable
Running 'Model Generator'...

The following files will be generated:
        [new] models/someTable.php

Ready to generate the selected files? (yes|no) [yes]:y

Files were generated successfully!
Generating code using template "/var/www/sweethouse.loc/vendor/yiisoft/yii2-gii/generators/model/default"...
 generated models/someTable.php
done!

生成如下代码

    namespace app\models;

use Yii;

/**
 * This is the model class for table "some_table".
 *
 * @property integer $id
 * @property string $name
 */
class someTable extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'some_table';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
        ];
    }
}

但如果只有

somelocal.loc/gii/model

没用,这并不可惜,CLI 生成器更友好。

但对我来说最重要的问题是 mysite 无法解析表名, ActiveRecord 看不到它,并导致站点崩溃并出现以下错误

    Database Exception – yii\db\Exception

SQLSTATE[HY000]: General error: 1 no such table: some_table
Failed to prepare SQL: SELECT * FROM `some_table`
Error Info: Array
(
    [0] => HY000
    [1] => 1
    [2] => no such table: some_table
)
↵
Caused by: PDOException

SQLSTATE[HY000]: General error: 1 no such table: some_table

in /var/www/mysite/vendor/yiisoft/yii2/db/Command.php at line 225

还有 Controller 操作,我想在其中访问我的表值

public function actionSome()
{
    $some = someTable::find()->all();

    return $this->render('some',[
        'some' => $some
    ]);
}

只有sqlite3数据库才会出现以下错误

最佳答案

将 dsn 值设置为数据库的系统路径。

为了使其可移植,使用 yii 提供的路径别名:

'dsn' => 'sqlite:@app/db/database.db',

关于php - yii2 sqlite 无法解析表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551566/

相关文章:

php - 执行mysqldump以sql格式备份数据库

php - MYSQL语法错误

yii2 - 无法使用assets/registerCss函数注册css/js

api - Yii 2 RESTful API使用OAuth2进行身份验证(Yii 2高级模板)

php - UNION SELECT mysql php pdo 无值返回

php - 清理表单数据

SQLite ADO .NET 和 ExecuteScalar()

iphone - 函数 'sqlite3_key' 的隐式声明?

mysql - 在 Rails 应用程序中设置 MySQL

php - 如何更改 Yii2 中的目录等 url 参数?