在YII中保存多级模型的问题

时间:2010-06-13 09:22:52

标签: php yii yii-cmodel

用户及其地址详情的我的表格结构如下

CREATE TABLE tbl_users (
  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  loginname varchar(128) NOT NULL,
  enabled enum("True","False"),
  approved enum("True","False"),
  password varchar(128) NOT NULL,
  email varchar(128) NOT NULL,
  role_id int(20) NOT NULL DEFAULT '2',
  name varchar(70) NOT NULL,
  co_type enum("S/O","D/O","W/O") DEFAULT "S/O",
  co_name varchar(70),
  gender enum("MALE","FEMALE","OTHER") DEFAULT "MALE",
  dob date DEFAULT NULL,
  maritalstatus enum("SINGLE","MARRIED","DIVORCED","WIDOWER") DEFAULT "MARRIED",
  occupation varchar(100) DEFAULT NULL,
  occupationtype_id int(20) DEFAULT NULL,
  occupationindustry_id int(20) DEFAULT NULL,
  contact_id bigint(20) unsigned DEFAULT NULL,
  signupreason varchar(500),
  PRIMARY KEY (id),
  UNIQUE KEY loginname (loginname),
  UNIQUE KEY email (email),
  FOREIGN KEY (role_id) REFERENCES tbl_roles (id),
  FOREIGN KEY (occupationtype_id) REFERENCES tbl_occupationtypes (id),
  FOREIGN KEY (occupationindustry_id) REFERENCES tbl_occupationindustries (id),
  FOREIGN KEY (contact_id) REFERENCES tbl_contacts (id)
) ENGINE=InnoDB;

CREATE TABLE tbl_contacts (
  id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  contact_type enum("cres","pres","coff"),
  address varchar(300) DEFAULT NULL,
  landmark varchar(100) DEFAULT NULL,
  district_id int(11) DEFAULT NULL,
  city_id int(20) DEFAULT NULL,
  state_id int(20) DEFAULT NULL,
  pin_id bigint(20) unsigned DEFAULT NULL,
  area_id bigint(20) unsigned DEFAULT NULL,
  po_id bigint(20) unsigned DEFAULT NULL,
  phone1 varchar(20) DEFAULT NULL,
  phone2 varchar(20) DEFAULT NULL,
  mobile1 varchar(20) DEFAULT NULL,
  mobile2 varchar(20) DEFAULT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (district_id) REFERENCES tbl_districts (id),
  FOREIGN KEY (city_id) REFERENCES tbl_cities (id),
  FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;

CREATE TABLE tbl_states (
  id int(20) NOT NULL AUTO_INCREMENT,
  name varchar(70) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;


CREATE TABLE tbl_districts (
  id int(20) NOT NULL AUTO_INCREMENT,
  name varchar(70) DEFAULT NULL,
  state_id int(20) DEFAULT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;


CREATE TABLE tbl_cities (
  id int(20) NOT NULL AUTO_INCREMENT,
  name varchar(70) DEFAULT NULL,
  district_id int(20) DEFAULT NULL,
  state_id int(20) DEFAULT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (district_id) REFERENCES tbl_districts (id),
  FOREIGN KEY (state_id) REFERENCES tbl_states (id)
) ENGINE=InnoDB;

关系如下 用户有多个联系人,即永久地址,当前地址,办公室地址。 每个联系人都有州和城市。

用户 - >联系人 - >状态如下

如何一次性保存此结构的模型。

请尽快提供回复

2 个答案:

答案 0 :(得分:0)

我相信我在使用许多外键保存多级模型时遇到了类似的问题。它似乎不能轻易地“一气呵成”保存...... 看看my solution here

答案 1 :(得分:0)

您可以查看Google代码中的gii-templates扩展程序 -

gii templates on google code

- >>它可能记录得更好,但它会尝试做你需要的。 (如果你很好地实现它可能会节省一些时间,但我甚至考虑手工编码 如果我是你。)