yii2使用隨記

1.時間範圍判斷


$machines = Machine::find()
->select('id,name')
->andFilterWhere(['between', 'timestamp', $start_date, $end_date])
->asArray()
->all();


2.時間戳與日期轉換


$timestamp = strtotime($datetime);
$datetime = date('Y-m-d H:i:s', $timestamp);


3.使用sql語句


$db = Yii::$app->db;
$sql = '...';
$result = $db->createCommand($sql)->bindValues(array(
':driver_id' => $driver->id,...
))->queryOne();


4.分頁


(1)nextpage由服務端返回第幾頁




if (!isset($nextpage) || empty($nextpage)) {
$nextpage = 0;
}


if ($nextpage < 0) {
return;
}




$page = 5;




$machines = Machine::find()
->offset($nextpage * $page)
->limit($page)
->all();




$total_count = ...




if (本次查詢個數 + $nextpage * $page < $total_count) {
echo json_encode($this->success(array('machines' => $result, 'nextpage' => $nextpage + 1)));


} else {
echo json_encode($this->success(array('machines' => $result, 'nextpage' => '')));
}
(2)nextpage由服務端返回自定義時間戳,用於訂單按日期排序並統計


5.排序
->orderBy(['farmer_comment_timestamp' => SORT_DESC])


6.isset,empty,is_null驗證數據


7.輸入的數據校驗


8.不能爲空判斷
->andWhere('farmer_comment != :farmer_comment', [':farmer_comment' => ''])


9.圖片url
$url = Yii::$app->urlManager->getHostInfo() . Yii::$app->urlManager->getBaseUrl() . '/' . $farmer['avatar'];


10.參數判斷
if (!isset($farmer_id) || empty($farmer_id)) {
echo json_encode($this->fail('', 0, '農民編號不能爲空'));
return;
}
11.編碼
header("Content-type:json/application;charset=utf-8");


12.字符串截取
$region_id1 = substr($region_id, 0, 2);


13.數字截取
$region_id1 * pow(10, 10)


14.字段求和
$machine_order->getField()->sum('client_area');


15.調用存儲過程
$temp = Yii::$app->db->createCommand('call region_count_proc(:machine_order_id,:driver_id)')
->bindValues([':machine_order_id' => $machine_order_id, ':driver_id' => $machine_order->driver_id])
->queryOne();


16.時間範圍判斷
$dates = Schedule::find()
->select(['date', 'status'])
->where(['machine_id' => $machine_id])
->andWhere('date >= :start_date', [':start_date' => $start_date])
->andWhere('date <= :end_date', [':end_date' => $end_date])
->orderBy('date')
->all();


17.多表查詢後條件
$works = $machine
->getWorks()
->select('id,farmer_id,farmer_comment,farmer_star,farmer_comment_timestamp')
->where(['status' => Work::STATUS_FINISH])
->andWhere('farmer_comment != :farmer_comment', [':farmer_comment' => ''])
->offset(0)
->limit(2)
->orderBy(['farmer_comment_timestamp' => SORT_DESC])
->asArray()
->all();


18.根據兩點間的經緯度計算距離
/**
     * @desc 根據兩點間的經緯度計算距離
     * @param float $lat 緯度值
     * @param float $lng 經度值
     */
    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters


        /*
        Convert these degrees to radians
        to work with the formula
        */


        $lat1 = ($lat1 * pi()) / 180;
        $lng1 = ($lng1 * pi()) / 180;


        $lat2 = ($lat2 * pi()) / 180;
        $lng2 = ($lng2 * pi()) / 180;


        /*
        Using the
        Haversine formula


        http://en.wikipedia.org/wiki/Haversine_formula


        calculate the distance
        */


        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;


        return round($calculatedDistance);
    }


19.默認路由 
'defaultRoute' => 'admin'


20.默認動作
public $defaultAction = 'index'




21.百度推送 
本地使用php5.6沒問題,服務端使用php5.4報錯SDK params invalid, check the param
跟蹤源代碼發現問題。
php5.4中pushMsgToSingleDevice($channel_id , $message, $opts);channel_id必須是字符串
調用第三方包的時候php中注意數據類型




22.通過model向數據庫中保存數據時,注意數據類型,因爲model的屬性都是有類型,類型不對數據不能保存成功
$field->obstacle_type = strval($obstacle_type_ids);


23.implode,explode第二個參數是字符串不能爲數字


24.array轉string


implode(array,'字符串分割符')


25.string轉array


explode('字符串分割符',string)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章