我有一個帶有students
表的數據庫設置。 students
表與一些其他表具有一對多的關係。即一個學生可以有多個科目。而且一個學生可以有多個獎項。PHP Active Record:Multiple有很多關聯
下面是代碼:
class student extends Model {
static $table_name = 'students';
static $primary_key = 'username';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $has_many = [
[
#'subjects', 'foreign_key' => 'studentUsername',
['subjects', 'foreign_key' => 'studentUsername'],
['academicAwards', 'foreign_key' => 'studentUsername'],
['nonAcademicAwards', 'foreign_key' => 'studentUsername'],
['sportParticipation', 'foreign_key' => 'studentUsername', 'class_name' => 'sportParticipation'],
['leadershipPositions', 'foreign_key' => 'studentUsername'],
['houseParticipation', 'foreign_key' => 'studentUsername', 'class_name' => 'houseParticipation']
]
];
}
class subject extends Model {
static $table_name = 'studentSubjects';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
}
class academicAward extends Model {
static $table_name = 'academicAwards';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
}
class nonAcademicAward extends Model {
static $table_name = 'nonAcademicAwards';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
}
class sportParticipation extends Model {
static $table_name = 'sportParticipation';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
}
class leadershipPosition extends Model {
static $table_name = 'leadershipPositions';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
}
class houseParticipation extends Model {
static $table_name = 'houseParticipation';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $belongs_to = [
['student']
];
當我運行代碼如上,我得到以下錯誤:
Warning: trim() expects parameter 1 to be string, array given in vendor\php-activerecord\php-activerecord\lib\Inflector.php on line 116
Notice: Uninitialized string offset: 0 in vendor\composer\ClassLoader.php on line 317
Notice: Uninitialized string offset: 0 in vendor\composer\ClassLoader.php on line 349
如果我有:
class student extends Model {
static $table_name = 'students';
static $primary_key = 'username';
static $datetime_format = 'Y-m-d H:i:s'; // https://stackoverflow.com/questions/17222180/phpactiverecord-incorrect-datetimeformat
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
}
它的工作原理很好,除了它只適用於單一關係; one student to many subjects
。
可能會有所幫助http://www.phpactiverecord.org/docs/ActiveRecord/HasMany – jamesmstone