<?= $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']); ?>
Related
I tried to change the label of a Submit button with no luck:
<?= $this->Form->button(__('Submit'), array('name' => 'Create')) ?>
How I can change it?
Just change the text from Submit to Create:
<?= $this->Form->button(__('Create')) ?>
It will generate a button that looks like below:
Simply do this :
$options = [];
<?= $this->Form->button(__('Create'), $options) ?>
In CakePHP 4.x, you can use
<?= $this->Form->submit("Login",['class'=>'any-css-class-if-needed']); ?>
For reference: Creating Submit Elements
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']); ?>
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.
I'm a begginer in Yii2 framework.
I work on a forum :
In the forum/posts method, there is a list of posts about the topic.
Each Post have a score which I want up and down in AJAX.
In my view posts.php, I open a Pjax block :
<?php Pjax::begin(); ?>
Votes : <?= $val['score'] ?>
<?= Html::a('+', ['/post/voteup','id'=>$val['id']]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$val['id']]) ?>
<?php Pjax::end(); ?>
In my PostController :
public function actionVoteup($id){
//Update request
$postRepo=new PostRepository();
$postRepo->vote('plus', "id=$id");
$post=$postRepo->getAll("id=$id");
return $this->renderAjax('vote', ['id'=>$id, 'score'=>$post[0]['score']]);
}
You can see I return the Vote.php view in Ajax, same code Pjax.
<?php Pjax::begin(); ?>
Votes : <?= $score ?>
<?= Html::a('+', ['/post/voteup','id'=>$id]) ?>
<?= Html::a('-', ['/post/votedown','id'=>$id]) ?>
<?php Pjax::end(); ?>
The update request is OK but I have some problems/questions :
Ex : I want to up the 2nd post score, I click, OK, I click a second time, the part of view which is refresh is the 1st Post score (but in database, it's the good score updated).
I think the problem is about my part of view that I return in my actionVoteup().
Should I return forum/posts or post/vote ?
When I click in the link, my URL is : post/voteup ; how can I return in the original URL forum/posts ?
I don't really understand how works Pjax, and I didn't find good examples about its utilisation.
Thanks for your replies :)
You need to add 'enablePushState' => false in Pjax attribute.
Like as
<?php Pjax::begin(['enablePushState' => false]); ?>
For more info. Visit this Demo
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();
}
}