标签:
yii框架
指定控制器
?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; } }
标签:
原文地址:http://www.cnblogs.com/huen/p/4852160.html