Yii2:link to related update page from different model - php

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']) ?>

Related

How to apply url in Yii framework?

I have a page (commerce.php), I want to give the link to the below-mentioned button, how I do that in Yii framework? My page path is frontend/views/site/commerce.php.
Below code is belong from index.php page
<button class="btn"><h5>Marketing Team</h5>
try the following
<?= \yii\helpers\Html::a(
'Marketing Team', ['site/market'], ['class' => 'btn btn-primary']
); ?>

How to use the update after create form in yii2?

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.

how to set the selected option using illuminate Form

I am using the HtmlServiceProvider in laravel 5.1 to build my form , I want to set the selected option in select dropdown list tag when I get the data from my model class
My code so far:
{!! Form::model($advertisment,['url'=>'dashboard/edit/advertisment']) !!}
{!! Form::select('Gender', [""=>'Select Gender',"Male"=>'Male', "Female"=>'Female'],0, ['class' => 'form-control', 'id' => 'form_control_6' ,'required']) !!}
{!! Form::close() !!}
Set the 0 parameter to "Male" for example and you will figure it out
If you don't want to override the value from the model, pass in null

How to display values in listBox() for multiple selection using Yii2

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();
}
}

Yii2 Button with Parameter

I have this button in my Yii2 project:
Html::a('label', ['/attributes/index'], ['class'=>'btn btn-primary']) ;
The button is located in the page:
/site/view
Now what I want to do is pass a parameter when this button gets clicked to the attributes/index page from the site/view page.
To be more specific it is the ID that I want to pass of a particular record from a DB I am viewing.
Cheers.
You can pass parameters as key => value pairs after the route:
Html::a('label', ['/attributes/index', 'id' => $id], ['class'=>'btn btn-primary']) ;
See the Yii2 docs: http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#toRoute()-detail

Categories