I am working on Yii2. I have a controller in which I am doing the following.
/**
* #param $id
* #return string|\yii\web\Response
* #throws NotFoundHttpException
* #throws \Exception
* #throws \yii\db\Exception
* #throws \yii\db\StaleObjectException
*/
public function actionViewcreated($id)// passed the id of my model which is created in the previous step
{
$params = "";
//print_r('hi');
$model= $this->findModel($id); // this will find my model/record based on the id
$sub_div = $model->sub_div;
$meter_type = $model->meter_type;
$query = /** #lang text */
"SELECT DISTINCT m.`id` AS meter_id, ins.`meter_msn` AS Meter_Serial_Number, ins.`meter_type` AS Meter_Type, sd.`sub_div_code` AS Sub_Division_Code,sd.`name` AS Sub_Division_Name
FROM `installations` ins
INNER JOIN `meters` m ON ins.`meter_msn` = m.`meter_msn`
INNER JOIN `meter_acceptance_header` map ON ins.`meter_type` = map.`meter_type`
INNER JOIN `survey` sur ON ins.`ref_no` = sur.`ref_no`
INNER JOIN `survey_hesco_subdivision` sd ON sur.`sub_division` = sd.`sub_div_code`
WHERE ins.`meter_type` = '$meter_type'
AND sd.`sub_div_code` = '$sub_div'
AND m.`id` NOT IN (SELECT DISTINCT md.`meter_id` FROM
`meter_acceptance_details` md WHERE md.`flag` IN (1))";
$session = Yii::$app->session;
$session->set('my_sql', $query);
$sqlCount= /** #lang text */
"SELECT COUNT(DISTINCT m.`id`)
FROM `installations` ins
INNER JOIN `meters` m ON ins.`meter_msn` = m.`meter_msn`
INNER JOIN `meter_acceptance_header` map ON ins.`meter_type` = map.`meter_type`
INNER JOIN `survey` sur ON ins.`ref_no` = sur.`ref_no`
INNER JOIN `survey_hesco_subdivision` sd ON sur.`sub_division` = sd.`sub_div_code`
WHERE ins.`meter_type` = '$meter_type'
AND sd.`sub_div_code` = '$sub_div'
AND m.`id` NOT IN (SELECT DISTINCT md.`meter_id` FROM `meter_acceptance_details` md WHERE md.`flag` IN (1))";
$params = Yii::$app->request->queryParams;
//print_r(isset($params->Meter_Serial_Number));
if(isset($params['Meter_Serial_Number']) && $params['Meter_Serial_Number']!==''){
$query.="AND WHERE (ins.`meter_msn`='".$params['Meter_Serial_Number']."')";
$sqlCount="AND WHERE (ins.`meter_msn`='".$params['Meter_Serial_Number']."')";
}
if(isset($params['Sub_Division_Name'])&&$params['Sub_Division_Name']!==''){
$query.="AND WHERE (sd.`name`='".$params['Sub_Division_Name']."')";
$sqlCount.="AND WHERE (sd.`name`='".$params['Sub_Division_Name']."')";
}
//print_r($query);
$count = Yii::$app->db->createCommand($sqlCount)->queryScalar();
$session = Yii::$app->session;
$session->set('total', $count);
if($count <= 0)
{
$this->findModel($id)->delete();
\Yii::$app->getSession()->setFlash('errors', '
<div class="alert alert-error alert-dismissable">
<button aria-hidden="true" data-dismiss="alert" class="close"
type="button">×</button>
<strong>There are no meters installed against the selected Sub Division!!!! </strong>Acceptance is not Created</div>');
return $this->redirect(['index', 'id' => $model->id]);
}
else
{
$dataProvider = new SqlDataProvider([
'sql' => $query,
'totalCount' => $count,
'pagination' => false,
]);
return $this->render('viewcreated', [
'dataProvider' => $dataProvider,
'model' =>$model,
'id' => $model->id
]);
}
}
View
$this->title = $model->id;
$this->title = 'Meter Acceptance Form';
$this->params['breadcrumbs'][] = $this->title;
.
.
.
<?php Pjax::begin(); ?>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label'=>'Serial #',
'value' => function($d)
{
return $d->id;
}
],
[
'label' => 'Meter Type',
'value' => function ($d) {
if(is_object($d))
return $d->meter_type;
return ' - ';
},
],
'sub_div',
[
'label' => 'Sub Division Name',
'value' => function ($d) {
if(is_object($d))
return $d->subDiv->name;
return '-';
},
],
[
'label' => 'Prepared By',
'value' => function ($d) {
if(is_object($d))
return $d->prepared->name;
},
],
'prepared_at',
'status',
],
]) ?>
.
.
.
<?php Pjax::end(); ?>
.
.
.
Submit
When I press the Submit button I am getting the following error
PHP Notice 'yii\base\ErrorException' with message 'Undefined variable: model'
in
E:\xampp\htdocs\inventory-web\backend\views\meteracceptanceheader\viewcreated.php:17
And line 17 is $this->title = $model->id;
How can I get rid of this issue?
Update 1
By doing print_r($model); exit(); I got the following result
common\models\MeterAcceptanceHeader Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 1 [sub_div] => 37111 [meter_type] => L.T.TOU [prepared_by] => 12 [prepared_at] => 2018-08-03 08:39:22 [updated_at] => [status] => Prepared ) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ( [id] => 1 [sub_div] => 37111 [meter_type] => L.T.TOU [prepared_by] => 12 [prepared_at] => 2018-08-03 08:39:22 [updated_at] => [status] => Prepared ) [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) )
Update 2:
View Process Controller
/**
* #param $id
* #return \yii\web\Response
* #throws NotFoundHttpException
* #throws \Exception
* #throws \yii\db\StaleObjectException
*/
public function actionViewprocess($id)
{
$model = $this->findModel($id);
$accpt_id = $model->id;
$meter_type = $model->meter_type;
$ogp_sub_div = $model->sub_div;
if(Yii::$app->request->isAjax && Yii::$app->request->post())
{
$data = explode(',',$_POST['data']);
foreach($data as $value)
{
$m = new MeterAcceptanceDetails;
$m -> load(Yii::$app->request->post());
$m->accpt_id = $accpt_id;
$m->meter_type = $meter_type;
$m->created_at = date('Y-m-d H:i:s');
$m->meter_id = $value;
$m->meter_msn = \common\models\Meters::idTomsn($value);
$m->flag = 1;// 1 means created
$m->ogp_sub_div = $ogp_sub_div;
if($m->save())
{
$model->status = MeterAcceptanceHeader::$status_titles[1];
$model->update();
}
else{
$this->renderAjax('viewcreated');
}
}
}
else{
$this->renderAjax('viewcreated');
}
$query = /** #lang text */
"SELECT DISTINCT a.`id` AS Accpt_Id,u.`name` AS Prepared_By,b.`meter_msn` AS Meter_Serial_Number, b.`meter_type` AS Meter_Type,
sd.`name` AS Sub_Div_Name,DATE(b.`created_at`) AS 'Date' FROM
`meter_acceptance_header` a
INNER JOIN `meter_acceptance_details` b ON a.`id` = b.`accpt_id`
INNER JOIN `survey_hesco_subdivision` sd ON b.`ogp_sub_div` = sd.`sub_div_code`
INNER JOIN `user` u ON a.`prepared_by` = u.`id`
WHERE b.`accpt_id` =$accpt_id AND b.`meter_type` = '$meter_type'";
return $this->redirect(Url::toRoute(['meteracceptanceheader/viewprocess','id' => $model->id, 'model' => $this->findModel($id)]));
}
View Process View
$this->title = $model->id;
$this->title = 'Meter Acceptance';
$this->params['breadcrumbs'][] = $this->title;
<section class="content-header">
<h1>Meter Acceptance</h1>
</section>
<section class="content">
<div class="box">
<div class="box-body">
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=>'Serial #',
'value' => 'Accpt_Id'
],
'Date',
'Prepared_By',
'Meter_Serial_Number',
'Meter_Type',
'Sub_Div_Name',
],
]); ?>
</div>
</div>
</section>
Any help would be highly appreciated.
You are using an anchor link to submit to the viewprocess
Submit
which resolves to a GET request and then in the actionViewprocess you are checking
if(Yii::$app->request->isAjax && Yii::$app->request->post())
which resolves to false and the control goes to the else part where you call
$this->renderAjax('viewcreated')
, and here is the main problem you are not passing the $model with the view which is required and hence gives you error on the very first line
$this->title = $model->id;
which is in the exception too, you should either pass all the required vars for the view if you are trying to render it from another action or redirect to that action rather than using renderAjax()
Most likely your model doesn't include phodoc comment which includes the field descriptions. Ie. on top of your model class, you should have a comment like this:
/**
* This is the model class for table "applications".
*
* #property integer $id
* #property integer $someotherfield
*/
NOTE: you have posted controller code, but the functionality there would belong to model as per MVC principles. If this comment doesn't help, please post also your model code.
Related
I have 4 tables
Equipments:
Laptop : ID - PK, laptop_model, laptop_series, laptop_date_acq, status;
Display : ID - PK, display_model, display_series, display_date_acq, status;
Phone : ID - PK, phone_model, phone_series, phone_date_acq, status;
UserEquipmentMapping: ID - PK, laptop_id ( FK TO id from table laptop), display_id(FK to id from table display), phone_id(FK to id from table Phone), start_date, end_date.
For each user, i am mapping an unique equipment with start_date and end_date, so for now in userequipmentmapping/index i am getting for one user 4 rows like this :
USER_X - Display_model - NULL - NULL - START_DATE - END_DATE;
USER_X - Display_model - NULL - NULL - START_DATE - NULL;
USER_X - NULL - Phone Model - NULL - START_DATE - NULL;
USER_X - NULL - NULL - Laptop Model - START_DATE - NULL;
i want in my grid_view to get on one single row an user with the equipments without end_date.
USER_X - DISPLAY_MODEL - PHONE_MODEL - LAPTOP_MODEL ( The ones without end_date, because the ones with end_date are not available anymore) and in view for each user to see all the equipments assigned, with or without end_date.
Any guidance is a lot of help, being a beginner in yii2. Thank you in advance !
Here is the code:
Controller:
<?php
namespace app\controllers;
use Yii;
use app\models\UserEquipmentMapping;
use app\models\UserequipmentmappingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\User;
use app\models\Laptop;
use app\models\Phone;
use app\models\Display;
/**
* UserequipmentmappingController implements the CRUD actions for UserEquipmentMapping model.
*/
class UserequipmentmappingController extends Controller
{
/**
* {#inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all UserEquipmentMapping models.
* #return mixed
*/
public function actionIndex()
{
$usermodel = new UserEquipmentMapping();
$userquery = $usermodel->getUsers();
$displaymodel = new UserEquipmentMapping();
$displayquery = $displaymodel->getDisplays();
$phonemodel = new UserEquipmentMapping();
$phonequery = $phonemodel->getPhones();
$laptopmodel = new UserEquipmentMapping();
$laptopquery = $laptopmodel->getLaptops();
#foreach($query as $q)
# print_r($q);
#
# die;
$searchModel = new UserequipmentmappingSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'userquery' => $userquery,
'displayquery' => $displayquery,
'laptopquery'=> $laptopquery,
'phonequery'=> $phonequery,
]);
}
/**
* Displays a single UserEquipmentMapping model.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new UserEquipmentMapping model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new UserEquipmentMapping();
$usermodel = User::find()->all();
$laptopmodel = Laptop::find()->all();
$phonemodel = Phone::find()->all();
$displaymodel = Display::find()->all();
#die;
#print_r(Yii::$app->request->post()); die;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//code
#foreach($laptopmodel as $m){
#print_r($m->status );
#print_r($model->load(Yii::$app->request->post()));
#print_r($model);
#print_r($model->laptop_id);
#print_r($laptopmodel);
#die;
if($model->laptop_id)
{
$laptop = Laptop::findOne($model->laptop_id);
$laptop->status = Laptop::STATUS_ASSIGNED;
$laptop->save();
}
if($model->display_id)
{
$display = Display::findOne($model->display_id);
$display->status = Display::STATUS_ASSIGNED;
$display->save();
}
if($model->phone_id)
{
$phone = Phone::findOne($model->phone_id);
$phone->status = Phone::STATUS_ASSIGNED;
$phone->save();
}
$model->save();
return $this->redirect(['index', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'usermodel' => $usermodel,
'laptopmodel' => $laptopmodel,
'phonemodel' => $phonemodel,
'displaymodel' => $displaymodel,
]);
}
/**
* Updates an existing UserEquipmentMapping model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$usermodel = User::find()->all();
$laptopmodel = Laptop::find()->all();
$phonemodel = Phone::find()->all();
$displaymodel = Display::find()->all();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
'usermodel' => $usermodel,
'laptopmodel' => $laptopmodel,
'phonemodel' => $phonemodel,
'displaymodel' => $displaymodel,
]);
}
/**
* Deletes an existing UserEquipmentMapping model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
* #throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the UserEquipmentMapping model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return UserEquipmentMapping the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = UserEquipmentMapping::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Model :
<?php
namespace app\models;
use Yii;
use app\models\User;
use app\models\UserQuery;
use yii\db\ActiveQuery;
/**
* This is the model class for table "user_equipment_mapping".
*
* #property int $id
* #property int $user_id
* #property int|null $laptop_id
* #property int|null $phone_id
* #property int|null $display_id
* #property string|null $start_date
* #property string|null $stop_date
*
* #property Display $display
* #property Laptop $laptop
* #property Phone $phone
* #property User $user
*/
class UserEquipmentMapping extends \yii\db\ActiveRecord
{
/**
* {#inheritdoc}
*/
public static function tableName()
{
return 'user_equipment_mapping';
}
/**
* {#inheritdoc}
*/
public function rules()
{
return [
[['user_id'], 'required'],
[['user_id', 'laptop_id', 'phone_id', 'display_id'], 'integer'],
[['start_date', 'stop_date'], 'safe'],
[['display_id'], 'exist', 'skipOnError' => true, 'targetClass' => Display::className(), 'targetAttribute' => ['display_id' => 'id']],
[['laptop_id'], 'exist', 'skipOnError' => true, 'targetClass' => Laptop::className(), 'targetAttribute' => ['laptop_id' => 'id']],
[['phone_id'], 'exist', 'skipOnError' => true, 'targetClass' => Phone::className(), 'targetAttribute' => ['phone_id' => 'id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* {#inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'laptop_id' => 'Laptop ID',
'phone_id' => 'Phone ID',
'display_id' => 'Display ID',
'start_date' => 'Start Date',
'stop_date' => 'Stop Date',
];
}
/**
* Gets query for [[Display]].
*
* #return \yii\db\ActiveQuery|DisplayQuery
*/
public function getDisplay()
{
return $this->hasOne(Display::className(), ['id' => 'display_id']);
}
/**
* Gets query for [[Laptop]].
*
* #return \yii\db\ActiveQuery|LaptopQuery
*/
public function getLaptop()
{
return $this->hasOne(Laptop::className(), ['id' => 'laptop_id']);
}
/**
* Gets query for [[Phone]].
*
* #return \yii\db\ActiveQuery|PhoneQuery
*/
public function getPhone()
{
return $this->hasOne(Phone::className(), ['id' => 'phone_id']);
}
/**
* Gets query for [[User]].
*
* #return \yii\db\ActiveQuery|UserQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getUsers()
{
return $this->hasMany(User::className(),['user_id', 'id']);
}
public function getLaptops()
{
return $this->hasMany(Laptop::className(),['laptop_id', 'id']);
}
public function getDisplays()
{
return $this->hasMany(Display::className(),['dislpay_id', 'id']);
}
public function getPhones()
{
return $this->hasMany(Phone::className(),['phone_id', 'id']);
}
/**
* {#inheritdoc}
* #return UserEquipmentMappingQuery the active query used by this AR class.
*/
public static function find()
{
return new UserEquipmentMappingQuery(get_called_class());
}
}
Index:
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* #var $this yii\web\View */
/* #var $searchModel app\models\UserequipmentmappingSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'User Equipment Mappings';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-equipment-mapping-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create User Equipment Mapping', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'formatter' => [
'class' => 'yii\i18n\Formatter',
'nullDisplay' => '-',],
'columns' => [
#['class' => 'yii\grid\SerialColumn'],
#'id',
#'user.username',
['attribute' => 'user',
'label' => 'Username',
'value' => 'user.username'
#},
],
#'laptop.laptop_model',
['attribute' => 'laptop',
'label' => 'Laptop Series',
'value' => 'laptop.laptop_series'
#},
],
#'laptop_id',
#'phone.phone_model',
['attribute' => 'phone',
'label' => 'Phone Series',
'value' => 'phone.phone_series'
#},
],
#'phone_id',
#'display.display_model',
['attribute' => 'display',
'label' => 'Display Series',
'value' => 'display.display_series'
#},
],
#'display_id',
'start_date',
'stop_date',
['class' => 'yii\grid\ActionColumn', 'header' => "Actions"],
],
]); ?>
</div>
Form:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\User;
use kartik\date\DatePicker;
use yii\web\View;
use yii\web\JqueryAsset;
use app\models\Laptop;
use app\models\Display;
use app\models\Phone;
$userData=ArrayHelper::map($usermodel, 'id','username');
$laptopData=ArrayHelper::map($laptopmodel,'id','laptop_series');
$phoneData=ArrayHelper::map($phonemodel,'id','phone_series');
$displayData=ArrayHelper::map($displaymodel,'id','display_series');
$availablelaptopData = ArrayHelper::map(Laptop::find()->where(['status' => 'Available'])->all(), 'id', 'laptop_series');
$availablephoneData = ArrayHelper::map(Phone::find()->where(['status' => 'Available'])->all(), 'id', 'phone_series');
$availabledisplayData = ArrayHelper::map(Display::find()->where(['status' => 'Available'])->all(), 'id', 'display_series');
?>
<div class="user-equipment-mapping-form">
<?php $form = ActiveForm::begin(); ?>
<?=$form->field($model, 'user_id')->dropDownList($userData, ['prompt'=>'Select...']);?>
<?=$form->field($model, 'laptop_id')->dropDownList($availablelaptopData, ['prompt'=>'Select...']);?>
<?=$form->field($model, 'phone_id')->dropDownList($availablephoneData, ['prompt'=>'Select...']);?>
<?=$form->field($model, 'display_id')->dropDownList($availabledisplayData, ['prompt'=>'Select...']);?>
<?=$form->field($model, 'start_date')->widget(DatePicker::classname(), [
'language' => 'en',
'value' => date('yyyy/mm/dd', strtotime('+7 days')),
'readonly' => true,
#'disabled' => true,
#'size' => 'lg',
#'type' => DatePicker::TYPE_COMPONENT_APPEND,
'options' => ['placeholder' => 'Selectati data'],
'pluginOptions' => [
#'orientation' => 'top right',
'format' => 'yyyy/mm/dd',
'todayHighlight' => true,
'todayBtn' => true,
'autoclose'=>true,
]
]);?>
<?=$form->field($model, 'stop_date')->widget(DatePicker::classname(), [
'language' => 'en',
'value' => date('yyyy/mm/dd', strtotime('+7 days')),
'readonly' => true,
#'disabled' => true,
#'size' => 'lg',
#'type' => DatePicker::TYPE_COMPONENT_APPEND,
'options' => ['placeholder' => 'Selectati data'],
'pluginOptions' => [
#'orientation' => 'top right',
'format' => 'yyyy/mm/dd',
'todayHighlight' => true,
'todayBtn' => true,
'autoclose'=>true,
]
]);?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I'm new to PHP and I'm building API for wishlist, for both guest users and logged-in users in Laravel Lumen. Here I'm using TrailManagerService as session manager
It saves data as
- for logged in users
Array
(
[trail_id] => 4b19bd9d-f2da-431b-8aba-d181d7eca736
[inception_time] => 1599813465
[last_used] => 1600762156
[customer_id] => 106210
[customer_data.customer_id] => 106210
[customer_data.firstname] => XXXX
[customer_data.lastname] => YYYY
[customer_data.gender] => Male
[customer_data.dob] => 1999-10-19
[customer_data.email] => xx#yy.com
[customer_data.mobile] => 2245436547
[customer_data.referral_code] => HRI11489
-for guest users
Array
(
[trail_id] => 8b7e6931-6ad3-48a0-af61-4caaab85cf20
[inception_time] => 1600761357
[last_used] => 1600761391
I want to add wishlist items to trailData and save it to DB if user does login and then emptytraildata after it get saved to DB. Also, if user doesnt login wishlist data should be saved in trailData for current session.
Code for WishlistControllerService
<?php
namespace App\Http\Services\v1\Products;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;
use App\Services\SOA\TrailManagerService;
use App\Repositories\WishlistRepository;
class WishlistControllerService
{
public function __construct(WishlistRepository $wishlistRepository)
{
$this->wishlistRepository = $wishlistRepository;
}
public function showList(Request $request){
$trailData = TrailManagerService::getAllTrailData('customer_id');
$trailCustomerId = TrailManagerService::getTrailData('customer_id');
$headerCustomerId = (int) $request->header('X-Customer-ID', 0);
$responseData = [];
$trailCustomerId = $trailData['customer_id'] ?? 0;
print_r($trailData);exit;
if($trailCustomerId === $headerCustomerId) {
// print_r("hi");exit;
$wishlistData = $this->wishlistRepository->getWishlist($trailCustomerId);
$responseData = [
'identifier' => 'wishlist',
'data' => [
'list' => $wishlistData
]
];
} else if($trailCustomerId === 0) {
// print_r("bye");exit;
// $tempWishlistItem = [
// // 'guestUserID' => $,
// 'productID' => $request->input('product_id'),
// 'sizeID' => $trailData['size_id'],
// 'productQuantity' => $request->input('quantity'),
// 'shippingPin' => $request->input('postal_code'),
// 'shippingCity' => $request->input('city_name'),
// ];
$responseData = [
'identifier' => '',
'data' => [
'list' => []
]
];
} else {
// print_r("why");exit;
$responseData = [
'status' => 403,
'message' => 'Access Forbidden'
];
}
return $responseData;
}
public function store($request, $productId){
$trailData = TrailManagerService::getAllTrailData();
$responseData = [];
$headerCustomerId = (int) $request->header('X-Customer-ID', 0);
$trailCustomerId = $trailData['customer_id'] ?? 0;
$tempWishlistItem =
[
// 'siteUserID`' => $customerId ?? $trailCustomerId,
'productID' => $productId,
'sizeID' => $request->input('size_id'),
'productQuantity' => $request->input('quantity'),
'currency' => strtoupper($request->input('currencyCode')),
];
$tempData[] =
array_push($trailData, $tempWishlistItem);
TrailManagerService::setTrailData($trailData);
print_r($trailData);exit;
// if($trailCustomerId === $customerId) {
$wId = $this->wishlistRepository->create($wishlistModelData);
$responseData = ['data' => ['userWID' => $wId]];
$wishlistModelData = [
'siteUserID' => $customerId,
'productID' => $productId,
'sizeID' => $request->input('size_id'),
'productQuantity' => $request->input('quantity'),
'currency' => strtoupper($request->input('currencyCode')),
];
// print_r($wishlistModelData);exit;
$wishlistItem = $this->wishlistRepository
->findWhere(['status' => 1, 'siteUserID' => $customerId, 'productID' =>
$productId]);
if(empty($wishlistItem[0]) === false) {
$responseData = [
'status' => 403,
'message' => 'Forbidden, Item already in Wishlist'
];
} else {
$wId = $this->wishlistRepository->create($wishlistModelData);
$responseData = ['data' => ['userWID' => $wId]];
}
} else {
// $tempWishlistData = [];
$tempWishlistItem =
[
// 'siteUserID' => $customerId ?? $trailCustomerId,
'productID' => $productId,
'sizeID' => $request->input('size_id'),
'productQuantity' => $request->input('quantity'),
'currency' => strtoupper($request->input('currencyCode')),
];
$tempWishlistData = array_merge($trailData, [$tempWishlistItem]);
print_r($tempWishlistData);exit;
$trailDataTemp = TrailManagerService::setTrailData(
$tempWishlistData
);
print_r($trailDataTemp);exit;
if($customerId === $trailData['customerID']){
foreach($tempWishlistData as $item) {
$wId = $this->wishlistRepository->create($wishlistModelData);
$responseData = ['data' => ['userWID' => $wId]];
}
TrailManagerService::emptyTrailData();
}
return $responseData;
}
I want to add wishlist items to trailData and save it to DB if user does login and then emptytraildata after it get saved to DB. Also, if user doesnt login wishlist data should be saved in trailData for current session.
Code for Wishlist Controller
<?php
namespace App\Http\Controllers\v1\Products;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Services\v1\Products\WishlistControllerService;
use Validator;
use App\Helpers\Utilities;
use App\Services\SOA\TrailManagerService;
use Illuminate\Support\Facades\Route;
class WishlistController extends Controller
{
private $controllerService;
public function __construct(WishlistControllerService $controllerService)
{
// $request = app(Request::class);
// $customerId = $request->header('X-Customer-ID', 0);
// TrailManagerService::authorize($customerId);
$this->controllerService = $controllerService;
}
public function index(Request $request)
{
$responseData = $this->controllerService->showList($request);
$httpStatus = $responseData['status'] ?? 200;
return $this->response($responseData, $httpStatus);
}
public function store(Request $request, int $productId){
$requestParams = $request->all();
// $requestParams['productId'] = $request->header('product_id');
$validator = Validator::make($requestParams,
[
'product_id' => 'integer',
'size_id' => 'integer',
'quantity' => 'required|integer|digits_between:1,2',
'currencyCode' => 'required|string|in:INR,EUR,USD,AUD,CAD,SGD,HKD'
],
[
'product_id.integer'=> 'Parameter product_id is mandatory',
'size_id.integer' => 'Invalid value for parameter size_id',
],
);
if ($validator->fails()) {
$messages = $validator->errors();
$responseData = Utilities::requestValiationResponse($messages);
} else {
// print_r($productId);exit;
$responseData = $this->controllerService->store($request, $productId);
}
$httpStatus = $responseData['status'] ?? 200;
return $this->response($responseData, $httpStatus);
}
public function remove(Request $request, int $productId)
{
$responseData = $this->controllerService->remove($request, $productId);
$httpStatus = $responseData['status'] ?? 200;
return $this->response($responseData, $httpStatus);
}
}
Code for Wishlist Repo
<?php
namespace App\Repositories;
use Prettus\Repository\Eloquent\BaseRepository;
use App\Models\MxUserWishlist;
use Illuminate\Support\Facades\DB;
use App\Helpers\Utilities;
class WishlistRepository extends BaseRepository
{
/**
* Specify Model class name
*
* #return string
*/
public function model()
{
return MxUserWishlist::class;
}
/**
* #param int $customerId
* #return array
*/
public function getWishlist($trailCustomerId): array
{
$wishlistData = [];
$query = "SELECT W.userWID, W.siteUserID, W.productID,P.productTitle, PSI.imageName,
W.sizeID, S.sizeTitle, D.designerName,P.designerID, P.categoryID,
PS.productPrice, PS.discountPercent,PS.filterPrice,P.seoUri
FROM mx_user_wishlist W
INNER JOIN mx_product P ON P.productID = W.productID
INNER JOIN mx_product_set PS ON (W.productID = PS.productID AND W.sizeID = PS.sizeID)
INNER JOIN mx_product_set_images PSI ON PSI.productID = W.productID
INNER JOIN mx_size S ON S.sizeID = W.sizeID
INNER JOIN mx_designer D ON P.designerID = D.designerID
WHERE W.siteUserID = $trailCustomerId";
$wishlistCollection = DB::select($query, ['siteUserID' => $trailCustomerId]);
if(empty($wishlistCollection) === false) {
foreach ($wishlistCollection as $key => $wishlist) {
$productId = 0;
$productUrl = '';
$productUrl = '/products/'.$wishlist->seoUri.'/'.$wishlist->productID;
$wishlistItems = [
'id' => $wishlist->productID,
'name' => $wishlist->productTitle,
'image' => config('global.cdni_url').'/tr:w-317/uploads/product/'.$wishlist->imageName,
'product_url' => $productUrl,
'sizes' => [
'id' => $wishlist->productID,
'name' => $wishlist->sizeTitle
],
'designer_name' => $wishlist->designerName,
'category_id' => $wishlist->categoryID,
'mrp' => $wishlist->productPrice,
'discount_percentage' => $wishlist->discountPercent,
'you_pay' => $wishlist->filterPrice,
];
}
return $wishlistItems;
} else {
$responseData = [];
return $responseData;
}
// return $responseData;
}
I want to write a mysql query in Yii2 search model but when performing searching criteria it gives the errors on joins.
This is my search model.
class StudentRegistrationSearch extends StudentRegistration {
/**
* #inheritdoc
*/
public function rules() {
return [
[['id', 'student_id', 'recordstatus', 'addedbyuserid'], 'integer'],
[[ 'registration_date', 'dateadded', 'let'], 'safe'],
];
}
/**
* #inheritdoc
*/
public function scenarios() {
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* #param array $params
*
* #return ActiveDataProvider
*/
public function search($params) {
$query = StudentRegistration::find()->where(['recordstatus' => 1]);
$query = <<<EOD
SELECT
students.student_name,
students.`id`,
students.`reg_no`,
reg.`registration_date`,
exam.`exam_year`,
exam.`exam_title`
FROM students
LEFT JOIN student_registration reg ON (reg.`student_id` = students.`id`)
LEFT JOIN student_reg_detail detail ON(detail.`student_register_id` = reg.`id`)
LEFT JOIN def_exams exam ON(exam.`id` = detail.reg_exam_id)
WHERE students.`recordstatus` = 1 AND reg.`recordstatus` = 1 AND detail.`recordstatus` = 1
ORDER BY exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name; EOD;
$query = Yii::$app->db->createCommand($query);
$query = $query->queryAll();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'student_id' => $this->student_id,
'registration_date' => $this->registration_date,
'recordstatus' => $this->recordstatus,
'dateadded' => $this->dateadded,
'addedbyuserid' => $this->addedbyuserid,
'let' => $this->let,
]);
$query->orderBy('student_id');
return $dataProvider;
}}
I want to show the data from multiple table in the single grid and then perform filter operation but simple query is not working.
Can you please help me someone.
Thanks in advance.
Try something like following
$query = (new yii\db\Query())
->from(['s' => 'students'])
->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;
OR
$query = Students::find()
->from(['s' => Students::tablename()])
->select(['s.student_name', 's.id', 's.reg_no', 'reg.registration_date', 'exam.exam_year', 'exam.exam_title'])
->leftJoin(['reg' => 'student_registration', 'stu.student_id = s.id'])
->leftJoin(['detail' => 'student_reg_detail', 'stu.student_id = s.id'])
->leftJoin(['exam' => 'def_exams ', 'exam.id = detail.reg_exam_id'])
->where(['s.recordstatus' => 1, 'reg.recordstatus' => 1, 'detail.recordstatus' => 1])
->orderBy('exam.exam_year DESC, exam.exam_title,reg.registration_date,students.student_name')
;
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
I have a Pjax container in which i want to load either books or authors.My active form has two tabs by which i choose what to load. I tested my sql in phpmyadmin and they are ok(returns what expected). Tried to var_dump after each row of my controller but it seems to be ok also. But when i comes to the 'magic of the pjax' the page get reloaded. There is no errors in the console log. The response from the request in the Network tab returns the whole html. Can you take a look and give and advance?
Controller action:
public function actionSingleGenre(){
$first_step = Yii::$app->getRequest()->getQueryParam('first_step');
$second_step = Yii::$app->getRequest()->getQueryParam('second_step');
$id = end(explode('-', $second_step));
$genresPage = Page::findOne(76);
$booksPage = Page::findOne(45);
$authorsPage = Page::findOne(46);
$lang = Lang::getCurrent();
$genre = Brands::findOne($id);
$banner = CategoryImage::find()->where(['page_id' => $genresPage->id])->one();
$pageSize = 12;
$entity = 'book';
$sql = "SELECT *
FROM `product` as p
LEFT JOIN `productLang` as pl ON p.`id`=pl.`product_id`
LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
WHERE bb.`brand_id`=$id";
if(Yii::$app->request->isPjax){
if(isset($_GET['type']) && $_GET['type'] != ''){
$type = $_GET['type'];
if($type == 1){
$sql = "SELECT *
FROM `product` as p
LEFT JOIN `productLang` as pl ON p.`id`=pl.`id`
LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
WHERE bb.`brand_id`=$id";
}
else if($type == 2)
{
$sql = "SELECT *
FROM `author` as a
LEFT JOIN `authorLang` as al ON a.`id`=al.`author_id`
WHERE a.`id` IN (
SELECT p.`author_id`
FROM `product` as p
LEFT JOIN `book_brand` as bb ON p.`id`=bb.`book_id`
WHERE bb.`brand_id`=$id
)";
$entity = 'author';
}
}
}
$count = count(Yii::$app->db->createCommand($sql)->queryAll());
$dataProvider = new SqlDataProvider([
'sql' => $sql,
'totalCount' => $count,
'pagination' => [
'pageSize' => $pageSize,
'route' => $first_step . '/' . $second_step
]
]);
$models = $dataProvider->getModels();
$pagination = new Pagination([
'pageSize' => $pageSize,
'totalCount' => $count,
'route' => $first_step . '/' . $second_step
]);
if(Yii::$app->request->isPjax){
return $this->renderAjax('single-genre', [
'genresPage' => $genresPage,
'authorsPage' => $authorsPage,
'lang' => $lang,
'banner' => $banner,
'booksPage' => $booksPage,
'genre' => $genre,
'models' => $models,
'pagination' => $pagination,
'entity' => $entity,
]);
}
return $this->render('single-genre', [
'genresPage' => $genresPage,
'authorsPage' => $authorsPage,
'lang' => $lang,
'banner' => $banner,
'booksPage' => $booksPage,
'genre' => $genre,
'models' => $models,
'pagination' => $pagination,
'entity' => $entity,
]);
}
jQuery:
function pjaxFilterForm() {
var dataString = $("#filter-group2").serialize();
$.pjax.defaults.timeout = false;
$.pjax({
container: "#booksGrid",
url: location.href.split("?")[0],
data: dataString,
scrollTo: false
});
return false;
}
View:
<div class="filter-tags-holder">
<?php \yii\bootstrap\ActiveForm::begin([
'method' => 'get',
'action' => '#',
'options' => ['data-pjax'=>true, 'onsubmit'=>'return pjaxFilterForm()', 'id'=>'filter-group2']
]) ?>
<div id="submit-helper-tab"></div>
<input type="hidden" name="type">
<ul id="filterGenresList" class="option-set">
<li><a class="selected" data-value="1" href="#"><?= Yii::t('app', 'app.Books') ?></a></li>
<li><a data-value="2" href="#"><?= Yii::t('app', 'app.Authors') ?></a></li>
</ul>
<?php \yii\bootstrap\ActiveForm::end(); ?>
</div>
<!-- Filter Nav -->
<!-- Recommended -->
<?php \yii\widgets\Pjax::begin(['id'=>'booksGrid']) ?>
<?php if($models) : ?>
<div id="filter-masonry" class="gallery-masonry">
<?php foreach ($models as $model){
if($entity == 'book'){
$model = \backend\modules\products\models\Product::findOne($model['product_id']);
echo $this->render('_authorBooks', ['book' => $model, 'lang' => $lang, 'booksPage' => $booksPage]);
}else if($entity == 'author'){
$model = \backend\models\Author::findOne($model['author_id']);
echo $this->render('_authorGenre', ['book' => $model, 'lang' => $lang, 'authorsPage' => $authorsPage]);
}
} ?>
</div>
<div class='pagination-holder'>
<?= \yii\widgets\LinkPager::widget([
'pagination' => $pagination,
'hideOnSinglePage' => true,
'prevPageLabel' => Yii::t('app', 'app.Prev'),
'nextPageLabel' => Yii::t('app', 'app.Next')
]) ?>
</div>
<?php endif; ?>
<?php \yii\widgets\Pjax::end() ?>
I have a mysql Query with joins and it is working perfect. I want to pass the result to Yii2 ActiveDataProvider.
My Controller code look like
`
public function actionIndex()
{
$theme = Theme::find();
$query = "SELECT t.*,COUNT(d.id) AS total_downloads FROM `themes` AS T
LEFT JOIN downloads AS D
ON D.theme_id = T.id GROUP
by t.id ORDER BY total_downloads DESC LIMIT 6";
$connection=Yii::$app->db;
$trends = $connection->createCommand($query);
$model = $trends->queryAll();
$object = (object) $model;
$ActiveDataProvider = new ActiveDataProvider(['query' => $object]);
return $this->render('trendings',[
'ActiveDataProvider'=>$ActiveDataProvider,
]);
}`
and View file code `
ListView::widget([
'dataProvider' => $ActiveDataProvider,
'options' => [
'tag' => 'div',
'class' => 'list-wrapper',
'id' => 'list-wrapper',
],
'pager' => [
'firstPageLabel' => 'first',
'lastPageLabel' => 'last',
'prevPageLabel' => '<span class="glyphicon glyphicon-chevron-left"></span>',
'nextPageLabel' => '<span class="glyphicon glyphicon-chevron-right"></span>',
'maxButtonCount' => 3,
],
// 'layout' => "{pager}\n{items}\n{summary}",
'summary' => false,
'itemView' => '_list',
]);
?>`
Since I have passed QueryAll so ActiveDataProvider is not woking and passing a error "The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses"
U didnt passed queryAll(), u passed a result of function queryAll(), which is an array of objects. And ure not using this:
$theme = Theme::find();
Change this:
$query = "SELECT t.*,COUNT(d.id) AS total_downloads FROM `themes` AS T
LEFT JOIN downloads AS D
ON D.theme_id = T.id GROUP
by t.id ORDER BY total_downloads DESC LIMIT 6";
to:
$theme->select = 'YOUR SELECT HERE';
Dont use queryAll(), just pass $theme object as query param do ActiveDataProvider.
And remove this:
$connection=Yii::$app->db;
$trends = $connection->createCommand($query);
$model = $trends->queryAll();
$object = (object) $model;