2014-07-21 1 views
0

중요한 것을 놓치고 있어야하지만이 것을 분류 할 수는 없습니다 나는 많은 사람의 주소를 연관시키고 싶습니다. 내 거주지 : 도와 줄 사람이 있습니까?테이블을 PHP 활성 레코드와 연결하는 방법을 알아낼 수 없습니다

CREATE TABLE `testpersons` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` varchar(20) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; 

CREATE TABLE `testaddresses` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` varchar(20) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; 

CREATE TABLE `testassociate` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `testuser_id` int(11) NOT NULL, 
    `testgroup_id` int(11) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ; 

는 그럼 난 내 개체를 만들 :

class Testperson extends ActiveRecord\Model { 
    static $table_name = 'testpersons'; 
    static $has_many = array(
     array('testaddress', 
      'through'=>'test_associate', 
      'foreign_key'=>'testgroup_id', 
      'primary_key'=>'testuser_id', 
      'class_name'=>'Testaddress') 
    ); 
} 

class Testaddress extends ActiveRecord\Model { 
    static $table_name = 'addresses'; 
    static $belongs_to = array(
     array('testperson', 
      'through'=>'test_associate', 
      'foreign_key'=>'testuser_id', 
      'primary_key'=>'testgroup_id') 
     ); 
} 

그런 다음 내 결과를 얻으려고 노력 : = Testperson는 :: (2) 찾을 수 $의 사람; echo var_dump ($ person);

object(Testperson)[16] 
    public 'errors' => null 
    private 'attributes' (ActiveRecord\Model) => 
    array (size=2) 
    'id' => int 2 
    'name' => string 'tata' (length=4) 
    private '__dirty' (ActiveRecord\Model) => 
    array (size=0) 
    empty 
    private '__readonly' (ActiveRecord\Model) => boolean false 
private '__relationships' (ActiveRecord\Model) => 
array (size=0) 
    empty 
private '__new_record' (ActiveRecord\Model) => boolean false 

누군가가 내 협회 뭐가 잘못 말해 수 :

는 것을 준다? 많은 thx

답변

1

AddressPerson에 여러 개 연결하려는 경우 DB 구조가 잘못되어 불필요하게 복잡합니다.

CREATE TABLE `testpeople` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` varchar(20) NOT NULL, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `testaddresses` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `testperson_id` int(11) NOT NULL, 
    `name` varchar(20) NOT NULL, 
    PRIMARY KEY (`id`) 
); 

이제 모델은 다음과 같이 될 것이다 :
은 당신이 원하는 것은 이것이

class Testaddress extends ActiveRecord\Model { 
    static $belongs_to = array(
     array('testperson') 
    ); 
} 

class Testperson extends ActiveRecord\Model { 
    static $table_name = 'testpeople'; 

    static $has_many = array(
     array('testaddress') 
    ); 
} 

충분합니다 그.

그러나 through을 사용하려면 해당 관계가있는 중간 테이블의 모델도 있어야합니다.
예를 들어, db 구조를 사용하겠습니다. 그 $person->testaddresses와 같은 일부 관계를 요청할 때 채워집니다 캐시 변수가 있기 때문에 빈 __relathionships에 대해서는

class Testperson extends ActiveRecord\Model { 
    static $table_name = 'testpeople'; 

    static $has_many = array(
     array('testassociate', 'foreign_key' => 'testuser_id'), 
     array('testaddress', 'through' => 'testassociate', 'foreign_key' => 'testgroup_id') 
    ); 
} 

class Testassociate extends ActiveRecord\Model { 
    static $belongs_to = array(
     array('testperson', 'foreign_key' => 'testuser_id'), 
     array('testaddress', 'foreign_key' => 'testgroup_id'), 
    ); 
} 

class Testaddress extends ActiveRecord\Model { 
    static $has_one = array(
     array('testassociate', 'foreign_key' => 'testgroup_id'), 
     array('testaddress', 'through' => 'testassociate', 'foreign_key' => 'testuser_id'), 
    ); 
} 

, 그것은이다.