Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';我不知道如何将表与php活动记录关联_Php_Activerecord_Phpactiverecord - Fatal编程技术网

Can';我不知道如何将表与php活动记录关联

Can';我不知道如何将表与php活动记录关联,php,activerecord,phpactiverecord,Php,Activerecord,Phpactiverecord,我一定错过了一些重要的事情,但我无法解决这件事 我想将许多地址关联到一个人,以便。。。这是我的解决方案: 有人帮忙吗 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 TA

我一定错过了一些重要的事情,但我无法解决这件事 我想将许多地址关联到一个人,以便。。。这是我的解决方案: 有人帮忙吗

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')
        );
}
然后试图得到我的结果: $person=Testperson::find(2); echo var_dump(人)

提出:

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如果您只想将多个
地址
关联到一个
,那么您的数据库结构是错误的,不必要地复杂。
你想要的是:

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')
    );
}
这应该足够了

但是,当您想使用
时,您还应该有一个具有相应关系的中间表模型。
例如,我将使用您的db结构

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'),
    );
}
关于空的
\uu relaships
,这是因为这是一个缓存变量,当您请求一些关系时,将填充该变量,例如
$person->testaddress