2014/05/25-关联模型/HAS_ONE/HAS_MANY/BELONGS_TO
ClClass AdminRelationModel extends RelationModel { protected $tableName="admin"; protected $_link=array( 'userinfo'=>array( 'mapping_type' => HAS_ONE, //'class_name'=>'userinfo', 'mapping_name'=>'userinfo', 'foreign_key' => 'aid', 'mapping_fields'=>'qq,tel',//不设置输出所有字段 //'as_fields' =>'qq,tel:telephone,aid',//字段映射后save失效 ), 'discount'=>array( 'mapping_type'=>HAS_MANY, //'class_name'=>'discount', 'mapping_name'=>'discount', 'foreign_key'=>'did', 'mapping_fields'=>'name,number,did,id', 'mapping_limit'=>'3',//输出条数限制 'mapping_order'=>'id ASC',//排序设置 ), 'role'=>array( 'mapping_type'=>BELONGS_TO, 'mapping_name'=>'role', 'foreign_key'=>'usertype',//admin表中的usertype字段 'as_fields' =>'name:role_name',//字段映射 ), );}
$user=D('AdminRelation'); $userinfo=$user->relation(true)->select(); $data['id']='2';//有ID数据无需where $data['realname']='youyoushanyu';//修改主表数据时,save()返回1. $data['userinfo']=array('qq'=>'910099271');//HAS_ONE数据 $data['discount']=array( array('id'=>'1','number'=>'1010575687'),//HAS_MANY数据 ); if($status=$user->relation(true)->where('id=2')->save($data)){ p($status);echo '成功!'; }else{ p($status);echo '失败'; }