thinkphp-关联模型上
一对一:HAS_ONE
控制器
<?php
namespace Home\Controller;
use Think\Controller;
class GlmxingController extends Controller {
public function index(){
$user=D('Sks');
$arr=$user->relation(true)->select();
echo '<pre>';
var_dump($arr);
echo '</pre>';
}
}
?>
模型
<?php
namespace Home\Model;
use Thimk\Model;
use Think\Model\RelationModel;
class GlmxingModel extends RelationModel{
protected $_link = array(
'Sfz' => array(
BELONGS_TO //一对一,但一里还有很多一:
'mapping_type' => self::HAS_ONE, //设置关联模式,如一对一
'class_name' => 'Sfz', //要关联的模型类名,没写默认上面的
'foreign_key' => 'uid', //关联的外键名称,自动对应id
'as_fields' => 'Sfz', //关联的字段值映射成数据同级字段
),
);
}
?>