I have a _form:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'result')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test1')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test2')->textInput(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
I need to set a formula: result = test1*test2
Using raw sql I can put it in controller:
public function actionIndex(){
Yii::$app->db->createCommand("UPDATE table1 SET result = test1*test2")->execute();
etc...
}
And it's ok. But I think it's not correct. So the best way to do this is:
public function actionTest($id)
{
$model = $this->findModel($id);
$model->result = $model->test1 * model->test2
}
after:
<?= Html::a('gotest', ['test', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
But how can I get $model->id from form create to actionIndex ? Or maybe other option?
I think a clean way to do this in Yii2 should be using afterFind callback to fill your result field with your calculated value, like this:
In your Model:
public function afterFind()
{
$this->result = $this->test1 * $this->test2;
}
All logic you put in your afterFind will be executed just after Yii2 get the result from DB. It's nice when you need to calculate something before put in a form or a DataProvider, because everything will happens right in the model.
If I get this right, you won't even need the result field in your database. (in this way, just declare it in your model, creating an virtual field like this: public $result; and you'll be able to use it just like if it exists in table.
Related
So I have this CRUD where I use the same form to create and edit entries.
I need in several form selects, when creating (no data present for that particular field yet) my select to show the placeholder, but when editing, my select to show whatever is stored on database for that particular id filed. So I have:
Controller:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
and my view form:
Working for create:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
Working for edit:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
I'm having 2 troubles here, if I have null as my selected field, it won't bring the selected data on edit, if I have $prospect->client_id , it will return a error on create as there's no data yet.
I tried to solve this by creating a variable $thishouse on controller and passing it to view on return view('prospects.create', compact('house', 'thisclient','clients', 'reps')); and view Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} but seems a bit dirty whne having several form selects...
The second trouble is if I leave a placeholder on Edit, it will show the placeholder, not $prospect->client_id itself.
What's the best and simplest way to achieve all of this and use the same form for create and edit?
Thanks
You can use Form::open and Form::model to create and edit. As an example, you can set in your view:
#if(isset($prospect))
{!! Form::model($prospect, ['action' => ['ProspectController#update', $prospect->id], 'method' => 'patch']) !!}
#else
{!! Form::open(array('action' => 'ProspectController#store', 'method' => 'POST')) !!}
#endif
And then you can create the select like this:
{!! Form::select('client_id', $clients, old('client_id'), ['class' => 'form-control'] ) !!}
So, when you are editing, Laravel will select the attribute from the variable on model function.
And since you are using Laravel 5.5, you could also use #isset instruction.
How do I get a value and make it appear on a dropDownlist on yii2
For example I have 2 models which is hotel guest and service request.
I wanna get the hotelguest_id value from its model and store it on a dropdown on the service request.
Similar like this.
<?= $form->field($model, 'hotelguest_id')>dropDownList(yii\helpers\ArrayHelper::map(hotelguest_id::find(->all(),
'id', 'id')) ?>
I already figured it out
By using
use app\models\Hotelguest;
use yii\helpers\ArrayHelper;
<?= $form->field($model, 'hotelguest_id')->dropDownList(ArrayHelper::map(Hotelguest::find()->all(),
'id', 'id'), ['prompt' => 'Select Hotel Guest ID']); ?>
<?= $form->field($model, 'SRF_STUDENT_ID')->dropdownList(
ArrayHelper::map(StudentDetails::find()->all(),'STUDENT_REGISTRATION_ID','STUDENT_REGISTRATION_ID'),
['prompt'=>'Select Student Id']); ?>
how to set where condition in the dropdownlist. i want select STUDENT_REGISTRATION_ID from StudentDetails where is_select = 0; data result set dropdownlist. please give me your help.
Try this:
<?= $form->field($model, 'SRF_STUDENT_ID')->dropdownList(
ArrayHelper::map(StudentDetails::find()->where(['is_select' => 0])->all(),'STUDENT_REGISTRATION_ID','STUDENT_REGISTRATION_ID'),
['prompt'=>'Select Student Id']); ?>
I am creating a blog using Yii2. I have basic DB structure having tables:
Posts
Categories
Posts_Categories
I am using Yii2 ActiveForm to create post creation form. There are input fields for Title (text field), Content (text area), Categories (list box for multiple category selection).
I am not able to populate listBox with db values.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'Content')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName',['multiple' => true])); ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is throwing following error:
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: backend\models\Category::1
There is error in listbox line. Secondly, after populating the data in listbox, how can I handle data insertion with respect to posts and multiple categories relationship.
You have syntax error in listBox. so, complete end parenthesis ) before multiple property.
Like as,
<?= $form->field($model, 'CategoryId')->listBox(\yii\helpers\ArrayHelper::map(\backend\models\Category::find()->all(),'CategoryId','CategoryName'),['multiple' => true]); ?>
For the second part of your question on data insertion. You may need to traverse the array of the selected (posted) items in the list box. Here is an example that may be included in the actionCreate() or actionUpdate() functions of your controller file (I am assuming PostController.php):
$selectedList = $_POST['model_name']['CategoryId'];
if(isset($selectedList)) {
foreach($selectedList as $value){
$pcmodel = new Post_Categories(); //assumption on model name
//do neccessary check here
$pcmodel->post_id = $postmodel->id;
//assumption that value is being stored
$pcmodel->category_id = $value;
$pcmodel->save();
}
}
I have two models namely daily_ward_entry and discharge_note both the models are related by discharge_note.regn_number = daily_ward_entry.ipd_patient_id. I want to create a link from daily_ward_entry to update page on discharge_note.
I can't make out how to create such link.
I have tried like this, but it doesn't redirect at all.
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', $dischargenote->regn_number =>$model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>
your mistake is not correct send parameter. parameter send with key-value pairs. see doc
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', 'id' => $model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>