我正在测试PHP Active Record,但我面临一些问题。
设置AR
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_model_directory( __DIR__ . '/models');
$cfg->set_connections(array(
'development' => 'sqlite://:memory:' ));
});models/Author.php
class Author extends ActiveRecord\Model
{
static $table_name = 'author';
}models/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();创建author表
CREATE TABLE [author]
(
[id] INTEGER NOT NULL PRIMARY KEY,
[first_name] VARCHAR(128) NOT NULL
)创建book表
CREATE TABLE [book]
(
[id] INTEGER NOT NULL PRIMARY KEY,
[title] VARCHAR(255) NOT NULL,
[author_id] INTEGER,
FOREIGN KEY (author_id) REFERENCES author(id)
)创建author对象似乎很好,但创建一本书会引发异常
Uncaught ActiveRecord\UndefinedPropertyException: Undefined property: Book->author_id使用标准PDO查询数据库似乎运行良好...所以我想知道我是不是在Php活动记录中遗漏了什么
发布于 2017-04-10 18:29:31
author_id是一个非空的外键,因此必须始终在插入时指定:
$book = new Book();
$book->title = 'Dead men tell no tales.';
$book->author_id = 1; // replace this value with an appropriate one.
$book->save();https://stackoverflow.com/questions/43295808
复制相似问题