I am trying to get who took a book from a database so i want to put admission number so that i can get the details of the stodent; return date etc.
public function actionCreate()
{
$model = new BooksReturn();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
so you need to create SqlDataProvider , that will take admission number and check it with database with Yii::$app->user->identity->(db with student id or something) and then you can return it as a result? or I can't get what you want
Related
I need to do some stuff before saving my data in DB.
the problem is that the data changes during the validation and save process.
here is some code:
public function actionCreate()
{
$model = new Customers();
if ($model->load(Yii::$app->request->post()) && $model->validate('special_field')) {
// do some stuff
// data changes here
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
but $model->validate('special_field') function does not work
I've tried this and it worked properly
if ($model->load(Yii::$app->request->post())) {
// your stuff
if ($model->validate('special_field')) {
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
$errors = $model->errors;
var_dump($errors);
die();
}
}
My Controller:
public function actionCreate()
{
$model = new SuratMasuk(['scenario' => 'create']);
if ($model->load(Yii::$app->request->post()))
{
try
{
$picture = UploadedFile::getInstance($model, 'imageFile');
$model->imageFile = $_POST['SuratMasuk']['id_suratmasuk'].'.'.$picture->extension;
if($model->save())
{
$picture->saveAs('uploads/' . $model->id_suratmasuk.'.'.$picture->extension);
Yii::$app->getSession()->setFlash('success','Data saved!');
return $this->redirect(['view','id'=>$model->id_suratmasuk]);
}
else
{
Yii::$app->getSession()->setFlash('error','Data not saved!');
return $this->render('create', [
'model' => $model,
]);
}
}
catch(Exception $e)
{
Yii::$app->getSession()->setFlash('error',"{$e->getMessage()}");
}
}
else
{
return $this->render('create', [
'model' => $model,
]);
}
}
getting this error message when i try to save my post. and it just uploads the text not images. i've tried $model->save(false); but not working
i'm newbie on yii, i'll appreciate your help
I guess this is because you try to pass id here:
return $this->redirect(['view', 'id' => $model->id_suratmasuk]);
and since actionView almost for sure requires id as parameter you get this error because $model->id_suratmasuk is empty.
You need to set proper rules() in SuratMasuk model.
Do not use POST variables directly, this is asking for being hacked.
Do not use save(false) if you need to save anything that comes from user (validation is a must!).
Add rules() for all attributes so there will be no surprises like with this id_suratmasuk being empty.
Check result of saveAs() on UploadedFile instance - this can go false as well.
I am using yii2 for a weigh bridge project
Upon create, the user is redirected to view but my controller doesn't validate the information in such a way that even if data is not entered in the form fields a user is always redirected to view.
How can I implement the validation property
Controller code:
public function actionCreate()
{
$model = new TruckWeight1();
if ($model->load(Yii::$app->request->post()) ) {
$model->time_recorded =date('H:i:s');;
$model->recorded_by =
$model->recorded_date = date('Y-m-d');
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
try this
public function actionCreate()
{
$model = new TruckWeight1();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->time_recorded =date('H:i:s');;
$model->recorded_by =
$model->recorded_date = date('Y-m-d');
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
for more on validation validation
I want to create record where the particular field to be save contains the value of current model's id plus the format I made.
To get the id value I've tried to make Controller like this:
public function actionCreate()
{
$model = new NomorSurat();
if ($model->load(Yii::$app->request->post()))
{
// Save nosurat field with particular format name
$model->save(); // I save to get the current ID here, but return value still nothing
$number = $model->id;
$bulan = date('m');
$tahun = date('Y');
// Save with current id + custom format
$model->nosurat = $number.'/'.'BTPN-TMH'.'/'.Yii::$app->joenmarz->romanic_number($bulan).'/'.$tahun;
... // some stuff
// Then save it all, once again
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
But the variable $number saved in nosurat field returns only the custom format I've made, when I tried it in my View:
...
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'nosurat',
... // some attributes
],
]) ?>
Here's screenshot of the result:
Just add a method to your model which returns the current id and what you have saved in your database on the attribute nosurat like below and use this function for display.
public function getNosuratDisplay()
{
return $this->id.$this->nosurat;
}
This of course would make it more difficult if you want to query for that attribute because the id isn't saved in the database.
So i guess the best solution would be to generate and save this attribute in afterSave (e.g. only generate the id in insert mode i guess this is what you want).
public function generateNosurat()
{
$number = $this->id;
$bulan = date('m');
$tahun = date('Y');
$this->nosurat = $number.'/'.'BTPN-TMH'.'/'.Yii::$app->joenmarz->romanic_number($bulan).'/'.$tahun;
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if ($insert) {
$this->generateNosurat();
$this->save();
}
}
I'm writing simple shopping app and I have some issues with the add to cart link
I'm passing al the same parameter that were on Yii's default Crud form via get and i want to save them in database. Sad thing is when I tried to simply get 'post' to 'get' like this: its not working
if ($model->load(Yii::$app->request->get()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
I think I am missing something very simple but I'm rather new to this so any help will be appreciated.
Oh well i fixed that :D
public function actionCreate()
{
$model = new Koszyk();
$model->setAttributes($_GET);
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else {
$m =$_GET;
return $this->render('create', [
'model' => $model,'param'=>$m
]);
}
}