yii2 model->curd

2022-03-25 20:39:12 浏览数 (1)

代码语言:javascript复制
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: 2022-02-28 10:09:39
-- 服务器版本: 10.1.13-MariaDB
-- PHP Version: 5.6.21

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = " 00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `ssss`
--

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

--
-- 表的结构 `goods`
--

CREATE TABLE `goods` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- 转存表中的数据 `goods`
--

INSERT INTO `goods` (`id`, `name`) VALUES
(1, '11111'),
(2, '22222'),
(3, '333'),
(4, '444'),
(5, '555');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `goods`
--
ALTER TABLE `goods`
  ADD PRIMARY KEY (`id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `goods`
--
ALTER TABLE `goods`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

gii生成Goods放在commonmodels下。 增加

代码语言:javascript复制
<?php
namespace frontendcontrollers;
use Yii;
use yiiwebController;
use commonmodelsGoods;
class UserController extends Controller
{
	public function actionIndex()
	{
			$user= new Goods;         
$user->name ="cyg";  
$user->save();
	}
}

修改:

代码语言:javascript复制
<?php
namespace frontendcontrollers;
use Yii;
use yiiwebController;
use commonmodelsGoods;
class UserController extends Controller
{
	public function actionIndex()
	{
		$user = Goods::find()->where(['id'=>6])->one(); //获取name等于test的模型
$user->name = "liwen"; //修改age属性值
$user->save();   //保存
	}
}

删除:

代码语言:javascript复制
<?php
namespace frontendcontrollers;
use Yii;
use yiiwebController;
use commonmodelsGoods;
class UserController extends Controller
{
	public function actionIndex()
	{
		$user = Goods::find()->where(['id'=>6])->one(); 
		$user->delete();
	}
}

0 人点赞