I have a database setup with a students
table. The students
table has a one to many relationship with a few other tables. ie a student can have multiple subjects. but also a student can have multiple awards.
Here is the code:
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']
];
When I run the code as above, I get the following error:
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
If I have :
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',
]
];
}
It works fine, except it only works for the single relationship; one student to many subjects
.
Your first definition looks wrong I think: $has_many
should be an array of arrays, not an array of array of arrays. You have this in your single-has-many example with only students, but in your first example you've added an array.
so this works indeed:
static $has_many = [
[
'subjects', 'foreign_key' => 'studentUsername',
]
];
as should this:
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 ']'
];