Wednesday 11 October 2017

Yii2 Pjax Reload Grid view and stay on same page


Yii 2 Pjax -    In order to stay in same page when using Pjax reload() use the below code,

 $.pjax.reload({
container:'.grid-view',
url: $('.grid-view li.active a').attr('href')
});



Yii2 Query Builder Like operator


To use "LIKE" operator in Yii2 query builder, use the below code,

$sql = "SELECT *  FROM  TABLE_NAME  WHERE item_code = :item_code AND DATE_FORMAT(req_date, '%Y-%m') LIKE :period";
                $req_date = '%' . $req_date . '%';
                $command = Yii::$app->db->createCommand($sql);
                $command->bindParam('item_code', $item_code);
                $command->bindParam('period', $req_date);
                $result = $command->execute();

Yii2 Sum Column in Active Record



To get sum of column using active record,
$query = Item::find();
            $query->where(['item_code' => $item_code]);
            $cost = $query->sum('lot_qty');