創建Model模型
把"Home/Model"文件夾剪切到Application文件夾下,讓Home和Admin共同使用。
第一種實例化模型的方法
第二種實例化模型的方法
第三種實例化模型的方法
在tp3.2以后,M()和D()是一樣的,在3.2之前版本中
M()類似於是 new Model();
D('goods') 類似於 new GoodsModel():
提示:我們可以看到goods表的信息,在GoodsModel中沒有寫代碼,所有的業務邏輯都是Model類來實現的。
對表的操作
增加:M('表名')->add($data);
Insert into 表() values(),()
刪除:M('表名')->delete($id);
Delete from 表 where
更新:M('表名')->save($data);
Update 表 set 字段=值,字段=值 where
查詢:M('表名')->select();
Select * from 表 where group by having order by limit
普通查詢(顯示所有的商品)
1、GoodsController中的代碼如下:
2、在模板中循環取出goods/showlist.html
3、獲取一條記錄
條件查詢
having和where的區別
區別一:
where后面跟的字段必須在數據表中有
having后面跟的字段必須在數據集中有
區別二:
select * from 表 where 條件 group by 分組 having 條件
動態查詢
getby+字段名
聚合函數
數據添加
1、通過關聯數組的方法添加
2、通過AR(active record)方式來添加
數據修改
1、通過關聯數組的方法
2、通過AR(active record)方式
數據刪除
直接執行SQL語句
修改模板的鏈接
showlist.html
add.html