-
關(guān)聯(lián)查詢: use yii\db\ActiveRecord; class Customer extends ActiveRecord{//幫助顧客獲取訂單 public function getOrders(){ $result=$this->hasMany(order::className(),['customer_id'=>'id'])->asArray()->all(); return $result; } } 關(guān)聯(lián)查詢: hasMany:一對多,hasOne:一對一 $customer->orders; $customer當沒有orders屬性時,$customer自動調(diào)用_get()方法,拼接調(diào)用getOrders()方法,并自動在后面加上all()方法或者one()方法,至于何時自動拼接all或者one,取決于關(guān)聯(lián)查詢是用的hasMany還是hasOne,如果是hasMany則拼接all,否則反之。 //關(guān)聯(lián)查詢 //根據(jù)顧客查詢她/他的訂單的信息 // $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); //根據(jù)訂單查詢顧客的信息 $order = Order::find()->where(['id'=>1])->one(); $customer = $order->customer; //以屬性的方式獲取數(shù)據(jù) print_r($customer); 注:若使用以屬性的方式獲取數(shù)據(jù),則在模型里面要定義一個方法,該方法要以get+屬性的命名方式。查看全部
-
//修改數(shù)據(jù) $result = Test::find()->where(['id'=>4])->one(); $result->title="title4"; $result->save();查看全部
-
添加數(shù)據(jù): helloController.php文件: $test = new Test; $test->id = 3; $test->title = 'title3'; $test->validate();//驗證器,驗證字段是否合法 if($test->hasErrors()){ echo 'data is error';//字段不合法 die; } $test->save(); 如果想要驗證,在模型test.php文件中有對應的方法rules進行驗證 test.php: public function rules(){ return [ ['id','integer'],['title','string','length'=>[0,5]]]; }查看全部
-
//數(shù)據(jù)模型之單表刪除 //刪除數(shù)據(jù),先取出要刪除的數(shù)據(jù) /*$results = Test::find()->where(['id'=>1])->all(); $results[0]->delete();//調(diào)用delete()方法就可以刪除第一條數(shù)據(jù)*/ //刪除數(shù)據(jù)有個更快捷的方式:調(diào)用控制器當中的deleteAll()方法把整個表里的數(shù)據(jù)刪掉;同時這個方法里也可以帶上查詢條件指定刪除哪部分的數(shù)據(jù)。 //Test::deleteAll('id>0'); Test::deleteAll('id>:id',array(':id'=>0));//deleteAll也支持占位符的功能查看全部
-
1.ActiveRecord(活動記錄類)<br> (1)方法:findBySql()繼承之父類的,結(jié)果返回一個對象(放在數(shù)組當中)<br> 2.SQL入侵<br> (1)占位符:(:id),加載Sql語句后面<br> (2)在findBySql($sql,array())數(shù)組中賦值。由于array()會把用戶傳遞過來的值作為一個整體去處理,<br> (3)findBySql防止SQL注入<br> // $sql='select * from Test where id=:id ';<br> // $result=Test::findBySql($sql,array(':id'=>2))->asArray()->all();//findBySql第二個參數(shù)設置占位符<br> <br> $sql='select * from Test where id=:id ';<br> $result=Test::find($sql)->where(['id'=>3])->asArray()->all();//findBySql第二個參數(shù)設置占位符<br> p($result); //id>0 /*$results = Test::find()->where(['>','id',0])->all(); print_r($results);*/ //id>=1 and id<=2 //$results = Test::find()->where(['between','id',1,2])->all(); //echo count($results); //title like '%title%' //$results=Test::find()->where(['like','title','title'])->all(); //print_r($results); //將查詢結(jié)果轉(zhuǎn)換成數(shù)組 // $results=Test::find()->where(['like','title','title'])->asArray()->all(); //批量查詢 foreach ($Test::find()->batch(2) as $value) { # code... } print_r($results);查看全部
-
$layout = common ; //布局文件 $this 視圖組件 如果想替換公共文件中的某段(數(shù)據(jù)塊),可以在視圖文件中使用: <?php $this->beginBlock('block1'); ?> <h1>....</h1> <?php $this->endBlock();?> 公共文件中調(diào)用 <?=$this->blocks['block1'];?> 判斷顯示數(shù)據(jù)塊有木有,然后在顯示 <?php if(isset($this->blocks['block1'])):?> <?=$this->blocks['block1'];?> <?php else: ?> <h1>hello Common </h1> <?php endif; ?>查看全部
-
1.在一個視圖(index.php)中顯示另一個試圖(about.php):在視圖index.php文件中使用<?=php echo $this->render('about');?>顯示about視圖; 2.注意:這時候Controller調(diào)用的是renderpartical() 3.當需要給(about.php)頁面?zhèn)魅雲(yún)?shù)時,用render的第二個參數(shù):$this->render('about',array('key'=>'value')),這樣就可以把第二個參數(shù)數(shù)組傳遞給about.php這個視圖中 4.(about.php)頁面如何使用(index.php)頁面?zhèn)鬟f過來的數(shù)據(jù)? (1)通過數(shù)組下邊(key)直接可以訪問 (2)語句:<?=$key;?>查看全部
-
public $layout = 'common';//通過$layout屬性去告訴render方法去顯示common布局文件 //視圖之布局文件 //把布局文件common.php和視圖文件home.php、about.php用render()方法進行拼合,才 能達到之前視圖的顯示效果。 //rensder()方法在顯示視圖文件的時候會做2件事:第1件事是會把視圖里的內(nèi)容放到$content這個變量中;第2件事是render()方法會把布局文件給顯示出來。 return $this->render('about');查看全部
-
<?php use yii\helpers\Html; use yii\helpers\HtmlPurifier; ?> <P><?=Html::encode($view_hello_str) ?> </P>//可以原樣顯示<script></script>代碼 <P><?=HtmlPurifier::process($view_hello_str) ?> </P>//可以過濾掉<script></script>代碼查看全部
-
1.控制器和視圖之間傳遞數(shù)據(jù): (1)定義一個變量$hello="Hello World!"; (2)聲明一個數(shù)組$date=array(); (3)這里使用了關(guān)聯(lián)數(shù)組,把這個字符串變量放到數(shù)組$date中 (4)$date['view_hello_str']=$hello; (5)return $this->renderPartial(“index”,$date); 2.在視圖中如何使用數(shù)據(jù)(也就是控制器傳遞過來的數(shù)組) (1)只需在模板中調(diào)用$data的key值(也就是數(shù)組下標),即可獲得對應的value值 (2)<?=$鍵名 ?> //?= 之間不能有空格查看全部
-
1.Controller和View關(guān)聯(lián) (1)控制器中: 調(diào)用一個renderpartial()方法,這個方法是基類Controller中的方法 這里使用$this這個關(guān)鍵字調(diào)用。 (2)renderpartial(“parm”),參數(shù)表示要顯示那個視圖文件,當然yii還規(guī)定了要顯示視圖,必須在前面加一個return語句: 完整語句:return $this->renderpartial(“parm”); (3)別的都叫方法,yii中就得叫動作。查看全部
-
//server到客戶端 $cookie = Yii::$app->response->cookies; //獲取cookie $cookie_data = array('name'=>'user', 'value'=>'slcheng'); $cookie->add(new yii/web/Cookie($cookie_data)); //設置cookie數(shù)據(jù) $cookie->remove('user'); //刪除cookie數(shù)據(jù) //從客戶端取得cookie $cookie = Yii::$app->request->cookies; //獲得瀏覽器請求的數(shù)據(jù)查看全部
-
session.save_path session 保存的路徑,在php.ini中設置<br> session->open();開啟session<br> $session->set();創(chuàng)建seesion數(shù)據(jù)<br> $session->get()取出session數(shù)據(jù)<br> $session->remove()刪除session數(shù)據(jù)<br> /可以通過數(shù)組方式進行操作session<br> $session[] = ''<br> unset($sission[])//刪除<br> //兩張方式,一種是對象方式處理,另一種是數(shù)組方式查看全部
-
相應處理: $res = \YII::response; 更改狀態(tài)碼:$res->statusCode = "404"; 添加header: $res->header->add("pragma", "no-cache"); 修改header: $res->header->set("pragma", "max-age=5"); 刪除header: $res->header->remove("pragma"); 跳轉(zhuǎn): $res->header->add("location", "http://www.baidu.com"); 重定向:$this->redirect("http://www.baidu.com", "302"); 文件下載:$res->header->add("content-disposition", "attachment; filename="a.jpg"); $res->sendFile("./robots.txt");查看全部
-
namespace是用來區(qū)分不同位置的相同類名,use關(guān)鍵字用來定義重復的位置,as用來進行修改相同類名,直接\是用來引用頂層空間的文件查看全部
舉報
0/150
提交
取消