phpactiverecord无法创建对象-undefined属性

时间:2017-04-08 15:13:44

标签: php sqlite

我正在测试PHP Active Record,但我遇到了一些问题。

设置AR

    ActiveRecord\Config::initialize(function($cfg)
    {
        $cfg->set_model_directory( __DIR__ . '/models');
        $cfg->set_connections(array(
            'development' => 'sqlite://:memory:' ));
    });

模型/ Author.php

class Author extends ActiveRecord\Model
{
    static $table_name = 'author';
}

模型/ book.php中

<?php

class Book extends ActiveRecord\Model
{
    static $table_name = 'book';

        static $belongs_to = array(
            array('author')
        );

}

创建新作者

$author = new Author();
$author->first_name = 'John';
$author->save();

制作新书

$book = new Book();
$book->title = 'Dead men tell no tales.';
$book->save();

创建作者表

CREATE TABLE [author]
        (
            [id] INTEGER  NOT NULL PRIMARY KEY,
            [first_name] VARCHAR(128)  NOT NULL
        )

创建书表

CREATE TABLE [book]
        (
            [id] INTEGER  NOT NULL PRIMARY KEY,
            [title] VARCHAR(255)  NOT NULL,
            [author_id] INTEGER,
            FOREIGN KEY (author_id) REFERENCES author(id)
        )

创建作者对象似乎很好,创建一本书会引发异常

 Uncaught ActiveRecord\UndefinedPropertyException: Undefined property: Book->author_id

使用标准PDO查询数据库似乎工作正常...所以我想知道我是否错过了Php Active Record中的某些内容

1 个答案:

答案 0 :(得分:0)

"#/filterpop"是非空外键,因此必须始终在插入时指定:

author_id