In Yii2, while you use gridview widget to display your table data, for performing any operation on the record by selecting the checkbox, gridview assign the value of the first column retrieved from your the result set. But you need to set some different value to the checkbox, so to do that you can use the below code GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'class' => '\kartik\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $widget) { return ["value" => $model['id']]; // this can be substituted with any column value of your result data }, ], 'name', ['class' => 'yii\grid\ActionColumn'], ], ]); instead of the default one GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], [ 'class' => '\kartik\grid\CheckboxColumn', 'mergeHeader'=>false ], 'name', ['class' => 'yii\grid\ActionColumn'], ], ]);
Category: MVC
Validate checkbox array and prefill with selected value on validation error in laravel 5
Say for example you have your checkbox field code in your twig file as below:-
@foreach ($serviceList as $servloop)
<div class=”checkbox”>
<label>
<input type=”checkbox” name=”services[]” value=”{{$servloop->service_id}}” {{ ( is_array(old(‘services’)) && in_array($servloop->service_id, old(‘services’)) ) ? ‘checked ‘ : ” }} />
{{$servloop->service_name}}
</label>
</div>
@endforeach
Controller file code
Validation Rule will be as below:-
$rules = [
‘services’ => ‘required|min:1’, // min:1 means atleast one should be selected
];
$customMessages = [
‘services.required’ => ‘Select the service you will need from us, for and during the Expo.’,
];
how to set public permission to image uploaded on s3 aws in laravel 5.5
First set your s3 bucket configuration in your .env file as below.
S3_AWS_KEY=AKIAIDTXXXHUR7ABCDEF
S3_AWS_SECRET=2XTTy0q8xxdaddffrtsfsdfdfgfhvcdsfsf
S3_AWS_REGION=us-east-2
S3_AWS_BUCKET=BUCKET_NAME
In your controller file add the following code to set the public property to the uploaded image.
if (request()->hasFile(‘image_field_name_inyour_form’)) {
$folderPath = ‘profile/thumbnailfolder/’; // Set the folder path where you want to upload on s3 bucket
$imageName = time().rand(0,999999).’.’.request()->file(‘main_image’)->getClientOriginalExtension(); // Set name for the uploaded image with extension
$imageObj = request()->file(‘main_image’); // This will retrieve the uploaded images attributes. in short the file objects.
$uploadResponse = Storage::disk(‘s3’)->put($folderPath.$imageName, file_get_contents($imageObj), ‘public’); // here if you dont set the “public” attributes, when you access the file in your browser it will display access permission denied error.
$imageNameToStoreInYourDbOrCode = $imageName;
}
Hope you like the code. Please write back for any queries.
How to wrap data column in Gridview widget in yii or yii2 framework
The column which you want to wrap should be added the property called “contentOptions” where you can apply the css property to suits your needs and requirements as below.
[
‘attribute’ => ‘column name’,
‘format’ => ‘html’,
‘noWrap’ => false,
‘mergeHeader’=>true,
‘contentOptions’ => [‘style’ => ‘width: 50%; overflow: scroll;word-wrap: break-word;white-space:pre-line;’],
‘value’=>function ($data) {
return $data[‘question’];
},
],
How to get raw sql query in yii or yii2 framework
If you have used the query builder in yii 2.0.8 framework then to get the raw sql generated use the below statement.
$query->andFilterWhere([‘like’, ‘username’, $this->username])
->andFilterWhere([‘like’, ’email_id’, $this->email])
->andFilterWhere([‘registration_date’ => $this->register_date]);
// This is echo the raw query generated
var_dump($query->createCommand()->getRawSql());
how to get sql query from facade DB::table in laravel 5
Normally we fetch the data as below
$dataresult = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’)
->get();
Now if we want to get the actaul query from $dataresult using $dataresult->toSql(); // it will not work
The solution for this is first prepare the query without the get() method and then call the toSql() method as below.
$dataQuery = DB::table(‘sometable’)
->select(‘sometable.column1’, ‘sometable.column2’, ‘sometable.column3’, ‘sometable.column4’, ‘sometable.column5’);
$dataQuery->toSql(); // this will give you the actual query
After that you can fetch the result as below.
$dataresult = $dataQuery->get();
Read the next article to know how to get the sql with data binding
Set widget label from action file in symfony 1.4
In Action File
==========================================================
$this->form = new youFormclassname();
To set individual Label
$this->form->getWidget(‘widgetname’)->setLabel(‘widget Custom label’);
To set multiple field label
$this->form->getWidgetSchema()->setLabels(array(
‘field_name_1’ => ‘Field 1 Custom Lable’,
‘field_name_2’ => ‘Field 2 Custom Lable’,
}
++++++++++++++++++++++++++++++++++++++
In Template File
============================================
<?php echo $form[‘field_name_1’]->renderLabel() ?>
Symfony 1.4 generate url for current application and environment
$sf_request->getUriPrefix().$sf_request->getRelativeUrlRoot().$sf_request->getPathInfoPrefix()
configure database using propel
$ ./symfony configure:database --name=propel --class=sfPropelDatabase "mysql:host=localhost;dbname=dbname" username userpassword
Self join criteria in symfony
For Example :
SELECT tbl1.id FROM tbl1 a LEFT JOIN tbl1 b ON a.somecolumnname = b.somecolumn ORDER BY a.somecolumn DESC
The criteria would be as follow
$c=new Criteria();
$c->addAlias(‘a’, ‘tbl1’);
$c->addAlias(‘b’, ‘tbl1’);
$c->addSelectColumn(‘b.id’);
$c->addSelectColumn(‘a.tbl1_id’);
$c->addDescendingOrderByColumn(‘a.somecolumn’);
$c->addJoin(‘a.somecolumnname’,’b.somecolumn’,Criteria::LEFT_JOIN); // Specify your criteria LEFT, RIGHT or INNER
$rs = tbl1Peer::doSelect($c);
Thanks Cheers 🙂