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
]);
}
}
Related
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
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 have a form with multi-select fields named "city" form in Yii2. When I submit form
the post data show me the following:
$_POST['city'] = array('0'=>'City A','1'=>'City B','2'=>'City C')
But I want to save the array in serialize form like:
a:3:{i:0;s:6:"City A";i:1;s:6:"City B";i:2;s:6:"City C";}
But I don't know how to modify data before save function in Yii2. Followin is my code:
if(Yii::$app->request->post()){
$_POST['Adpackage']['Page'] = serialize($_POST['Adpackage']['Page']);
$_POST['Adpackage']['fixer_type'] = serialize($_POST['Adpackage']['fixer_type']);
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model
]);
}
Please help me.
Thanks all for your effort. I have solved the issue. here is the code :
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
$this->Page = serialize($_POST['Adpackage']['Page']);
$this->fixer_type = serialize($_POST['Adpackage']['fixer_type']);
return true;
} else {
return false;
}
}
Just put this code in model and its working
It's because Yii::$app->request->post() is different than $_POST at this stage. Try to change your code to:
$post = Yii::$app->request->post();
$post['Adpackage']['Page'] = serialize($post['Adpackage']['Page']);
$post['Adpackage']['fixer_type'] = serialize($post['Adpackage']['fixer_type']);
$model->load($post);
Update:
Also it would be better to do it on ActiveRecord beforeSave() method.