php第五章
2015-10-03 22:00
555 查看
yii框架
指定控制器
?r=hello/index表示:index.php 跳转到HelloController控制器中actionIndex函数
例子:
HelloController.php
FirstTest.php
Orders.php
Customer.php
指定控制器
?r=hello/index表示:index.php 跳转到HelloController控制器中actionIndex函数
例子:
http://localhost:63342/htdocs/yii_basic/web/index.php?r=hello/query
HelloController.php
<?php /** * Created by PhpStorm. * User: forr * Date: 15/10/2 * Time: 12:02 */ namespace app\controllers; use yii\web\Controller; use app\models\FirstTest; class HelloController extends Controller { public function actionIndex() { $request = \YII::$app->request; echo $request->get('id'); // echo 'hello world 23333333'; //跳转 // $this->redirect('https://www.baidu.com/'); //下载文件 // $response = \YII::$app->response; // $response->sendFile('./favicon.ico');//路径web/favicon.ico } //查询数据 public function actionQuery() { echo 'my db'; $sql = 'select * from first_test where id = :id'; $list = FirstTest::findBySql($sql,array(':id'=>1))->all(); print_r($list).'<br/>'; //批量查询每次查询2条记录 $sql = 'select * from first_test'; foreach(FirstTest::find()->batch(2) as $test) { echo count($test); } } //删除数据 public function actionDelete() { // $sql = 'select * from first_test where id = :id'; // $list = FirstTest::findBySql($sql,array(':id'=>4))->all(); // $list[0]->delete(); //或 FirstTest::deleteAll('id = :id',array(':id'=>3)); } //添加数据 public function actionAdd() { $test1 = new FirstTest(); $test1->name = '张杰'; $test1->title = '逆战'; $test1->age = 32; //启动校验规则 $test1->validate(); if($test1->hasErrors()) { echo '名称过长,不符合规则'; die; } $test1->save(); } }
FirstTest.php
<?php /** * Created by PhpStorm. * User: forr * Date: 15/10/2 * Time: 16:40 */ namespace app\models; use yii\db\ActiveRecord; class FirstTest extends ActiveRecord{ public function rules() { return [ // 有个有效的名字 ['name', 'string','length'=>[0,10]], ]; } }
Orders.php
<?php /** * Created by PhpStorm. * User: forr * Date: 15/10/2 * Time: 16:40 */ namespace app\models; use yii\db\ActiveRecord; class Orders extends ActiveRecord{ public function getCustomer() { $customer = $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray(); return $customer; } }
Customer.php
<?php /** * Created by PhpStorm. * User: forr * Date: 15/10/2 * Time: 16:40 */ namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public function getOrders() { $order = $this->hasMany(Orders::className(),['customer_id'=>'id'])->asArray(); return $order; } }
相关文章推荐
- php第四章
- php第三章
- php第二章
- php第一章
- php设计模式之观察者模式
- PHP 正则表达式匹配 preg_match 与 preg_match_all 函数
- php连接mysql
- 学习ThinkPHP3.2.2:video10,定制错误页面
- 学习ThinkPHP3.2.2:video10,设置模板读取控制器中某个操作的url
- 学习ThinkPHP3.2.2:video10,可以通过设置 TMPL_FILE_DEPR 简化模板文件目录层次
- 学习ThinkPHP3.2.2:video9,function的作用范围
- 学习ThinkPHP3.2.2:video9,对3.1的分组与3.2.2的模块的理解
- Zend Studio错误总结
- PHP excel读取excel文件转换为数组
- 学习ThinkPHP3.2.2:video9,用“C”函数读取配置文件内容
- 学习ThinkPHP3.2.2:video9,配置文件存放位置的变化
- PHP递归实现无限级分类
- PHP 学习2
- php PDO面向对象的方式连接数据库
- php正则选择某个值的标签内容