기본 키 email_address가 채워지지 않으므로 우편 목록을 추가하기 위해 Contact :: sync (array (1,2,3))를 사용하려는 몇 가지 모델이 실패한 것으로 가정합니다.문자열 기반 기본 키가있는 Many to Many Laravel 4
테이블 구조 :
Contacts
- email_address string (PK)
- name string
MailingLists
- id integer (PK)
- name string
ContactMailingLists
- contact_id string (PK, FK)
- mailing_list_id integer (PK, FK)
및 모델 :
<?php
class MailingList extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'mailing_lists';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function contacts()
{
return $this->belongsToMany('\Application\Entity\Contact', null, 'id', 'email_address')->withTimestamps();
}
}
class Contact extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'contacts';
/**
* @var string
*/
protected $primaryKey = 'email_address';
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function mailing_lists()
{
return $this->belongsToMany('\Application\Entity\MailingList', null, 'id', 'mailing_list_id')->withTimestamps();
}
}
사용하여 -> 동기화() 또는 웅변 다른 함수 관계가 오류를 생성한다. 삽입시 email_address가 email_address 대신 0으로 설정되므로 sync() 메소드는 오류를 생성합니다.
편집 : 코드에 관련된 키를 추가했습니다. pivot table
이름과 함께 관계를 선언 할 때
무엇이 오류입니까? – Shawn
@Shawn, 나는 내 앞에 정확한 오류가 없지만 피칭 테이블에 삽입하면 $ incrementing 설정으로 인해 email_address가 0으로 설정되어 있기 때문에 외래 키 제약 조건 오류가 발생합니다. – Voziv