【问题标题】:yii2 sqlite fails to resolve table namesyii2 sqlite 无法解析表名
【发布时间】:2016-02-22 10:44:36
【问题描述】:

我在 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

答案是

表“some_table”不存在。

但是从 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

和控制器操作,我想在其中访问我的表值

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

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

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

【问题讨论】:

  • 我应该把它作为问题发送到 yii2 repo 吗?大家觉得有点奇怪

标签: php sqlite yii2 generator gii


【解决方案1】:

使用数据库的系统路径设置 dsn 值。

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

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

【讨论】:

  • 您是从其中一个应用开始的吗?别名是在基本模板和高级模板中定义的,但我不确定是不是只安装 yii yiiframework.com/wiki/667/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2021-03-24
  • 2022-07-07
  • 2017-02-28
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多