I'm a beginner in yii2 , I would like to save in the table associated with the account , the account id . Any suggestions?
My Model
class Offri extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'viaggio';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['citta_part','citta_arrivo','user_id'], 'required'],
[['data_part','ora_part','data_arrivo','ora_arrivo'],'safe'],
[['posti_disponibili', 'conferma_utenze', 'user_id','posti_max'], 'integer'],
[['prezzo'], 'number'],
[['citta_part', 'via_part', 'citta_arrivo', 'via_arrivo', 'veicolo'], 'string', 'max' => 45],
[['note'], 'string', 'max' => 255],
[['wifi', 'bagno', 'ac_dc','condizioni'],'integer'],
[['user_id'], 'unique'],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id_viaggio' => Yii::t('app', 'Id Viaggio'),
'citta_part' => Yii::t('app', 'Citta Part'),
'via_part' => Yii::t('app', 'Via Part'),
'ora_part' => Yii::t('app', 'Ora Part'),
'data_part' => Yii::t('app', 'Data Part'),
'posti_disponibili' => Yii::t('app', 'Posti Disponibili'),
'conferma_utenze' => Yii::t('app', 'Conferma Utenze'),
'prezzo' => Yii::t('app', 'Prezzo'),
'note' => Yii::t('app', 'Note'),
'citta_arrivo' => Yii::t('app', 'Citta Arrivo'),
'data_arrivo' => Yii::t('app', 'Data Arrivo'),
'ora_arrivo' => Yii::t('app', 'Ora Arrivo'),
'via_arrivo' => Yii::t('app', 'Via Arrivo'),
'veicolo' => Yii::t('app', 'Veicolo'),
'posti_max' => Yii::t('app', 'Posti Max'),
'wifi' => Yii::t('app', 'Wifi'),
'bagno' => Yii::t('app', 'Bagno'),
'ac_dc' => Yii::t('app', 'Ac Dc'),
];
}
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('offri');
}
the field that I want to save in the table is user_id , which must be to the account id
My Controller
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\filters\AccessRule;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Accounts;
use app\models\Offri;
class SiteController extends Controller
{
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
[
'actions' => ['create'],
'allow' => true,
'roles' => ['admin'],
],
[
'actions' => ['view', 'search'],
'allow' => true,
'roles' => ['?', '#', 'admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* #inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionregisterUser()
{
$model = new Accounts();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->registerUser()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
}
public function actionSomeAction($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => (string) $model->_id]);
}elseif (Yii::$app->request->isAjax) {
return $this->renderAjax('_form', [
'model' => $model
]);
} else {
return $this->render('_form', [
'model' => $model
]);
}
}
public function actionOffri()
{
$model = new Offri;
if($model->load(Yii::$app->request->post())&& $model->validate() && $model->save())
{
Yii::$app->session->setFlash('success', 'Hai inserito i dati correttamente');
return $this->render('offri', ['model' => $model]);
}else {
Yii::$app->getSession()->setFlash('error', 'Completa correttamente tutti i campi.');
return $this->render('offri', ['model' => $model]);
}
}
}
update your actionOffri() like below
public function actionOffri()
{
$model = new Offri;
if($model->load(Yii::$app->request->post())){
$model->user_id=Yii::$app->user->identity->id;
if($model->validate() && $model->save()){
Yii::$app->session->setFlash('success', 'Hai inserito i dati correttamente');
return $this->render('offri', ['model' => $model]);
}else {
Yii::$app->getSession()->setFlash('error', 'Completa correttamente tutti i campi.');
return $this->render('offri', ['model' => $model]);
}
}
work my Controller
public function actionOffri()
{
$model = new Offri;
$model->user_id = Yii::$app->user->identity->id;
if($model->load(Yii::$app->request->post())&& $model->validate() && $model->save())
{
Yii::$app->session->setFlash('success', 'Hai inserito i dati correttamente');
return $this->render('offri', ['model' => $model]);
}else {
Yii::$app->getSession()->setFlash('error', 'Completa correttamente tutti i campi.');
return $this->render('offri', ['model' => $model]);
}
}
work my model
public function rules()
{
return [
[['citta_part','citta_arrivo'], 'required'],
[['data_part','ora_part','data_arrivo','ora_arrivo'],'safe'],
[['posti_disponibili', 'conferma_utenze','posti_max'], 'integer'],
[['prezzo'], 'number'],
[['citta_part', 'via_part', 'citta_arrivo', 'via_arrivo', 'veicolo'], 'string', 'max' => 45],
[['note'], 'string', 'max' => 255],
[['wifi', 'bagno', 'ac_dc','condizioni'],'integer'],
];
}
i have removed in rules 'user_id'
Related
I am working on an application with user platform shown below:
Controller
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Model
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_login_id' => Yii::t('app', 'User Login ID'),
'user_password' => Yii::t('app', 'Password'),
'user_type' => Yii::t('app', 'User Type'),
'is_block' => Yii::t('app', 'Block Status'),
'is_confirmed' => Yii::t('app', 'Block Status'),
'confirmed_at' => Yii::t('app', 'Date Confirmed'),
'created_at' => Yii::t('app', 'Created At'),
'created_by' => Yii::t('app', 'Created By'),
'updated_at' => Yii::t('app', 'Updated At'),
'updated_by' => Yii::t('app', 'Updated By'),
'current_pass' => Yii::t('app','Current Password'),
'new_pass' => Yii::t('app','New Password'),
'retype_pass' => Yii::t('app', 'Retype Password'),
'admin_user' => Yii::t('app', 'Admin Username'),
'create_password' => Yii::t('app', 'Password'),
'confirm_password' => Yii::t('app', 'Confirm Password'),
];
}
View
<?php Pjax::begin() ?>
<div class="box box-primary">
<div class="box-body">
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{items}\n{pager}",
'columns' => [
'user_login_id',
'user_type',
[
'attribute' => 'created_at',
'value' => function ($data) {
return (!empty($data->created_at) ? Yii::$app->formatter->asDate($data->created_at) : ' - ');
},
],
[
'header' => Yii::t('urights', 'Confirmation'),
'value' => function ($model) {
if (!$model->is_confirmed) {
return '<div class="text-center"><span class="text-success">' . Yii::t('urights', 'Confirmed') . '</span></div>';
} else {
return Html::a(Yii::t('urights', 'Confirm'), ['confirm', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to confirm this user?'),
]);
}
},
'format' => 'raw',
],
[
'header' => Yii::t('urights', 'Block status'),
'value' => function ($model) {
if ($model->is_block) {
return Html::a(Yii::t('urights', 'Unblock'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to unblock this user?'),
]);
} else {
return Html::a(Yii::t('urights', 'Block'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-danger btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to block this user?'),
]);
}
},
'format' => 'raw',
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
],
],
]);
?>
</div>
</div>
<?php Pjax::end() ?>
As shown in the diagram, when I click on Confirm (Green Button), it shown disable the button and turn it to Confirmed. Then set is_confimred to 0 (zero).
Also, if I click on Block (Red Button), it should change the button caption to unblock and set is_block to 0.
However, I am not getting result, but I have the page shown below:
How do I resolve it?
You should add toggle functions for both confirm and block to the controller.
public function actionConfirm($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_confirmed = $model->is_confirmed ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
public function actionBlock($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_block = $model->is_block ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
This is my user model
class User extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'user';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[[ 'created_at', 'updated_at', 'branch_id', 'postcode', 'tel_no', 'child_no','company_id'], 'integer'],
// [['branch_id', 'edu_level', 'date_joined','address1','country','state','city','postcode','race','position_level','religion','gender','staff_id','username','password','ic_number','tel_no','marital_status','child_no','bumi_status','resident_status','email','company_id'], 'required'],
[['get_mail', 'gender', 'marital_status', 'resident_status', 'bumi_status','designation','status'], 'string'],
[['date_joined'], 'safe'],
[['staff_id', 'password', 'edu_level', 'position_level', 'address2', 'address3', 'address4', 'country', 'state', 'city', 'race', 'religion'], 'string', 'max' => 100],
[['username', 'fullname', 'password_hash', 'password_reset_token', 'email', 'auth_key'], 'string', 'max' => 255],
[['ic_number'], 'string', 'max' => 14],
[['address1'], 'string', 'max' => 1000],
[['staff_id'], 'unique'],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('user', 'ID'),
'staff_id' => Yii::t('user', 'Staff ID'),
'username' => Yii::t('user', 'Username'),
'fullname' => Yii::t('user', 'Fullname'),
'password' => Yii::t('user', 'Password'),
'password_hash' => Yii::t('user', 'Password Hash'),
'password_reset_token' => Yii::t('user', 'Password Reset Token'),
'email' => Yii::t('user', 'Email'),
'ic_number' => Yii::t('user', 'Ic Number'),
'auth_key' => Yii::t('user', 'Auth Key'),
'status' => Yii::t('user', 'Status'),
'created_at' => Yii::t('user', 'Created At'),
'updated_at' => Yii::t('user', 'Updated At'),
'company_id' => Yii::t('user', 'Company'),
'branch_id' => Yii::t('user', 'Branch'),
'edu_level' => Yii::t('user', 'Education Level'),
'position_level' => Yii::t('user', 'Position Level'),
'designation' => Yii::t('user', 'Designation'),
'get_mail' => Yii::t('user', 'Get Mail'),
'date_joined' => Yii::t('user', 'Date Joined'),
'gender' => Yii::t('user', 'Gender'),
'address1' => Yii::t('user', 'Address1'),
'address2' => Yii::t('user', 'Address2'),
'address3' => Yii::t('user', 'Address3'),
'address4' => Yii::t('user', 'Address4'),
'country' => Yii::t('user', 'Country'),
'state' => Yii::t('user', 'State'),
'city' => Yii::t('user', 'City'),
'postcode' => Yii::t('user', 'Postcode'),
'tel_no' => Yii::t('user', 'Tel No'),
'marital_status' => Yii::t('user', 'Marital Status'),
'child_no' => Yii::t('user', 'Child No'),
'race' => Yii::t('user', 'Race'),
'religion' => Yii::t('user', 'Religion'),
'resident_status' => Yii::t('user', 'Resident Status'),
'bumi_status' => Yii::t('user', 'Bumi Status'),
];
}
public static function find()
{
return new UserQuery(get_called_class());
}
public function getCountries()
{
return $this->hasOne(Countries::classname(),['id'=>'country']);
}
public function getStates()
{
return $this->hasOne(States::classname(),['id'=>'state']);
}
public function getNext_of_kin()
{
return $this->hasMany(NextOfKin::classname(),['staff_id'=>'staff_id']);
}
}
This is my view controller
public function actionView($staff_id)
{
$request = Yii::$app->request;
$model = $this->findModel($staff_id);
$model2 = $model ->next_of_kin;
//$sql = "SELECT next_of_kin.name AS Name FROM user left join next_of_kin ON next_of_kin.staff_id = user.staff_id";
$dataProvider = new ActiveDataProvider([
'query' => User::find() ->joinWith(['next_of_kin'])-> where(['next_of_kin.staff_id' => $staff_id]),
]);
if($request->isAjax)
{
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'title'=> "User #".$staff_id,
'content'=>$this->renderAjax('view', [
'model' => $model,
'dataProvider' => $dataProvider,
]),
'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
Html::a('Edit',['update','staff_id'=>$staff_id],['class'=>'btn btn-primary','role'=>'modal-remote'])
];
}
else
{
return $this->render('view', [
'model' => $model,
'dataProvider' => $dataProvider,
"size" => "modal-lg",
]);
}
}
This is the view file
[
'label' => 'Next of Kin Details',
'content' => GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'username',
'fullname',
'staff_id',
'next_of_kin.name',
],
]),
],
I trying to get the name from table next_of_kin based on the staff id inside the User table. But it keep show not set when I try to view it
Why use array when you want to join with a single table/relation, I mean it shouldn't be wrong, but it's confusing.
Your query doesn't seem wrong, look for the Database module of the debugger to see what the actual query Yii AR composed and run it in the DB in a raw form. The query itself is not broken, at least from what I can tell, DB architecture, the fact that you can query for a staff_id that has nothing to return, the way you pass $staff_id and several other stuff could be broken.
activeDataProvider return a collection of models
you can retrieve an array of model from dataProvider
$myModels = $dataProvider->getModels();
eg: get the first element(user model) next_of_kin models
$my_next_of_kin = $myModels[0]->next_of_kin;
iterate over models for get a value;
foreach ($my_next_of_kin as $key => $value) {
echo $value->your_next_of_kin_col;
}
It works only with one-to-one relations! You have one-to-many.
public function getNext_of_kin()
{
return $this->hasMany(NextOfKin::classname(),['staff_id'=>'staff_id']);
}
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'username',
'fullname',
'staff_id',
'next_of_kin.name', // "next_of_kin" should be one-to-one relation
],
How to display gridview based on current user login at index? For example, student A login and fill the form and logout. When other student login, the data that has been filled by student A will not display.
At gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'education_id',
'year',
'fieldstudy',
'institute',
'grade',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
At controller
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Education::find()->
where(['user_id' => Yii::$app->user->identity->id ]),
]);
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
}
At model
class Education extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'education';
}
public function rules()
{
return [
[['year', 'fieldstudy', 'institute', 'grade'], 'required'],
[['user_id'], 'integer'],
[['year', 'fieldstudy', 'institute', 'grade'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{
return [
'education_id' => 'Education ID',
'student_id' => 'Student ID',
'year' => 'Year',
'fieldstudy' => 'Fieldstudy',
'institute' => 'Institute',
'grade' => 'Grade',
];
}
public function getId($id)
{
return static::find(['user_id' => $id]);
}
If you need retrive the Education info related to an user id and assuming that in the Education model this relation is based on the field user_id you could use Yii::$app->user->identity->id
$dataProvider = new ActiveDataProvider([
'query' => Education::find()->
where(['user_id' => Yii::$app->user->identity->id ]),
]);
When i call my create action i got the followinf error :
Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Invalid validation rule: a rule must specify both attribute names and validator type.'
That's my controllers' action:
public function actionCreate()
{
$model = new Message();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
And my rules function :
public function rules()
{
return [
[['sender_id', 'receiver_id', 'text'], 'required'],
[['sender_id', 'receiver_id', 'last_message', 'is_new', 'is_deleted_by_sender', 'is_deleted_by_receiver'], 'integer'],
[['created_at'], 'safe'],
[['text'], 'string', 'max' => 100],
[['receiver_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['receiver_id' => 'id']],
[['sender_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['sender_id' => 'id']],
[['admin/message/chat/idUser/<id:\d+>' => 'admin/message/chat']],
];
}
This
[['admin/message/chat/idUser/<id:\d+>' => 'admin/message/chat']],
is UrlManager rule, not validation rule - it should be in UrlManager configuration.
I can not update if I do not select an image. What do wrong or I'm missing?
This is a controller:
if ($model->load(Yii::$app->request->post())) {
$nameImage=$model->name;
$model->bookimg=UploadedFile::getInstance($model,'bookimg');
$model->bookimg->saveAs('images/'.$nameImage.'.'.$model->bookimg->extension);
$model->bookimg='images/'.$nameImage.'.'.$model->bookimg->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
This is a model:
[['fkperiodo'], 'integer'],
[['date_public'], 'safe'],
[['name'], 'string', 'max' => 100],
[['volume'], 'string', 'max' => 5],
[['author',], 'string', 'max' => 255],
[['bookimg'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
[['fkperiod'], 'exist', 'skipOnError' => true, 'targetClass' => Period::className(), 'targetAttribute' => ['fkperiod' => 'id']],
This is a form:
<?= $form->field($model, 'bookimg')->fileInput()->label('Book Cover') ?>
You have to check if $model to be updated had already an image and react accordingly if user has chosen a new image.
Example
if ($model->load(Yii::$app->request->post())) {
$nameImage = $model->name;
$old_image = $model->bookimg;
$model->bookimg = UploadedFile::getInstance($model, 'bookimg');
//If user has chosen an image
if (!empty($model->bookimg)) {
if (!empty($old_image)) {
//Delete old image from filesystem or/and database
//...
//...
}
//Save the new image
$model->bookimg->saveAs('images/'.$nameImage.'.'.$model->bookimg->extension);
$model->bookimg = 'images/'.$nameImage.'.'.$model->bookimg->extension;
}
//Also consider checking if model has been successfully saved before redirect
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}