I have bootstrap modal with gridview in it. Pagination for dataProvider is set to 5. The problem is that I have only 1 row on the first page of my gridview, other pages seems to work good.
Here's gridview code
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $operations,
'filterModel'=>$fModel,
'columns' => [
'operation_code',
'amount',
[
'attribute'=>'type',
'content'=>function($model){
return ClientMoneyOperation::itemAlias('type_color',$model->type);
},
'filter'=>ClientMoneyOperation::itemAlias('type'),
],
[
'header'=>'Бонус',
'content'=>function($model){
return $model->bonus->amount;
}
],
[
'header'=>'Директорський бонус',
'content'=>function($model){
return $model->directorbonus;
}
],
'comment',
[
'attribute'=>'created_at',
'format'=>'datetime',
'filter'=>false],
]
]) ?>
<?php Pjax::end(); ?>
and search function
public function
search($params,$city_id=null,$client_id=null,$page_size=20,$group =
false,$code=null,$date_from = null){
if($city_id){
$query = ClientMoneyOperation::find()->joinWith(['user.place'])-
>where(['place.city_id'=>$city_id]);
} else {
$query = ClientMoneyOperation::find();
}
$query->joinWith(['client'])->joinWith(['bonus']);
if($group){
$query->groupBy('operation_code');
}
$query->orderBy('created_at DESC');
if($code){
$query->where(['operation_code'=>$code]);
}
$dataProvider = new ActiveDataProvider([
'query'=>$query,
'sort'=>['attributes'=>['created_at'=>SORT_DESC]],
'pagination'=>[
'pageSize'=>$page_size
],
]);
$this->load($params);
if(!$this->validate()){
return $dataProvider;
}
$query->andFilterWhere([
'client_money_operation.type'=>$this->type,
'client_money_operation.status'=>$this->status,
]);
if($client_id){
$query->andFilterWhere([
'client_money_operation.client_id'=>$client_id,
]);
}
$query->andFilterWhere(['like','client_money_operation.comment',$this->comment])
->andFilterWhere(['like','client.card_code',$this->card_code])
->andFilterWhere(['like','client_bonus_operation.amount',$this->bonus_amount])
->andFilterWhere(['like','client_money_operation.operation_code',$this->operation_code]);
if(!empty($this->created_at_range) && strpos($this->created_at_range, '-') !== false){
list($start_date, $end_date) = explode(' - ', $this->created_at_range);
$query->andFilterWhere(['between', 'client_money_operation.created_at', strtotime(date("Y-m-d",strtotime($start_date))), strtotime(date("Y-m-d",strtotime($end_date)+86400))]);
} else {
$query->andFilterWhere(['between', 'client_money_operation.created_at', $date_from != null ? 1 : strtotime(date("Y-m-d",time())), strtotime(date("Y-m-d",time() + 86400))]);
}
return $dataProvider;
}
other gridviews works ok with the same dataprovider. PageSize 5 is setted in controller, where I am calling function search()
Try with this two lines before the return:
$countQuery = clone ($query);
$dataProvider->totalCount = $countQuery->count();
return...
Related
When I am trying to insert new column using codeigniter version 4 with dbforge.
I got an error in Production mode:
Whoops!We seem to have hit a snag. Please try again later...
public function addLanguage()
{
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$this->dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}
What am I doing wrong in this code? Any potential help would be greatly appreciated!
Development mode error image below:
You can use this code
public function addLanguage()
{
$dbforge = \Config\Database::forge();
$language = preg_replace('/[^a-zA-Z0-9_]/', '', $this->request->getPost('language',FILTER_SANITIZE_STRING));
$language = strtolower($language);
if (!empty($language)) {
if (!$this->db->fieldExists($language, "language")) {
$dbforge->addColumn("language", [
$language => [
'type' => 'TEXT'
]
]);
$this->session->setFlashdata('message', 'Language added successfully');
return redirect()->route('backend/setting/language');
}
} else {
$this->session->setFlashdata('exception', display('please_try_again'));
}
return redirect()->route('backend/setting/language');
}
I have 3 views. The create and update view are the same. In the _form.php view file. I have this:
<?php
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'adr_country_id') ->dropDownList($cities) ?>
<?php ActiveForm::end()?>
In the create and update view:
<?= $this->render('_form', ['model' => $model, cities' => $cities ]); ?>
. I can create new record but cant update it. I got the error in the title. This is my controller for update. What might be the problem here? Thanks in advance
public function actionUpdate($id) {
$model = ArrayHelper::map(AdrCountry::find()->all(),'id','name');
$cities = AdrCity::findOne($id);
if ($cities->load(Yii::$app->request->post()) && $cities->validate())
{
$cities->save();
Yii::$app->session->setFlash('success', ' updated');
return $this->redirect(['index']);
}
return $this->render('update',[
'cities' => $cities,
'model' => $model
]); }
Update you controller's action
public function actionUpdate($id) {
$cities = ArrayHelper::map(AdrCountry::find()->all(),'id','name');
$model = AdrCity::findOne($id);
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
$model->save();
Yii::$app->session->setFlash('success', ' updated');
return $this->redirect(['index']);
}
return $this->render('update',[
'cities' => $cities,
'model' => $model
]); }
I'm getting error from yii\web\Response when I use ajax validation.
Object of class yii\web\Response could not be converted to string
widget:
public function run()
{
$model = new Participants();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$list = implode( ", ",$model->sections);
$model->sections = $list;
$model->save();
Yii::$app->session->setFlash(Alert::TYPE_SUCCESS, [
[
'title' => 'Congratulations!',
'text' => 'You are registered on the forum. Check out your email to get news about forum.',
'confirmButtonText' => 'Done!',
]
]);
return Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
}
return $this->render('header', [
'model' => $model,
]);
}
view of widget:
<?php $form = ActiveForm::begin();?>
...
<?= $form->field($model, 'email', ['enableAjaxValidation' => true])->textInput(['placeholder' => 'Email']); ?>
how I can solve this error? P.S. yii version - 2.0.17-dev
\yii\base\Widget::run() must return a string (all widgets basically)
All you should do within method run() is output or render content and you're attempting to return a Response object by way of return ActiveForm::validate($model); and return Yii::$app->controller->redirect(..)
i suggest you move all the code that supposed to handle form submission to it's own action
SiteController extends controller {
public function actionRegisterParticipant {
$model = new Participants();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$list = implode(", ", $model->sections);
$model->sections = $list;
$model->save();
Yii::$app->session->setFlash(Alert::TYPE_SUCCESS, [
[
'title' => 'Congratulations!',
'text' => 'You are registered on the forum. Check out your email to get news about forum.',
'confirmButtonText' => 'Done!',
]
]);
return Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
}
// ...
}
and the form in the widget view should always point to this action:
<?php $form = ActiveForm::begin(['action' => 'site/register-participant']);?>
...
<?= $form->field($model, 'email', ['enableAjaxValidation' => true])->textInput(['placeholder' => 'Email']); ?>
Widget should return string as a result, but return Yii::$app->controller->redirect() returns Response object with configured redirection. If you want to redirect inside of widget you should use something like that:
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->controller->asJson(ActiveForm::validate($model));
Yii::$app->end();
}
// ...
Yii::$app->session->setFlash(/* ... */);
Yii::$app->controller->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
Yii::$app->end();
But this smells like a bad design - widget should not be responsible for controlling application flow. It is better to handle user input in regular action/controller.
I have two tables with foreign key relation . How to store company name in employee table.
in my view i have like this
<?= $form->field($model, 'Company_company_id')->dropDownList(ArrayHelper::map(
Company::find()->orderBy('Company_name')->all(),'Company_id','Company_name'),
['prompt'=>'Select Company','id' =>'cname','name'=>'cname'])
?>
controller
public function actionCreate()
{
$model = new Employee();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->save();
return $this->redirect(['index']);
}else {
return $this->render('create', [
'model' => $model,]);
}
}
Try this
<?= $form->field($model, 'company_id')->dropDownList(
ArrayHelper::map(Company::find()->
where(['id'=>company_id])->all(), 'id', 'companyname'),
[ 'prompt' => 'Please Select Company']
)
?>
I am using yii2 advanced template improved
Not sure if relevant to problem/a solution but to get to the category I have a form with some javascript which on change redirects the user to the category selected.
I have the following code which allows me to access the posts within that category id I go to localhost:8888/advanced/article/category?id=1
The problem at the minute is that if I call getCategoryName function in my model from my category view the id parameter isn't being passed to the model function therefore when using getCategoryName defaults to sport.
public function actionCategory($id)
{
$model = new Article();
$searchModel = new ArticleSearch();
$query = Article::find()
->where(['category' => $id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $this->render('category', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model'=>$model,
]);
}
Then within my view I use the following which works to an extent in terms of executing the model function, however I am unsure on how to pass the parameter/current category id to the model function. The below code work for the _index & in the single article view.
<?= $model->CategoryName ?>
This is my model function
public function getCategoryName($category = null)
{
$category = (empty($category)) ? $this->category : $category ;
if ($category === self::CATEGORY_ECONOMY)
{
return Yii::t('app', 'Economy');
}
elseif ($category === self::CATEGORY_SOCIETY)
{
return Yii::t('app', 'Society');
}
else
{
return Yii::t('app', 'Sport');
}
}
Use something like this, in your view
$request = Yii::$app->request;
$id = $request->get('id');//get id from request or change according to your requirement
echo $model->getCategoryName($id);//full method name here
Try to use this. change === to ==:
public function getCategoryName($category = null)
{
$category = (empty($category)) ? $this->category : $category ;
if ($category == self::CATEGORY_ECONOMY)
{
return Yii::t('app', 'Economy');
}
elseif ($category == self::CATEGORY_SOCIETY)
{
return Yii::t('app', 'Society');
}
else
{
return Yii::t('app', 'Sport');
}
}