-
yii 操作头信息及下载文件查看全部
-
在控制器类中使用public $layout='布局文件'属性来指定渲染用到的布局文件查看全部
-
use yii\helpers\HtmlPurifier HtmlPurifier::process()过滤script代码不现实查看全部
-
use yii\helpers\Html Html::encode()处理变量中的script代码查看全部
-
model/order.php: <?php namespace app\models; use Yii; use yii\base\Model; use yii\db\ActiveRecord; class Order extends ActiveRecord { //根据订单查询顾客信息 public function getCustomer(){ $customer = $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray()->all(); return $customer; } }查看全部
-
model/customer.php: <?php namespace app\models; use Yii; use yii\base\Model; use yii\db\ActiveRecord; class Customer extends ActiveRecord { //获取顾客的订单信息 public function getOrders(){ $orders = $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray()->one(); return $orders; } }查看全部
-
public function actionJoin(){ //关联查询 //根据顾客查询他的订单信息 $name = 'zhangsan'; $customer = Customer::find()->where(['name'=>$name])->one(); $orders = $customer->getOrders(); var_dump($orders); //根据订单查询顾客信息 $order = Order::find()->where(['id'=>$id])->one(); $customer = $order->getCustomer(); var_dump($customer); //关联查询结果缓存 $name = 'zhangsan'; $customer = Customer::find()->where(['name'=>$name])->one(); $orders = $customer->getOrders(); /* ..... 如果中间有订单更新操作,需要先释放第一次查询的缓存结果 */ unset($customer->orders); $orders_new = $customer->getOrders(); var_dump($orders); //关联查询的多次查询 $customer = Customer::find()->all(); //这样有100个订单就要循环查询101次 foreach($customer as $customer){ $orders = $customer->orders; } $customer = Customer::find()->with('orders')->all(); //变成select * from order where customer_id in (...) , 从而在循环中不用去查询 select * from order where customer_id = ... foreach($customer as $customer){ $orders = $customer->orders; } } }查看全部
-
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; use yii\web\Cookie; use app\models\Test; use app\models\Customer; use app\models\Order; class SqlController extends Controller {查看全部
-
model/test.php <?php namespace app\models; use Yii; use yii\base\Model; use yii\db\ActiveRecord; class Test extends ActiveRecord { public function rules(){ return [ ['id','integer'], ['username','string','length'=>[0,6]] ]; } }查看全部
-
public function actionDelete(){ $id = 123; //删除数据,将数据查询出来再删除 $result = Test::find()->where(['id'=>$id])->all(); $result[0]->delete(); //直接删除 Test::deleteAll('id>:id',array(":id"=>$id)); } public function actionCreate(){ //增加数据,对写入的数据进行验证 $id = 123; $username = 'create'; $test = new Test; $test->id = $id; $test->username = $username; //调用testmodel里的rules方法进行检验 $test->validate(); if($test->hasErrors()){ echo 'this is a error'; die(); } $test->save(); } public function actionUpdate(){ //修改数据 $username = 'update'; $test = Test::find()->where(['id'=>123])->one(); $test->username = $username; $test->validate(); if($test->hasErrors()){ echo 'this is a error'; die(); } $test->save(); }查看全部
-
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\web\Cookie; use app\models\Test; class SqlController extends Controller { public function actionSearch(){ $id = 123; //查询数据,这里如果$id= 1 or 1 = 1 会查出所有id信息 $sql = "select * from test where id = $id"; $result = Test::findBySql($sql)->all(); //为防止sql注入使用占位符进行数据库操作 $sql = 'select * from test where id=:id'; $result = Test::findBySql($sql,array(':id'=>$id))->all(); //AR函数 $result = Test::find()->where(['id'=>$id])->all(); $result = Test::find()->where(['>','id',0])->all(); $result = Test::find()->where(['between','id',1,2])->all(); $result = Test::find()->where(['like','title','title'])->all(); //将对象转成数组进行查询减少内存消耗 $result = Test::find()->where(['between','id',1,2])->asArray()->all(); //批量查询数据减少内存消耗 foreach(Test::find()->batch(2) as $tests){ var_dump($tests); } var_dump($result); }查看全部
-
关联查询: use yii\db\ActiveRecord; class Customer extends ActiveRecord{//帮助顾客获取订单 public function getOrders(){ $result=$this->hasMany(order::className(),['customer_id'=>'id'])->asArray()->all(); return $result; } } 关联查询: hasMany:一对多,hasOne:一对一 $customer->orders; $customer当没有orders属性时,$customer自动调用_get()方法,拼接调用getOrders()方法,并自动在后面加上all()方法或者one()方法,至于何时自动拼接all或者one,取决于关联查询是用的hasMany还是hasOne,如果是hasMany则拼接all,否则反之。 //关联查询 //根据顾客查询她/他的订单的信息 // $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); // $order = $customer->hasMany('app\models\Order',['customer_id'=>'id'])->asArray()->all(); // $orders = $customer->getOrders(); // $orders = $customer->orders; // print_r($orders); //根据订单查询顾客的信息 $order = Order::find()->where(['id'=>1])->one(); $customer = $order->customer; //以属性的方式获取数据 print_r($customer); 注:若使用以属性的方式获取数据,则在模型里面要定义一个方法,该方法要以get+属性的命名方式。查看全部
-
View/Index/index.php: <?php use yii\helpers\Html; use yii\helpers\HtmlPurifier; ?> <h1><?=Html::encode($str_javascript);?></h1><!--输出hello, world<script>alert(3)</script>--> <h1><?=HtmlPurifier::process($str_javascript);?></h1><!--输出hello, world--> <?php echo $this->render('about');?><!--在index.php视图中显示另一个视图about.php--> View/Index/about.php: <?php $this->blocks['block1']='this is about with render block.'?><!--定义数据块--> layouts/common.php: <!DOCTYPE html> <html lang='en'> <head> <meta charset ='utf-8'> <title>Document</title> </head> <body> <h1>hello common</h1> <?php if(isset($this->blocks['block1'])):?><!--显示数据块--> <?=$this->blocks['block1'];?> <?php else:?> <h1>hello common</h1> <?php endif;?> <?=$content;?> </body> </html>查看全部
-
controller/IndexController.php: public function actionRenderpartial(){ $hello_str = 'hello, world'; $test_arr = array(1,2,3); $str_javascript = 'hello, world<script>alert(3)</script>'; $data = array(); //将需要传递的数据放到数组中 $data['hello_str'] = $hello_str; $data['test_arr'] = $test_arr; $data['str_javascript'] = $str_javascript; return $this->renderPartial('index.php',$data); //view文件夹下需要建立一个同控制器名相同的文件夹 } public $layout = 'common'; //调用view/layouts布局文件夹下的common.php public function actionRender(){ return $this->render('about'); //将about中的数据放到$content中,并显示layout布局 }查看全部
-
//修改数据 $result = Test::find()->where(['id'=>4])->one(); $result->title="title4"; $result->save();查看全部
举报
0/150
提交
取消