关于关联模型插入找不到错在哪?

浏览:661 发布日期:2013/05/25 分类:求助交流
控制器
if(!empty($_POST['sub'])){

$user = D("Article");


if(empty($_POST['addtime'])){
$_POST['addtime']=time();
}
//写入父表的数据
$data=array();
$data["oxs_title"] = $_POST['title'];
$data["oxs_intro"] = $_POST['intro'];
$data["oxs_top"] = $_POST['top'];
$data["oxs_index"] = $_POST['index'];
$data["oxs_addtime"] = $_POST['addtime'];
$data["oxs_articlezt"] = $_POST['articlezt'];


//写入子表的数据
$data["Articles"] = array(
'oxs_copyfrom' =>$_POST['copyfrom'],
'oxs_inputer' =>$_POST['inputer'],




'oxs_keyword' =>$_POST['keyword'],
'oxs_hits' =>$_POST['hits'],
'oxs_postnum' =>$_POST['postnum'],
'oxs_content' =>$_POST['content'],
);
$result = $user->relation(true)->add($data);

if($result){
echo "yes";
}else{

echo "no";
echo $user->getLastSql();


}

------------------------------------------------------------------------------

模型1

<?php
class ArticleModel extends RelationModel{
//文章表

//字段映射
protected $_map = array(
'title' =>'oxs_title', //标题
'intro' =>'oxs_intro', //文章概要
'top' =>'oxs_top', //是否置顶
'index' =>'oxs_index', //是否首页显示
'addtime' =>'oxs_addtime', //添加时间
'articlezt' =>'oxs_articlezt', //文章状态

);

//字段关联
protected $_link = array(
'Articles'=> array( //子关联的表
'mapping_type' =>HAS_ONE,

'foreign_key'=>'oxs_id',//关联ID 关联的外键名称
'class_name'=>'Articles',//子表 关联的映射名称,用于获取数据用 控制器用turn
//'mapping_name'=>'Articles',//子表 要关联的模型类名
//'mapping_fields '=>'oxs_copyfrom,oxs_inputer' 关联要查询的字段
//'as_fields'=>'id:zid', //一对一才能使用 直接把关联的字段值映射成数据对象中的某个字段
),
);

}

?>-----------模型2-----------------
<?php
class ArticlesModel extends RelationModel{
//文章具体表
protected $_map = array(
'copyfrom' =>'oxs_copyfrom', //来源
'inputer' =>'oxs_inputer', //写入者
'keyword' =>'oxs_keyword', //关键字
'hits' =>'oxs_hits', //点击率
'postnum' =>'oxs_postnum', //评论数
'content' =>'oxs_content', //文章内容
);

//关联模型
protected $_link = array(
'Article'=>array(
'mapping_type' =>BELONGS_TO,
'foreign_key' =>'uid',

),
);

}

?>
最佳答案
评论( 相关
后面还有条评论,点击查看>>