当前位置: 首页 > 工具软件 > Hau-yii2 > 使用案例 >

yii2使用随记

冯枫
2023-12-01

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)

 类似资料: