PHP Active Record:Multiple有很多关联

时间:2015-06-25 15:10:12

标签: php mysql database phpactiverecord

我有一个带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',
            ]
        ];
    }

它工作正常,但它只适用于单一关系; one student to many subjects

1 个答案:

答案 0 :(得分:1)

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