简体   繁体   English

PHP Active Record:多个具有许多关联

[英]PHP Active Record: Multiple has many associations

I have a database setup with a students table. 我有一个带有students表的数据库设置。 The students table has a one to many relationship with a few other tables. students表与其他一些表具有一对多的关系。 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 . 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. 我认为您的第一个定义看起来是错误的: $has_many应该是一个数组数组,而不是数组数组。 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 ']'        
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM