У меня есть настройка базы данных с таблицей 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