I am trying to use philippfrenzel/yii2fullcalendar
But I got this Error
PHP Fatal Error – yii\base\ErrorException
Class 'yii2fullcalendar\models\Event' not found
I have the following code:
public function actionBooking()
{
$events = array();
//Testing
$Event = new \yii2fullcalendar\models\Event();
$Event->id = 1;
$Event->title = 'Testing';
$Event->start = date('Y-m-d\TH:m:s\Z');
$events[] = $Event;
$Event = new \yii2fullcalendar\modelsEvent();
$Event->id = 2;
$Event->title = 'Testing';
$Event->start = date('Y-m-d\TH:m:s\Z',strtotime('tomorrow 6am'));
$events[] = $Event;
?>
<div class="calendar-index">
<?php echo \yii2fullcalendar\yii2fullcalendar::widget(array(
'events'=> $events,
));
echo '</div>';
}
Related
I have created Cake PHP 3, I want to add data, but when I clik submit button, the data is didn't save but the message show data has been save. I add into two different tables. When I try to add data in one table is fine.
This is my controller
StoreController.php
public function add()
{
$this->loadComponent('General');
$setStatus = 1;
$store = $this->Stores->newEntity();
if ($this->request->is('post')) {
// dd( $this->request->getData());exit;
$connection = ConnectionManager::get('ora');
$connection->begin();
$store = $this->Stores->patchEntity($store, $this->request->getData());;
$merchantTable = TableRegistry::get('MasterFile.Merchants');
$merchant = $merchantTable->find()->where(['MERCHANT_CODE'=>$store->MERCHANT_CODE])->first();
$store->MERCHANT_ID = $merchant->MERCHANT_ID;
$store->CREATED_DATE = date("Y-m-d h:i:s");
$store->LAST_UPDATED_DATE = date("Y-m-d h:i:s");
$store->LAST_APPROVED_DATE = date("Y-m-d h:i:s");
$store->LAST_VERSION_DATE = date("Y-m-d h:i:s");
// $store->store_address->LINE1 = $store->LINE1;
// Start - Controller Code to handle file uploading
if(!empty($this->request->data['STORE_LOGO']['name']))
{
$file = $this->request->data['STORE_LOGO']; //put the data into a var for easy use
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'png'); //set allowed extensions
$fileName = $this->request->data['STORE_LOGO']['name'];
$uploadPath = WWW_ROOT.'img/store_logo/';
$uploadFile = $uploadPath.$fileName;
//only process if the extension is valid
if(in_array($ext, $arr_ext))
{
if(move_uploaded_file($this->request->data['STORE_LOGO']['tmp_name'],$uploadFile))
{
$store['STORE_LOGO'] = $uploadFile;
}
}
}
if(!empty($this->request->data['BACKGROUND_PICTURE']['name']))
{
$fileName = $this->request->data['BACKGROUND_PICTURE']['name'];
$uploadPath = WWW_ROOT.'img/background_picture/';
$uploadFile = $uploadPath.$fileName;
if(move_uploaded_file($this->request->data['BACKGROUND_PICTURE']['tmp_name'],$uploadFile))
{
$store['BACKGROUND_PICTURE'] = $uploadFile;
}
}
// now do the save
if ($this->Stores->save($store)) {
$setStatus = 1;
$message = 'The store has been saved.';
if($setStatus == 1){
$this->loadComponent('General');
$this->loadModel('MasterFile.Addresss');
$setStatus = 1;
$address = $this->Addresss->newEntity();
//dd($this->request->data);
$this->request->data['Address']['LINE1'] = $this->request->data['LINE1'];
$this->request->data['Address']['LINE2'] = $this->request->data['LINE2'];
$this->request->data['Address']['LINE3'] = $this->request->data['LINE3'];
//dd($this->request->data['Address']);
$connection = ConnectionManager::get('ora');
$connection->begin();
$address = $this->Addresss->patchEntity($address, $this->request->data['Address']);
// dd($address);
// now do the save
if ($this->Addresss->save($address)) {
$setStatus = 1;
$message = 'The store has been saved.';
}else{
$setStatus = 0;
$message = 'The store could not be saved. Please, try again.';
}
$this->Flash->set(__($message));
}
}else{
$setStatus = 0;
$message = 'The store could not be saved. Please, try again.';
}
$this->Flash->set(__($message));
if($setStatus){
$connection->commit();
return $this->redirect(['action' => 'index']);
}else {
$connection->rollback();
}
}
$this->set(compact('store'));
$this->set('_serialize', ['store']);
}
What should i do?
Thank you for your help!
Try debugging the entity:
if ($this->Stores->save($store)) {
debug($store);
...
public function actionUpload(){
$model = new CsvForm();
if($model->load(Yii::$app->request->post())){
$file = UploadedFile::getInstance($model,'file');
$filename = 'Data.'.$file->extension;
$upload = $file->saveAs('uploads/'.$filename);
if($upload){
define('CSV_PATH','uploads/');
$csv_file = CSV_PATH . $filename;
$filecsv = file($csv_file);
print_r($filecsv);
foreach($filecsv as $data){
$modelnew = new Customer1();
$hasil = explode(",",$data);
$nim = $hasil[0];
$nama = $hasil[1];
$jurusan = $hasil[2];
$angkatan = $hasil[3];
$modelnew->username = $nim;
$modelnew->firstname = $nama;
$modelnew->lastname = $jurusan;
$modelnew->password = $angkatan;
$modelnew->save();
}
unlink('uploads/'.$filename);
return $this->redirect(['site/index']);
}
}else{
return $this->render('upload',['model'=>$model]);
}
}
This is my CSV data
user1,name,pass,info
user2,name,pass,info
etc..,
So I want to skip Bold content and proceed my execution.
You could shift the array
$filecsvtemp = file($csv_file);
$filecsv = array_shift($filecsvtemp );
in this way the first row of the array is not present in the resulting array
otherwise use a counter
$cnt = 0;
$filecsv = file($csv_file);
print_r($filecsv);
foreach($filecsv as $data){
if ($cnt!=0){
$modelnew = new Customer1();
$hasil = explode(",",$data);
$nim = $hasil[0];
$nama = $hasil[1];
$jurusan = $hasil[2];
$angkatan = $hasil[3];
$modelnew->username = $nim;
$modelnew->firstname = $nama;
$modelnew->lastname = $jurusan;
$modelnew->password = $angkatan;
$modelnew->save();
}
$cnt++;
}
i want to create json like this my json in my api controller in laravel.
here the controller
$data = [];
foreach ($products as $p => $product) {
$components = [];
$product_obj = new StdClass();
$product_obj->id = $product->id;
$product_obj->name = $product->name;
$product_obj->category_id = $product->category_id;
//$product_obj->category_name = $product->category->name;
$product_obj->sku = $product->sku;
$product_obj->weight = $product->weight;
$product_obj->discount_type = $product->discount_type;
$product_obj->additional_price = $product->additional_price;
$product_obj->discount = $product->discount;
$product_obj->start_discount = $date_start;
$product_obj->end_discount = $date_end;
$product_obj->thumbnail = $product->thumbnail;
$product_obj->mime_content_type = $product->mime_content_type;
$product_obj->description = $product->description;
$product_obj->is_active = $product->is_active;
//$product_obj->created_at = $product->created_at;
//$product_obj->updated_at = $product->updated_at;
foreach ($product->components()->get() as $c => $component) {
$item_arr = [];
$component_obj = new StdClass();
$component_obj->id = $component->id;
$component_obj->product_id = $component->product_id;
$component_obj->item_id = $component->item_id;
$component_obj->quantity = $component->quantity;
$component_obj->is_mandatory = $component->is_mandatory;
$component_obj->is_customqty = $component->is_customqty;
//$component_obj->created_at = $component->created_at;
//$component_obj->updated_at = $component->updated_at;
$component_obj->quantity_step = $component->quantity_step;
$component_obj->display_order = $component->display_order;
$component_obj->quantity_display = $component->quantity_display;
$component_obj->default_template = $component->default_template;
foreach ($component->item()->get() as $i => $item) {
$item_option_groups = [];
//$options = [];
$item_templates = [];
$item_backgrounds = [];
$item_background_images = [];
$item_obj = new StdClass();
$item_obj->id = $item->id;
$item_obj->name = $item->name;
$item_obj->price = $item->price;
$item_obj->width = $item->width;
$item_obj->height = $item->height;
$item_obj->allow_image_upload = $item->allow_image_upload;
foreach ($item->itemOptionGroups()->get() as $iog => $item_option_groups) {
$item_option_groups = [];
$item_option_groups_obj = new StdClass();
$item_option_groups_obj->id = $item_option_groups->id;
$item_option_groups_obj->name = $item_option_groups->name;
//miising item option on databse
foreach ($item->options()->get() as $io => $item_options) {
$option = [];
$item_options_obj = new StdClass();
$item_options_obj->id = $item_options->id;
$item_options_obj->item_id = $item_options->item_id;
$item_options_obj->item_group_id = $item_options->item_group_id;
$item_options_obj->option_id = $item_options->option_id;
foreach ($item->option()->get() as $o => $item_options) {
$option_obj = new StdClass();
$option_obj->id = $option->id;
$option_obj->name = $option->name;
$option_obj->price = $option->price;
$option[] = $option_obj;
}
$item_options_obj->option = $item_options->option;
$item_options[] = $item_options_obj;
}
$item_option_groups_obj->item_option_group = $item_option_groups;
$item_option_groups[] = $item_option_groups_obj;
//$component_obj->item = $item_arr;
//$components[] = $component_obj;
}
foreach ($item->itemTemplates()->get() as $it => $item_templates) {
$template = [];
$item_templates_obj = new StdClass();
$item_templates_obj->id = $item_templates->id;
$item_templates_obj->template_id = $item_templates->template_id;
$item_templates_obj->item_id = $item_templates->item_id;
foreach ($item->template()->get() as $t => $template) {
$template_obj = new StdClass();
$template_obj->id = $template->id;
$template_obj->name = $template->name;
$template_obj->row = $template->row;
$template_obj->col = $template->col;
$template_obj->thumbnail = $template->thumbnail;
$template_obj->margin = $template->margin;
$template_obj->padding = $template->padding;
$template[] = $template_obj;
}
$item_templates_obj->template = $template;
$item_templates[] = $item_templates_obj;
}
foreach ($item->itemBackgrounds()->get() as $ib => $item_backgrounds) {
$background_color = [];
$item_backgrounds_obj = new StdClass();
$item_backgrounds_obj->id = $item_backgrounds->id;
$item_backgrounds_obj->item_id = $item_backgrounds->item_id;
$item_backgrounds_obj->background_color_id = $item_backgrounds->background_color_id;
foreach ($item->background_color()->get() as $bc => $background_color) {
$background_color_obj = new StdClass();
$background_color_obj->id = $background_color->id;
$background_color_obj->color = $background_color->color;
$background_color[] = $background_color_obj;
}
$item_backgrounds_obj->background_color = $background_color;
$item_backgrounds[] = $item_backgrounds_obj;
}
foreach ($item->itemBackgroundImages()->get() as $ibi => $item_background_images) {
$background_image = [];
$item_background_images_obj = new StdClass();
$item_background_images_obj->id = $item_background_images->id;
$item_background_images_obj->item_id = $item_background_images->item_id;
$item_background_images_obj->background_image_id = $item_background_images->background_image_id;
foreach ($item->background_image()->get() as $bi => $background_image) {
$background_image_obj = new StdClass();
$background_image_obj->id = $background_image->id;
$background_image_obj->image = $background_image->image;
$background_image[] = $background_image_obj;
}
$item_background_images_obj->background_image = $background_image;
$item_background_images[] = $item_background_images_obj;
}
$item_obj->item_option_groups = $item_option_groups;
//$item_obj->item_option_groups = $options;
$item_obj->item_templates = $item_templates;
$item_obj->item_backgrounds = $item_backgrounds;
$item_obj->item_background_images = $item_background_images;
$item_arr[] = $item_obj;
}
$component_obj->item = $item_arr;
$components[] = $component_obj;
}
$product_obj->product_components = $components;
$data[] = $product_obj;
}
if(count($data) == 1){
$data = $data[0];
}
return response()->json($data);
}
}
and this is what i get from this controller json can anyone solve my this ? looks like the nesting going wrong. please help.
About json responses, you could use Laravel's built-in JSON response for this.
So, instead of building the json fields yourself, let laravel do it for you, like this:
return \Response::json( /* Array||Object to be converted */ );
//or
return response()->json( /* Array||Object to be converted */ );
And about the nested relationships that might not be loaded. Assuming that 'products', and 'items...' are Models:
I think a good way would be to use the 'with' or 'load' methods from your model, like this:
$products->load('components');
If you need multiple nested relationships, you can use the 'dot notation' like described in this question
Hope it helps.
Laravel response function automatically convert an array to JSON.
For example in my controller i have following:
$return_data = array('data' => 'some data','response' => 1);
return response($return_data);
Also The json method will automatically set the Content-Type header to application/json, as well as convert the given array to JSON using the json_encode PHP function:
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
For more details you can visit: https://laravel.com/docs/5.1/responses#json-responses
I'm using Yii2 active record transaction, but the validation failed in the middle of the codes, it still commit the transaction.
Please advice.
public function actionCreate()
{
$model = new PoAgen();
$seq = Sequence::FindOne(['seq_id' => 'INV/AG', 'seq_name' => (int)date('ymd')]);
if(is_null($seq))
{
$_seq = new Sequence();
$_seq->seq_id = 'INV/AG';
$_seq->seq_name = (int)date('ymd');
$_seq->value = 0;
$_seq->save();
$model->trx_id = $_seq->seq_id . '/' . $_seq->seq_name . str_pad($_seq->value+1, 3, "0", STR_PAD_LEFT);
}
else {
$seq->value += 1;
$model->trx_id = $seq->seq_id . '/' . $seq->seq_name . str_pad($seq->value, 3, "0", STR_PAD_LEFT);
$seq->update();
}
$model->pot_cong = 0;
$model->pot_basah_kg = 0;
$model->pot_tangkai = 0;
$model->ppn = 0;
$model->pot_pinjaman = 0;
$model->pot_panjar = 0;
$model->ongkos_angkut = 0;
$model->pot_angkut = 0;
$model->is_transfer = true;
$model->buy_date = date('Y-m-d');
if ($model->load(Yii::$app->request->post())) {
$transaction = Yii::$app->db->beginTransaction();
try
{
$model->created_by = Yii::$app->user->identity->id;
$model->created_time = date('Y-m-d H:i:s');
$saldo = Saldo::findOne(1);
$kas = Kas::findOne(1000); // pembelian agen pks
$agen = Agen::findOne($model->agen_id);
if(!$model->is_transfer)
{
if($kas->code == 'D')
{
$saldo->balance -= $model->total_bayar;
}
else
{
$saldo->balance += $model->total_bayar;
}
$saldo->update();
}
$model->save();
// posting to kasbook
$kasbook = new KasBook();
$kasbook->kasbook_id = uniqid();
$kasbook->kas_date = $model->buy_date;
$kasbook->ref_trxid = $model->trx_id;
$kasbook->kas_id = $kas->kas_id;
$kasbook->code = $kas->code;
$kasbook->total = $model->total_bayar;
$kasbook->balance = $saldo->balance;
$kasbook->received_by = '['. $model->agen_id . '] ' . $agen->agen_name;
$kasbook->remark = 'Berat Bersih : ' . $model->r_bersih . ', Harga : ' . $model->price;
$kasbook->vehicle_id = $model->vehicle_id;
$kasbook->is_transfer = $model->is_transfer;
$kasbook->created_by = Yii::$app->user->identity->id;
$kasbook->created_time = date('Y-m-d H:i:s');
$kasbook->save();
if($model->ppn != 0)
{
$kb_1 = new KasBook();
$kb_1->kasbook_id = uniqid();
$kb_1->kas_date = $model->buy_date;
$kb_1->ref_trxid = $model->trx_id;
$kb_1->kas_id = 2000;
$kb_1->code = 'K';
$kb_1->total = $model->ppn;
$kb_1->balance = $saldo->balance;
$kb_1->remark = 'Ppn Pembelian Agen';
$kb_1->vehicle_id = $model->vehicle_id;
$kb_1->is_transfer = true;
$kb_1->created_by = Yii::$app->user->identity->id;
$kb_1->created_time = date('Y-m-d H:i:s');
$kb_1->save();
}
// kurang posting ke purchase order barang
$transaction->commit();
return $this->redirect(['view', 'id' => $model->trx_id]);
}
catch(Exception $e) {
$transaction->rollBack();
throw $e;
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
validation failed in if($model->ppn !=0) { ... }
because there is no any primary key in the master table, but the transaction still do commit and the model inserted to database.
Please advice. Thanks before.
save() method does not throw exceptions but returns boolean so you need to throw it explicitly. Replace every
...->save();
in try-catch with
if (! ...->save()) {
throw new \yii\db\Exception('Error while saving ... model!');
// or use general \Exception()
}
so now it can be caught.
You can do this by :
$flag=true;
if($model->ppn != 0)
{
$kb_1 = new KasBook();
$kb_1->kasbook_id = uniqid();
$kb_1->kas_date = $model->buy_date;
$kb_1->ref_trxid = $model->trx_id;
$kb_1->kas_id = 2000;
$kb_1->code = 'K';
$kb_1->total = $model->ppn;
$kb_1->balance = $saldo->balance;
$kb_1->remark = 'Ppn Pembelian Agen';
$kb_1->vehicle_id = $model->vehicle_id;
$kb_1->is_transfer = true;
$kb_1->created_by = Yii::$app->user->identity->id;
$kb_1->created_time = date('Y-m-d H:i:s');
if(!$kb_1->save())
{
$flag=false;
}
}
if($flag)
$transaction->commit();
else
$transaction->rollback();
i am trying to insert data from the csv excel file to the database but the code seem not to work,
i don't know where am doing wrong.
here is my code:
public function postUploadS(){
$file = array('file' => Input::file('file'));
$rule = array('file' => 'required');
$mime = array( 'text/csv',
'text/plain',
'application/csv',
'text/comma-separated-values',
'application/excel',
'application/vnd.ms-excel',
'application/vnd.msexcel'
);
$uploaded_mime = Input::file('file')->getMimeType();
$staff_list=$_FILES['file']['tmp_name'];
$linecount = "";
$cols = 5;
$numx ="";
$mimes = array(
'text/csv',
'application/csv',
'text/comma-separated-values',
'application/excel',
'application/vnd.ms-excel',
'application/vnd.msexcel',);
if(empty($staff_list)){
$_SESSION['error']="<font color='#FF0000'>Please choose the csv file to upload</font>";
}
elseif(!in_array($_FILES['file']['type'], $mimes)) {
$_SESSION['error']="<font color='#FF0000'>Invalid file format, Please choose only csv exel format</font>";
}
else{
$staff_list=$_FILES['file']['tmp_name'];
$handle=fopen($staff_list,"r");
// read the first line and ignore it
fgets($handle);
//count number of lines of uploaded csv file
$fh = fopen($staff_list,'rb') or die("ERROR OPENING DATA");
while (fgets($fh) !== false) $linecount++;
fclose($fh);
var_dump($fh); exit();
while(($fileop=fgetcsv($handle,1000,",")) !==false){
$fullname = $fileop[1];
$staff_id = $fileop[0];
$gender = $fileop[2];
$position= $fileop[3];
$department= $fileop[4];
$numx = count($fileop);
$tfulname = trim($fullname);
$lfulname = strtolower($tfulname);
$name_full = explode(' ', $lfulname);
$firstname = $name_full[0];
$middlename = implode(array_slice($name_full, 1, -1));
$lastname = end($name_full);
$phone = '';
$email = '';
$college = '';
if($gender == 'M'){
$gender == 'Male';
}
elseif ($gender =='F') {
$gender == 'Female';
}
DB::beginTransaction();
try{
$staff = new Staff;
$staff->staff_id = $staff_id;
$staff->firstname = $firstname;
$staff->middlename = $middlename;
$staff->lastname = $lastname;
$staff->gender = $gender;
$staff->position = $position;
$staff->phone = $phone;
$staff->email = $email;
$staff->college = $college;
$staff->department = $department;
$staff->save();
}
catch(Exception $e){
Session::put('key','There is a duplicate row in your data,check the file and try again');
}
$cPass = ucfirst($lastname);
$hashed_pass = Hash::make($cPass);
$user = new User;
$user->username = $staff_id;
$user->password = $hashed_pass;
$user->save();
}
if($numx!=$cols){
DB::rollback();
Session::put("error","<font color='#FF0000'>Error,number of columns does not match the defined value,please check your file and try again</font>");
}
elseif(Session::has('key')){
DB::rollback();
Session::put("error","<font color='#FF0000'>Error,duplicate entry detected in your file,please check your file and try again</font>");
}
else{
DB::commit();
Session::put("success","<font color='#0099FF'>Staff list has been uploaded successfully</font>");
}
}
}
when i run above code no data is inserted and i don't get any error. help please
$file = Reader::createFromPath('path-to-your-file');
$result = $file->fetchAll();
foreach($result as $data){
// do database add here
}