gpt4 book ai didi

PHP 事件记录 : Multiple has many associations

转载 作者:搜寻专家 更新时间:2023-10-30 23:44:12 25 4
gpt4 key购买 nike

我有一个包含students 表的数据库设置。 students 表与其他几个表具有一对多关系。即一个学生可以有多个科目。而且一个学生可以有多个奖项。

代码如下:

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',
]
];
}

它工作正常,除了它只适用于单一关系; 一个学生到多个科目

最佳答案

我认为您的第一个定义看起来不对:$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 ']'
];

关于PHP 事件记录 : Multiple has many associations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31054188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com