dongtiannai0654 2016-02-22 10:44
浏览 131

yii2 sqlite无法解析表名

I found some bug in sqlite-conf, I don't know, but it doesnt seems to work as it intended. The thing I have the following db.php file

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

];

I can create and run migrations

    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');
    }
}

these are works fine, creates table etc.

    $ ./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.

But then comes some magic. I can't use gii generators from my local server, e.g.

somelocal.loc/gii/model

when I enter table name like

some_table

Answer is

Table 'some_table' does not exist.

However running gii model generator from CLI runs perfect

    $ ./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!

it generates the following code

    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',
        ];
    }
}

But if only

somelocal.loc/gii/model

didn't work, that wouldn't be pity, CLI generator is more friendly.

But the most important problem for me that mysite cant resolve tablenames, ActiveRecord doesn't see it, and crashes site with following error

    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

And controller action, where I want to access my table values

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

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

The following error comes only with sqlite3 database

  • 写回答

1条回答 默认 最新

  • doushe8577 2016-04-09 15:29
    关注

    Set the dsn value with the system path to the database.

    To make it portable, use the path alias provided by yii:

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

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持