thinkphp(1)关联模型学习

来源:互联网 发布:git server windows 编辑:程序博客网 时间:2024/06/08 07:05

确定主表后,创建模型,设置对应关系,对应子表以及外键后,在控制器实例化。使用relation进行操作。

HAS_MANY 为一对多

WscSortModel.class.php

<?php


namespace Addons\WscSort\Model;
use Think\Model;


/**
 * WscSort模型
 */
class WscSortModel extends Model\RelationModel{
public $_link = array(
'goods' => array(
'mapping_type' => self::HAS_MANY, //设置一对关系
'class_name' => 'WscGoods', //WscGoods为对应子表
'foreign_key' => 'sort' //关联的外键
)
);
}

ApiController.class.php

<?php


namespace Addons\WscSort\Controller;


use Think\ApiBaseController;


class ApiController extends ApiBaseController {
function test(){
$m =D('WscSort');
$data = $m->relation('goods')->select();
echo "<pre>";
print_r($data);
echo "</pre>";
}

}
?>

原创粉丝点击