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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艺菲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值