PHP Active Record:多个有很多关联
PHP Active Record: Multiple has many associations
我有一个数据库设置 students
table。 students
table 与其他几个 table 具有一对多关系。即一个学生可以有多个科目。而且一个学生也可以有多个奖项。
代码如下:
class student extends Model {
static $table_name = 'students';
static $primary_key = 'username';
static $datetime_format = 'Y-m-d H:i:s'; //
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'; //
static $belongs_to = [
['student']
];
}
class academicAward extends Model {
static $table_name = 'academicAwards';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class nonAcademicAward extends Model {
static $table_name = 'nonAcademicAwards';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class sportParticipation extends Model {
static $table_name = 'sportParticipation';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class leadershipPosition extends Model {
static $table_name = 'leadershipPositions';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class houseParticipation extends Model {
static $table_name = 'houseParticipation';
static $datetime_format = 'Y-m-d H:i:s'; //
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'; //
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
}
它工作正常,除了它只适用于单身关系; one student to many subjects
.
我认为您的第一个定义看起来不对:$has_many
应该是一个数组数组,而不是数组数组的数组。你在只有学生的单例中有这个,但在你的第一个例子中你添加了一个数组。
所以这确实有效:
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
应该这样:
static $has_many = [
//REMOVED '['
#'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']
//REMOVED ']'
];
我有一个数据库设置 students
table。 students
table 与其他几个 table 具有一对多关系。即一个学生可以有多个科目。而且一个学生也可以有多个奖项。
代码如下:
class student extends Model {
static $table_name = 'students';
static $primary_key = 'username';
static $datetime_format = 'Y-m-d H:i:s'; //
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'; //
static $belongs_to = [
['student']
];
}
class academicAward extends Model {
static $table_name = 'academicAwards';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class nonAcademicAward extends Model {
static $table_name = 'nonAcademicAwards';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class sportParticipation extends Model {
static $table_name = 'sportParticipation';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class leadershipPosition extends Model {
static $table_name = 'leadershipPositions';
static $datetime_format = 'Y-m-d H:i:s'; //
static $belongs_to = [
['student']
];
}
class houseParticipation extends Model {
static $table_name = 'houseParticipation';
static $datetime_format = 'Y-m-d H:i:s'; //
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'; //
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
}
它工作正常,除了它只适用于单身关系; one student to many subjects
.
我认为您的第一个定义看起来不对:$has_many
应该是一个数组数组,而不是数组数组的数组。你在只有学生的单例中有这个,但在你的第一个例子中你添加了一个数组。
所以这确实有效:
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
应该这样:
static $has_many = [
//REMOVED '['
#'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']
//REMOVED ']'
];