Yii2 inserting new record on update - php

I have a setup where for one of the Model I have repeat fields like user and userchildren
so userchildren have a set of fields like child_name, child_birth_date and child_gender
My controller code is like this:
public function actionUpdateProfile()
{
$user_id = Yii::$app->user->identity->id;
$model = User::find()->where(['id' => $user_id])->one();
$UserProfile = UserProfile::find()->where(['user_id' => $model->id])->one();
$userbillinginfo = UserBillingInfo::find()->where(['user_id' => $model->id])->one();
$userchildren = UserChildren::find()->where(['user_id' => $model->id])->all();
if ($userchildren) {
$profile = $UserProfile;
$billinginfo = $userbillinginfo;
$userchild = $userchildren;
} else {
$profile = new UserProfile;
$profile->user_id = $model->id;
$billinginfo = new UserBillingInfo;
$billinginfo->user_id = $model->id;
$userchild = New UserChildren;
$userchild->user_id = $model->id;
}
if (Yii::$app->request->isAjax && $model->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($model);
}
if (Yii::$app->request->isAjax && $profile->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($profile);
}
if (Yii::$app->request->isAjax && $billinginfo->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($billinginfo);
}
if (Yii::$app->request->isAjax && $userchild->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($userchild);
}
if ($model->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && $billinginfo->load(Yii::$app->request->post())) {
$model->username = $model->email;
$model->save();
$profile->save();
$billinginfo->save();
if (!empty($_POST['UserChildren']) && !is_null($_POST['UserChildren'])) {
foreach ($_POST['UserChildren'] as $rows) {
$userchild = New UserChildren;
$userchild->attributes = $rows;
$userchild->user_id = $model->id;
$userchild->save();
}
}
return $this->redirect(['view']);
} else {
return $this->render('update-profile', [
'model' => $model,
'profile' => $profile,
'billinginfo' => $billinginfo,
'userchild' => $userchild,
]);
}
}
I am facing two issues:
first, when I go for an update of the profile the data is shown properly in the form, I can add records without any issue to the Model UserChildren
But if I edit an existing record it is inserting this as a new record, whereas I expect this to be updated.
I had this line of code like below:
,
if ($model->load(Yii::$app->request->post()) and
$profile->load(Yii::$app->request->post()) and
$billinginfo->load(Yii::$app->request->post()) and
$billinginfo->load(Yii::$app->request->post())
) {
...
but after I updated the line of code,
$userchildren = UserChildren::find()->where(['user_id' => $model->id])->one();
to
$userchildren = UserChildren::find()->where(['user_id' => $model->id])->all();
and thus had to modify the line like below:
if ($model->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && $billinginfo->load(Yii::$app->request->post()) ) {
What I am missing here? Thanks.

Related

How to access array values in laravel

This method i am using for storing currentTime in database using ajax call and its working
public function saveTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 404]);
}
$video = \App\Lession::where('id',$lession_id)->first();
$lesson_id = $request->lession_id;
$progress = $request->time;
//save them somewhere
$watch_history = $user->watch_history;
$watch_history_array = array();
if ($watch_history == '') {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
} else {
$founder = false;
$watch_history_array = json_decode($watch_history, true);
for ($i = 0; $i < count($watch_history_array); $i++) {
$watch_history_for_each_lesson = $watch_history_array[$i];
if ($watch_history_for_each_lesson['lession_id'] == $lesson_id) {
$watch_history_for_each_lesson['progress'] = $progress;
$watch_history_array[$i]['progress'] = $progress;
$founder = true;
}
}
if (!$founder) {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
}
}
$data['watch_history'] = json_encode($watch_history_array);
$check = User::where('id',$user->id)->update(['watch_history' => $watch_history_array]);
return response()->json(['message' => 'Time saved', 200]);//send http response as json back to the ajax call
}
But when I use this another method to get the time back for playback it's getting the array inside an array
public function getTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 403]);
}
$user_video = User::where('id',$user->id)->first();
$array = $user_video->watch_history;
foreach ($array as $key => $value) {
echo $check;
}
}
My current Output in a database is as follows
[{"lession_id":"157","progress":"71.449464"},{"lession_id":"156","progress":"92.113123"}]
So help me to get the values out of that for each lession id

I want to replicate one row into 365 row in one table in Laravel

I want to save the same data with 365 records within one table. I'm new to Laravel.
I've tried with replicate() but was unsuccessful. I tried this on the Apache server and used Laravel5.7.
my controller
public function price_save(Request $request) {
$price=new Price();
$price->price=$request->price;
$price->extra_bed=$request->extra_bed;
$price->room_id=$request->room;
$id=$request->room;
$price = Price::find($id);
if(null !== $price){
$new = $price->replicate();
if(null !== $new){
$new->push();
// $price->save();
}
I am NOT sure about your code, But you can customize it as per your needs, To just get idea :
$model = User::find($id);
$model->load('invoices');
$newModel = $model->replicate();
$newModel->push();
foreach($model->getRelations() as $relation => $items){
foreach($items as $item){
unset($item->id);
$newModel->{$relation}()->create($item->toArray());
}
}
Credits
public function price_save(Request $request, int $id)
{
if ($id > 365) {
return;
}
$price = new Price();
$price->price = $request->price;
$price->extra_bed = $request->extra_bed;
$price->room_id = $request->room;
$price->save();
$existing = Price::find($request->room);
if ($existing) {
$request = $request->replace([
'price' => $price->price,
'extra_bed' => $price->extra_bed,
'room_id' => $price->room,
]);
}
return price_save($request, $id++);
}

Yii2 getting the url ID in redirect. in create action

like I have a view index.php(Gridview)- linked to a Menu Manage Instructor.
from there I click on edit for a specific instructor.
The URL I get is like:
admin/user/update-instructor?id=11
In this page I have multiple tabs, one tab is instructor_schedule, which is again a grid view with add records button on top.
I can add records clicking on add record without any issue.
My problem is now I want the form page redirect back to the page
admin/user/update-instructor?id=11
How I can achieve that?
I have tried like:
return $this->redirect(['user/update-instructor','id' => $model->id]);
and
return $this->redirect(['user/update-instructor','id' => $model->instructor_id]);
but I am getting the error missing information id.
Thanks.
action Create(ClassDurationController):
public function actionCreate() {
$model = new ClassDuration();
$count = count(Yii::$app->request->post('ClassDuration', []));
$classdurations[] =new ClassDuration();
for($i = 1; $i < $count; $i++) {
$classdurations[] = new ClassDuration();
}
//if ($model->load(Yii::$app->request->post()) && $model->save()) {
if (Model::loadMultiple($classdurations, Yii::$app->request->post()) && Model::validateMultiple($classdurations)) {
foreach ($classdurations as $classduration) {
// var_dump($classdurations);
$classduration->instructor_id=$_POST['ClassDuration'][0]['instructor_id'];
$classduration->save(false);
}
Yii::$app->getSession()->setFlash('successClass');
//return $this->redirect(['view', 'id' => $model->id]);
return $this->redirect(['user/update-instructor','id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
'classdurations' => $classdurations,
]);
}
Action Update(ClassDurationController):
public function actionUpdate($id,$tab='information') {
$model = $this->findModel($id);
$wd_instructor = ClassDuration::find('instructor_id')->where(['id'=>$id])->One();
$wd_instructor_id = $wd_instructor->instructor_id;
$classdurations = ClassDuration::find()->where(['instructor_id'=>$wd_instructor_id])->all();
if (Model::loadMultiple($classdurations, Yii::$app->request->post()) && Model::validateMultiple($classdurations)) {
foreach($classdurations as $classduration){
$classduration->location_id=$_POST['ClassDuration'][0]['location_id'];
$classduration->save(false);
}
Yii::$app->getSession()->setFlash('successClass');
// return $this->redirect(['view', 'id' => $model->id]);
return $this->redirect(['user/update-instructor', 'id' => $model->instructor_id, 'tab' => 'instructor_schedule']);
}
return $this->render('update', [
'model' => $model,
'workingdays' => $classdurations,
]);
}
ActionUpdateInstructor:
public function actionUpdateInstructor($id,$tab='information') {
$model = User::findOne($id);
$uploadPath = 'web/instructor/' . $id;
if (!file_exists($uploadPath)) {
mkdir($uploadPath);
}
$profile = Instructor::find()->where(['user_id' => $id])->one();
if ($profile) {
$instructor_profile = $profile;
} else {
$instructor_profile = new Instructor;
$instructor_profile->user_id = $id;
}
if ($id == 1) {
$cls = 'hide';
} else {
$cls = '';
}
$title = "Update";
$modelsRest = $model->rest;
$modelsBreakTime = $model->breakTime;
if (Yii::$app->request->isAjax && $model->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($model);
}
if (Yii::$app->request->isAjax && $instructor_profile->load($_POST)) {
Yii::$app->response->format = 'json';
return \yii\bootstrap\ActiveForm::validate($instructor_profile);
}
if ($model->load(Yii::$app->request->post()) && $instructor_profile->load(Yii::$app->request->post())) {
$oldIDs = ArrayHelper::map($modelsRest, 'id', 'id');
$modelsRest = Model::createMultiple(RestDays::classname(), $modelsRest);
Model::loadMultiple($modelsRest, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsRest, 'id', 'id')));
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelsRest),
ActiveForm::validate($model),
ActiveForm::validate($instructor_profile)
);
}
if (trim($model->password) != '') {
$model->setPassword($model->password);
}
$model->username = $model->email;
$model->save();
if ($model->user_role != '') {
$assign = AuthAssignment::find()->where(['user_id' => $model->id])->One();
$assign->item_name = $model->user_role;
$assign->save();
}
$instructor_profile->file = UploadedFile::getInstance($instructor_profile, 'file');
$instructor_profile->user_id = $model->id;
if ($instructor_profile->file != '') {
$instructor_profile->instructor_image = time() . '.' . $instructor_profile->file->extension;
}
$instructor_profile->save(false);
if ($instructor_profile->file != '') {
$uploadPath = 'web/instructor/' . $instructor_profile->user_id;
if (!file_exists($uploadPath)) {
mkdir($uploadPath);
}
$instructor_profile->file->saveAs($uploadPath . '/' . $instructor_profile->instructor_image);
}
$valid = $model->validate();
$valid = Model::validateMultiple($modelsRest) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (!empty($deletedIDs)) {
RestDays::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsRest as $modelRests) {
$modelRests->instructor_id = $model->id;
if (!empty($modelRests->from_date) && !($flag = $modelRests->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
//return $this->redirect(['view-instructor', 'id' => $model->id]);
return $this->redirect(['instructor']);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('update_instructor', [
'model' => $model,
'modelsRest' => (empty($modelsRest)) ? [new RestDays] : $modelsRest,
'instructor_profile' => $instructor_profile,
'title' => $title,
'cls' => $cls,
]);
}
In your action Create(ClassDurationController) you assign to $model only
$model = new ClassDuration();
Maybe you should insert another variable to get id from parent or
return $this->redirect(['user/update-instructor','id' => $_POST['ClassDuration'][0]['instructor_id']]);

How to use mailer in FOSUserBundle without registration?

I have created a form for an entity Event with user's fields like "email" and "password" what I use to create a user manually in the controller.
I can create the event and the user without problems, but I need to send a confirmation mail to enable the user. I can do it from the normal registration form, but here I don't know how to do it.
Sorry if my english isn't very good. I'm learning it.
The controller:
class EventController extends Controller
{
public function ajaxAction(Request $request) {
if (! $request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
// Get the province ID
$id = $request->query->get('category_id');
$result = array();
// Return a list of cities, based on the selected province
$repo = $this->getDoctrine()->getManager()->getRepository('CASEventBundle:Subcategory');
$subcategories = $repo->findByCategory($id, array('category' => 'asc'));
foreach ($subcategories as $subcategory) {
$result[$subcategory->getName()] = $subcategory->getId();
}
return new JsonResponse($result);
}
public function indexAction(Request $request) {
$lead = new Lead();
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
if(is_object($user)) {
$promotor = $em->getRepository('CASUsuariosBundle:Promotor')->find($user);
if (is_object($promotor)) {
$form = $this->createForm(new EventType($this->getDoctrine()->getManager()), $lead);
$template = "CASEventBundle:Default:event.html.twig";
$isPromotor = true;
}
} else {
$form = $this->createForm(new LeadType($this->getDoctrine()->getManager()), $lead);
$template = "CASEventBundle:Default:full_lead.html.twig";
$isPromotor = false;
}
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
if($isPromotor === true) {
$type = $promotor->getType();
$name = $user->getName();
$lastname = $user->getLastName();
$email = $user->getEmail();
$password = $user->getPassword();
$phone = $user->getPhone();
$company = $promotor->getCompany();
$lead->setEventType($type);
$lead->setPromotorName($name);
$lead->setPromotorLastName($lastname);
$lead->setPromotorEmail($email);
$lead->setPromotorPhone($phone);
$lead->setPromotorCompany($company);
}
$emailReg = $form->get('promotorEmail')->getData();
$passwordReg = $form->get('promotorPassword')->getData();
$nameReg = $form->get('promotorName')->getData();
$typeReg = $form->get('promotorType')->getData();
$lastnameReg = $form->get('promotorLastName')->getData();
$phoneReg = $form->get('promotorPhone')->getData();
$companyReg = $form->get('promotorCompany')->getData();
if(!empty($emailReg) && !empty($passwordReg)) {
$userManager = $this->get('fos_user.user_manager');
$newUser = $userManager->createUser();
$newPromotor = new Promotor();
$newUser->setUsername($emailReg);
$newUser->setEmail($emailReg);
$newUser->setName($nameReg);
$newUser->setLastname($lastnameReg);
$newUser->setPhone($phoneReg);
$newUser->setIsPromotor(true);
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($newUser, strval($passwordReg));
$newUser->setPassword($encoded);
$userManager->updateUser($newUser);
$newPromotor->setType($typeReg);
$newPromotor->setCompany($companyReg);
$newPromotor->setIdPromotor($newUser);
$em->persist($newUser);
$em->persist($newPromotor);
$em->persist($lead);
$em->flush();
//return $response;
}
$em->persist($lead);
$em->flush();
return $this->redirect($this->generateUrl('CASEventBundle_create'));
}
}
return $this->render($template, array('form' => $form->createView()));
}
}
you could just create a confirmation token yourself and set it to the not yet active user, send him a mail using swift wich contains a link to confirm like :
$confiToken = "123";
$url="http://url.com/confirm/$confiToken";
$user->setConfirmationToken($confiToken);
$message = $mailer->createMessage()
->setSubject('Confirm registration')
->setFrom('from#mail.com')
->setTo($sendTo)
->setBody(
$this->renderView(
'Bundle:Email:confirm.html.twig',
array(
'headline' => "Confirm your registration",
"sendTo"=>$sendTo,
"name"=>false,
"date"=>$date,
"message"=>"here ist your confirmationlink ".$url." "
)
),
'text/html'
);
$mailer->send($message);
when the user clicks the link in the email you can manually generate the token and set the user active:
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
...
$user=$em->getRepository("Bundle:User")->findOneByConfirmationToken($token);
if(!$user || $user->isEnabled()){
throw $this->createNotFoundException("link out of date");
}else {
$user->setEnabled(true);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.context')->setToken($token);
$this->get('session')->set('_security_main',serialize($token));
$em->flush();
return $this->redirect($this->generateUrl('core_customer_dashboard'));
}

Checkboxlist boxes won't appear checked on Update page

I have a checkboxlist on both create and update views:
<?php echo $form->field($model3, 'options[]')->checkboxList(['CPF' => 'CPF', 'FWL' => 'FWL', 'SDL' => 'SDL', 'CDAC' => 'CDAC']); ?>
Then I used this code to save the checkboxlist checked boxes as strings:
$model3->options = implode(',', $model3->options);
Upon updating, I used this code to convert it back to an array:
$model3->options = explode(",", $model3->options);
I expect that there should be checked boxes in my update page based on what's checked upon creation. But the checkboxlist boxes remain unchecked.
Here's my controller:
public function actionCreateNewStaff()
{
$session = Yii::$app->session;
if($session['user_type']=='SuperAdmin'){
$model1 = new User();
$model2 = new UserModule();
$model3 = new Employee();
if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())){
$model1->user_type = 'BizStaff';
$model1->status = 1;
date_default_timezone_set('Asia/Manila');
$model1->date_added = date('Y/m/d', time());
$model1->avatar = UploadedFile::getInstance($model1, 'avatar');
$model1->creator_id = Yii::$app->user->identity->_id;
$model1->parent = $session['store_owner'];
$model1->password = sha1($model1->password);
if($model1->save()){
$model2->user_id = $model1->_id;
$model2->save();
$model3->user_id = $model1->_id;
if(isset($model3->options)){
$model3->options = implode(',', $model3->options);
}
$model3->save();
Yii::$app->session->setFlash('success', "Success!");
}
else{
Yii::$app->session->setFlash('error', "User Registration Failed!");
}
return $this->redirect(['managestaff']);
} else {
return $this->renderAjax('createstaff', [
'model1' => $model1,
'model2' => $model2,
'model3' => $model3,
]);
}
}else{
return $this->goHome();
}
}
public function actionUpdateEmployee($id)
{
$session = Yii::$app->session;
$model1 = $this->findModel($id);
$password = $model1->password;
$model2 = UserModule::find()->where( [ 'user_id' => new \MongoId($id) ] )->one();
$model3 = Employee::find()->where( [ 'user_id' => new \MongoId($id) ] )->one();
if(isset($model3->options)){
$model3->options = explode(",", $model3->options);
}
$username = $model1->username;
if($model1->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post()) && $model3->load(Yii::$app->request->post())) {
if($model1->password != $password)
$model1->password = sha1($model1->password);
else
$model1->password = $password;
date_default_timezone_set('Asia/Manila');
$model1->date_added = date('Y/m/d', time());
$model1->save();
if($model1->save()){
$model2->user_id= $model1->_id;
$count = count($session['modules']);
foreach($session['modules'] as $module){
if(is_int($model2->{$module})){
$model2->{$module} = null; //unchecked
}
else{
$model2->{$module}= '1'; //checked
}
}
$model2->save();
if(isset($model3->options)){
$model3->options = implode(',', $model3->options);
}
$model3->save();
Yii::$app->session->setFlash('success', "User successfully updated!");
return $this->redirect(['viewemployee', 'id' => (string)$model1->_id]);
}
} else {
return $this->render('updateemployee', [
'model1' => $model1,
'model2' => $model2,
'model3' => $model3,
]);
}
}
How do I display these checked boxes on my update page?
After some experimenting my guess is that you just have to change the attribute name in the ActiveField:
//from
<?php echo $form->field($model3, 'options[]')->checkboxList([....]); ?>
//to
<?php echo $form->field($model3, 'options')->checkboxList([....]); ?>
I hope this is correct. What you also should do is on imploding:
$model3->options = empty($model3->options) ? '' : implode(',', $model3->options);
Since it could be that no checkbox is selected. Then an empty string will be sent to the server.

Categories