2015-06-25 61 views
1

我有一個帶有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

+0

可能會有所幫助http://www.phpactiverecord.org/docs/ActiveRecord/HasMany – jamesmstone

回答

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 ']'   
]; 
+1

的提示是我錯誤:你得到一個數組的地方,它期望一個字符串,e ..g。第一個成員有望成爲「科目」,而不是數組:) – Nanne