In radio button, value is coming. But, I want to display the name of that value.
my user_types (table)
id type status created_at
1 An Individual 1 2015
2 Firm 1 2015
UserController.php (controller)
public function actionRegister()
{
$model = new Users();
.
.
$modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])
->indexBy("id")->column();
return $this->render('register', [
'model' => $model,
'modelUserType'=>$modelUserType,
'module' => $this->module,
]);
}
register.php (view)
.
.
<?=
$form->field($model, 'user_type')
->radioList($type)
->label('Are You')
?>
.
.
It's coming like this.
I want like this
So any idea how to bring 'type' column to display instead of 'id' column.
Mr Partykar and now i understand you.This is my answer.This is work to me also for render list of radio buttons. Please, reply this is fix your problem
public function actionRegister()
{
$model = new Users();
.
.
$modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])->asArray()->all();
$type=[];
foreach($modelUserType as $k=>$v){
if(is_array($v))$type[$v["id"]]=$v["type"];
}
return $this->render('register', [
'model' => $model,
'type'=>$type,
'module' => $this->module,
]);
}
Related
I am new to yii and i am trying to use dependent drop down list as tutorial
My idea is to create a dependent drop down list by one table:
IntakeDetails [id, course_name, intake_no]
Inside the table can fill in different intakes with the same course name.
And then i using another form with the drop down (course name) and (intake). The (intake)drop down will show the values that selected from the drop down (course name).
My form:
<?= $form->field($model, 'course_name')->dropDownList(
ArrayHelper::map(IntakeDetails::find()->all(), 'course_name', 'course_name'),
[
'prompt'=>'Select course',
'onchange' => '
$.post(
"' . Url::toRoute('getoperations') . '",
{id: $(this).val()},
function(res){
$("#requester").html(res);
}
);
',
]); ?>
<?= $form->field($model,'intake_no')->dropDownList(
[],
[
'prompt' => 'Select intake',
'id' => 'requester'
]); ?>
And the controller
public function actiongetoperations()
{
if (Yii::$app->request->post('course_name')) {
$operationPosts = IntakeDetails::find()
->select('intake_no')
->where(['course_name' => $model->course_name])
->count();
if ($operationPosts > 0) {
$operations = IntakeDetails::find()
->select('intake_no')
->where(['course_name' => $model->course_name])
->all();
foreach ($operations as $operation)
echo "<option value='" . $operation->intake_no. "'>" . $operation->intake_no . "</option>";
} else
echo "<option>-</option>";
}
The form didn't give any values from the selected drop down even i have the values inserted to the database tables. So, it is possible to use it like that? If yes , please let me know. I am willing to learn. Thanks.
can you show your controller's behavior action?
does it reaches to actiongetoperations ?
whats response from action?
it should be
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'get-operations']
......
and actionGetOperations(){}
I am trying to implement the 2amigos SelectizeDropDownList widget in a form to add new values to a table directly within the dropdown.
I am using the model Book and the Model Author so basically want to be able to add a new author in the book form.
This is the book controller at the update function:
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'categories' => BookCategory::find()->active()->all(),
'publishers' => Publisher::find()->all(),
'copirights' => Copiright::find()->all(),
'authors' => Author::find()->all(),
]);
}
}
This is the form:
<?=
$form->field($model, 'author_id')->widget(SelectizeDropDownList::className(), [
// calls an action that returns a JSON object with matched
// tags
'loadUrl' => ['author/list'],
'value' => $authors,
'items' => \yii\helpers\ArrayHelper::map(\common\models\author::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
'options' => [
'class' => 'form-control',
'id' => 'id'
],
'clientOptions' => [
'valueField' => 'id',
'labelField' => 'name',
'searchField' => ['name'],
'autosearch' => ['on'],
'create' => true,
'maxItems' => 1,
],
])
?>
And this is the function author controller:
public function actionList($query) {
$models = Author::findAllByName($query);
$items = [];
foreach ($models as $model) {
$items[] = ['id' => $model->id, 'name' => $model->name];
}
Yii::$app->response->format = \Yii::$app->response->format = 'json';
return $items;
}
The form works fine to load, filter, search and add new items.
But it is not inserting the new typed attribute in the author table.
Do I need to add something in the book controller?
How can I check if it is a new value or a change of an existing author?
Thanks a lot
I made it work with the following code, not sure the most elegant because i am checking the if the author_id is a number or a string.
In my case the author won't be a number anyway.
public function actionUpdate($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$x = Yii::$app->request->post('Book');
$new_author = $x['author_id'];
if (!is_numeric($new_author)) {
$author = new Author();
$author->name = $new_author;
$author->save();
$model->author_id = $author->id;
}
if ($model->save()) {
return $this->redirect(['index']);
}
} else {
return $this->render('update', [
'model' => $model,
'categories' => BookCategory::find()->active()->all(),
'publishers' => Publisher::find()->all(),
'copirights' => Copiright::find()->all(),
'authors' => Author::find()->all(),
]);
}
}
I need to organize multiselect dropdownlist. I tried like this:
<?= $form->field($model, 'receiver_id')->widget(Select2::classname(),
[ 'data' => ArrayHelper::map(User::find()->all(),'id','username'),
'options' =>
['placeholder' => 'Select receivers...', 'multiple' => true],
'pluginOptions' =>
[ 'tags' => true,
'maximumInputLength' => 10
],
]);
?>
In the view it seems correctly, in the textfield receivers appear one by one, but when I press "Send" button it says that Receiver ID must be an integer. How can I solve this issue? I need to duplicate one db record for different receivers which I select using select2 dropdown list. For example, I choose in dropdownlist user1 and user2, "Send" action should work twice accordingly. In the db table named as 'letter' should be two same records with different id and receiver_id.
My actionCreate function in the Controller class:
public function actionCreate()
{
$model = new Letter();
if ($model->load(Yii::$app->request->post())) {
foreach($model->receiver_id as $r_id){
$save = new Letter();
$save->type_id = $model->type_id;
$save->subject = $model->subject;
$save->body = $model->body;
$save->sender_id = $model->sender_id;
$save->start_date = $model->start_date;
$save->end_date = $model->end_date;
$save->receiver_id = $r_id;
$save->save();
}
$model->attachment = UploadedFile::getInstance($model, 'attachment');
$filename = pathinfo($model->attachment , PATHINFO_FILENAME);
$ext = pathinfo($model->attachment , PATHINFO_EXTENSION);
$newFname = $filename.'.'.$ext;
$path=Yii::getAlias('#webroot').'/uploads/';
if(!empty($newFname)){
$model->attachment->saveAs($path.$newFname);
$model->attachment = $newFname;
if($model->save()){
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
return $this->render('create', [
'model' => $model,
]);
}
My IDE says on "$model->receiver_id" that "Expected types array or object, Actual: int"
Thanks in advance.
do receiver_id as array on your model; and
foreach($model->receiver_id as $r_id){
$save = new YourModel();
$save->yourProperty = $model->yourProperty;
....
$save->receiver_id = $r_id;
$save->save();
}
Hi I have 2 tables and models for them. First of them is
User
and second
News
I want to take user id from table News and draw her name and surname. There is printscreen of table News:
I am trying to use the function:
public function getUrUser()
{
$q= News::find()->where(['urUser_Id' =>'Id'])->one();
$RoyalUserData=User::findOne($q);
//$RoyalUserData= User::find()->where(['Id' => $q])->one();
return $RoyalUserData->Name;
}
But this doesnt work. Only when I prescribe to q value 3 for egzample then it work. I know that is the wrong code in my first line of my function probably. I know that is easy but I am a beginner and I've fought with the code for about 1 hour. Can anyone help me?
In my view I use this:
<?=$model->getUrUser();?>
And I use this in ListView.
My controller:
public function actionIndex()
{
$model = new NewsForm();
$searchModel = new NewsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->setSort(['defaultOrder' => ['Id'=>SORT_DESC]]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->saveNews();
return $this->redirect(['/content/news']);
} else {
return $this->render('index', [
'model' => $model,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
My index where i use _item:
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
], $options = ['class' => 'sorter'] ); ?>
And content of _item
<?php
use yii\helpers\Html;
?>
Treść Newsa:
<?=$model->Text;?> <br>
Autor:
<?=Yii::$app->user->identity->Name?>
<?=Yii::$app->user->identity->Surname?> <br>
Status Newsa:
<?=$model->cnNewsContentType_Id;?> <br>
<?= Html::a('Update', ['update', 'id' => $model->Id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->Id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?><br><br><br>
The News::find()->where(['urUser_Id' =>'Id'])->one() return a model not a field
then you must get the id field by the model this way
public function getUrUser($id)
{
// you already have the news model so don't need retrieve it
// it's enough pass the urUser_Id by $id
$RoyalUserData=User::findOne($id);
return $RoyalUserData->Name;
}
Then if you want show the ($RoyalUserData) Name in _item
<?=$model->getUrUser($model->urUser_Id);?>
public function getUrUser()
{
$q = News::find()->where(['urUser_Id' =>'Id'])->one();
return $q->user->name;
}
In user model you should create relation:
class News extends ActiveRecord
{
// ...
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
}
In my form, I'm having 8-9 fileds. In which all are hard-coded, Except one, which should come from database.
UsersControllers.php (controller)
public function actionRegister()
{
.
.
model = new Users();
$modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])->all();
return $this->render('register', [
'model' => $model,
'modelUserType'=>$modelUserType,
'module' => $this->module,
]);
}
Values are coming if i do print_r($modelUserType);
But, i don't know to show all those "UserType" table values in views for radio button.
<?=
$form->field($modelUserType, 'user_type')
->radioList(array('1' => 'An Individual', '2' => 'Firm')) // Here.
->label('Are You')
?>
register.php (view)
.
.
<?= $form->field($model, 'password')->passwordInput()->label('Password') ?>
<?= $form->field($model, 'confirm_password')->passwordInput()->label('Confirm Password') ?>
<?=
$form->field($model, 'user_type')
->radioList(array('1' => 'An Individual', '2' => 'Firm'))
->label('Are You')
?>
<?= $form->field($model, 'company_name')->textInput() ?>
.
.
I'm not getting any tutorials or I am not able to find the correct tutorials for it.
Little help needed. Please help me. Thanks.
public function actionRegister()
{
$model = new Users();
.
.
$modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])->asArray()->all();
$type=[];
foreach($modelUserType as $k=>$v){
if(is_array($v))$type[$v["id"]]=$v["type"];
}
return $this->render('register', [
'model' => $model,
'type'=>$type,
'module' => $this->module,
]);
}