cakephp save data is giving me an error - cakephp

I am trying to insert the following array
foreach($list as $l)
{
$data = array(
'source_number'=> $l['Csv']['Source'],
'destination_number'=> $l['Csv']['Destination'],
'seconds'=> $l['Csv']['Seconds'],
'callerID'=> $l['Csv']['CallerID'],
'disposition'=> $l['Csv']['Disposition'],
'cost'=> $l['Csv']['Cost'],
'billing_cost'=> $l['Csv']['newCost']
);
//$total[] = $l['Csv']['newCost'];
debug($data);
$this->CallcenterBilling->save($data);
unset($data);
}
But it dives me this error
( ! ) Fatal error: Call to a member function save() on a non-object in C:\wamp\www\wadic\app\Controller\CallcenterBillingsController.php on line 61
Call Stack
# Time Memory Function Location
1 0.0005 708160 {main}( ) ..\index.php:0
2 0.0169 2775744 Dispatcher->dispatch( ) ..\index.php:96
3 0.0279 3946176 Dispatcher->_invoke( ) ..\Dispatcher.php:89
4 0.0293 4016984 Controller->invokeAction( ) ..\Dispatcher.php:107
5 0.0293 4017992 ReflectionMethod->invokeArgs( ) ..\Controller.php:473
6 0.0293 4018024 CallcenterBillingsController->import( ) ..\CallcenterBillingsController.php:0
what am I missing?
thanks

Firstly you may sure that in CallcenterBillingsController you have CallcenterBilling model in uses:
public $uses = array('CallcenterBilling');
Secondary you can optimize this by saving not in loop:
see saveAll method of the model
so you will get something like:
class CallcenterBillingController extends AppController {
public $uses = array('CallcenterBilling');
public function someaction() {
$data = array();
foreach ($list as $l) {
$data[] = array(
'source_number' => $l['Csv']['Source'],
'destination_number' => $l['Csv']['Destination'],
'seconds' => $l['Csv']['Seconds'],
'callerID' => $l['Csv']['CallerID'],
'disposition' => $l['Csv']['Disposition'],
'cost' => $l['Csv']['Cost'],
'billing_cost' => $l['Csv']['newCost']
);
//$total[] = $l['Csv']['newCost'];
}
debug($data);
$this->CallcenterBilling->saveAll($data);
}
}
ps. if this not helps, please show controller code.

You should implement this:
$this->CallcenterBilling->read(null, 1);
$this->CallcenterBilling->set(array(
'source_number'=> $l['Csv']['Source'],
'destination_number'=> $l['Csv']['Destination'],
'seconds'=> $l['Csv']['Seconds'],
'callerID'=> $l['Csv']['CallerID'],
'disposition'=> $l['Csv']['Disposition'],
'cost'=> $l['Csv']['Cost'],
'billing_cost'=> $l['Csv']['newCost']
));
$this->CallcenterBilling->save();
This might works for you.

Related

controller store function array laravel 5.5

I need some help. I have a problem with store function with array in laravel. When I submit my form, I got error massage that "ksort() expects parameter 1 to be array, string given".
This is my store function :
public function store(request $request) {
$input=$request->all();
$images=array();
$total = count($request->path_scan_ijazah);
// return $total;
if($files=$request->file('path_scan_ijazah')){
for ($i=0; $i < $total; $i++) {
$nip[] = $request->nip;
$instansi[] = $request['nama_instansi_pendidikan'][$i];
$jurusan[] = $request['nama_jurusan'][$i];
$jenjang[] = $request['jenjang_pendidikan'][$i];
$gelar[] = $request['gelar'][$i];
$thn_masuk = $request['tahun_masuk'][$i];
$thn_lulus = $request['tahun_lulus'][$i];
$path = $request['path_scan_ijazah'][$i]->store('public/upload/ijazah');
$images[] = $path;
$data[] = Education::insert([
'nip_employee' => $nip,
'nama_instansi_pendidikan' => $instansi,
'nama_jurusan' => $jurusan,
'jenjang_pendidikan' => $jenjang,
'gelar' => $gelar,
'tahun_masuk' => $thn_masuk,
'tahun_lulus' => $thn_lulus,
'path_scan_ijazah' => $images
]);
}
// dd($data);
}
Please help me, what should I do?

JoomGallery.net | image ordering asc

i would like to use this simple image slider for the joomgallery for my art students:
https://github.com/danielhpavey/joomgallery-slider
the only problem is the ordering. how do i get an ascending image ordering by id and not by filename ?
thanks peter
<?php
class images
{
public function __construct()
{
$file = JPATH_ROOT. '/components/com_joomgallery/interface.php';
if(!file_exists($file)){
JError::raiseError(500, 'JoomGallery seems not to be installed');
} else {
require_once $file;
$this ->interface = new JoomInterface();
}
}
public function getFirstImage()
{
$images = $this ->talkToJoomgallery();
return $images[0];
}
public function getImages()
{
$images = $this ->talkToJoomgallery();
return $images;
}
public function talkToJoomgallery()
{
$images = $this ->interface ->getPicsByCategory( $this ->categoryid );
$imagepath = $this ->joomgalleryImagePath();
$theimages = array();
$c = 0;
foreach ($images as $i){
$theimages[$c]= array(
'imgpath' => JURI::base() . $imagepath . $i->catpath . '/' . $i->imgfilename
,'imgtitle' => $i->imgtitle
,'imgtext' => $i->imgtext
);
$c ++;
}
shuffle($theimages);
return $theimages;
}
private function joomgalleryImagePath()
{
return $this ->interface ->getJConfig( 'jg_pathoriginalimages' );
}
public function __set($property, $value){
$this->$property = $value;
}
}
Your images are loaded according to image id only but this command is reindexing the array i.e shuffle($theimages);
You can comment out that line by
//shuffle($theimages)
Also for ordering images you can change this line in helper.php file
$images = $this->interface->getPicsByCategory($this->categoryid);
to
$images = $this->interface->getPicsByCategory($this->categoryid,null,'ordering' );
This will do the ordering of images the way you did drag and drop of images at the joomla administrator backend.
UPDATE AS PER YOUR LATEST QUERY
Suppose you want to add a param value (sorting) that can be controlled through admin. You ned to change the xml file mod_joomgallery_slider.xml
Just add a new field like this
<field
name = "sorting"
type = "radio"
label = "Sorting"
description = "Sort by Ordering or random"
default = "ordering"
>
<option value = "ordering">Ordering</option>
<option value = "rand()">Random</option>
</field>
Next to get the param in helper.php file then change the function talkToJoomgallery() like this
public function talkToJoomgallery()
{
//Externally calling a module param
jimport( 'joomla.html.parameter' );
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_joomgallery_slider');
$moduleParams = new JRegistry();
$moduleParams->loadString($module->params);
$sorting = $moduleParams->get( 'sorting' );
$images = $this ->interface ->getPicsByCategory( $this ->categoryid,null,$sorting );
$imagepath = $this ->joomgalleryImagePath();
$theimages = array();
$c = 0;
foreach ($images as $i){
$theimages[$c]= array(
'imgpath' => JURI::base() . $imagepath . $i->catpath . '/' . $i->imgfilename
,'imgtitle' => $i->imgtitle
,'imgtext' => $i->imgtext
);
$c ++;
}
//var_dump($theimages); exit;
//shuffle($theimages);
return $theimages;
}
UPDATE: To display 2 slider modules on a single page.
Some files needs to changed:
In mod_joomgallery_slider.php file change this line at top
include('helper.php');
To
include_once('helper.php');
This ensures that the file is included once.
Another change will be to remove the function imageText in default.php and including it in helper.php class else a function redeclaration error will be thrown. But now the default.php file still give error, as function imageText will be not defined, but you already added that function to helper.php. So default.php will only work if you change
echo imageText( $i, $params );
To
echo $image->imageText( $i, $params );// You are calling helper object
Remember to change in both if and else conditions.

array named for no reason

I calling a repository method and passing an array in it for the parameters. But the array is named after the first parameter and I don't understand why.
Here's the call :
/**
* #param $month
* #param $year
* #return Conges[]
*/
public function getAllCongesPayes($year, $month)
{
return $this->congesRepository->getNbCongesByMonth(array('year' => $year, 'month' => $month, 'cngPaye' => true));
}
And in the error I can see that :
array('year' => array('year' => '2016', 'month' => '05', 'cngPaye' => true)))
And of course it's saying "Missing argument 2" because only one array is in it.
Here is the repository method:
public function getNbCongesByMonth($year, $month, $conge){
$qb = $this->createQueryBuilder('e');
$listOfEntities = $qb
->select('count(e) as nb')
// ->leftjoin('e.cngUsrLogin', 'u')
->where(
$qb->expr()->like('e.cngDateDebut',
$qb->expr()->literal($year.'-'.$month.'-%')
)
)
->andWhere('e.congesPayes = :conge')
// ->andWhere('u.usrGestionCra = 1')
// ->groupBy('e')
->setParameter('conge', $conge)
->getQuery()
->getResult();
return $listOfEntities;
}
and the call in the controller :
$this->congesService = $this->get("intranet.conges_service");
$nbCongesPayes = $this->congesService->getAllCongesPayes('2016', '05');
If someone could explain why this happens that would be awesome.
Thanks in advance.
OK, I'm really dumb and figured it 2 minutes after... Sorry for the post...
Here is the answer :
public function getNbCongesByMonth($array){
$qb = $this->createQueryBuilder('e');
$listOfEntities = $qb
->select('count(e) as nb')
// ->leftjoin('e.cngUsrLogin', 'u')
->where(
$qb->expr()->like('e.cngDateDebut',
$qb->expr()->literal($array['year'].'-'.$array['month'].'-%')
)
)
->andWhere('e.cngPaye = :conge')
// ->andWhere('u.usrGestionCra = 1')
// ->groupBy('e')
->setParameter('conge', $array['cngPaye'])
->getQuery()
->getResult();
return $listOfEntities;
}
Needed to pass an array in the parameters. I don't know why I did that.
Anyway it's solved

Cakephp3 ElasticSearch - How to get results

How do I get Data in Array format from a 'find('all')' call.
$query->all ()->getResponse ()->getData ()['message']
gives me a json string '{\"_source[]....}'
Below is my code sample
use Cake\ElasticSearch\TypeRegistry;
class PagesController extends AppController {
public function index() {
$english_pages = TypeRegistry::get ( 'EnglishPages' );
$query = $english_pages->find ( 'all' );
// $query = $query->getData();
// $query->all () ;
// $query->all ()->getResponse () );
// json_decode ( stripslashes($query->all ()->getResponse ()->getData ()['message']) , true ) ;
// echo json_last_error_msg ();
// json_encode ( $query->all ()->getResponse ()->getData ()['message'] ) ;
}
}
The Cakephp docs are not inline with the current Cakephp3 Elastic search on Github.
It works the same as using the ORM:
$query = $english_pages->find('all');
$results = $query->toArray();

cakephp how can i tell before function from an update

I am working on a CakePHP 2.x. The scenario is I am sending an encrypted and decrypted data to the database. So in order to do this I have written beforeSave function in each modal.
so right now the problem is whenever data is updated, the data is not going encrypted into db .. please anyone know how to i fix this issue
I am doing this in my controller. The update and save function:
foreach($data as $datas){
$count = $this->Contact->checkkey($datas['idUser'],$datas['key']);
if($count>0){
$this->Contact->updateContactAgainstkey($datas['name'],
$this->request->data['Contact']['mobileNo'],
$this->request->data['Contact']['other'],
$this->request->data['Contact']['email'],
$datas['key'],$datas['idUser']);
}else{
$this->Contact->create();
$this->Contact->save($this->request->data);
}
}
updateFunction in Model
public function updateContactAgainstkey($name,$mobileNo,
$other,$email,$key,$userid){
if($this->updateAll(
array('name' => "'$name'",
'mobileNo' => "'$mobileNo'",
'workNo' => "'$workNo'",
'homeNo' => "'$homeNo'",
'other' => "'$other'",
'email' => "'$email'",),
array('User_id'=>$userid,'key'=>$key))){
return true;
}else{
return false;
}
}
beforeSave function
public function beforeSave($options=array()) {
if ( isset ( $this -> data [ $this -> alias ] [ 'mobileNo' ] ) ) {
$this -> data [ $this -> alias ] [ 'mobileNo' ] = AllSecure::encrypt($this->data[$this->alias]['email']);
}
return true;
}
please help me if anyone know how to deal with this issue.
Try following code in model
public function updateAll($fields, $conditions = true) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$created = FALSE;
$options = array();
if($db->update($this, $fields, null, $conditions)) {
$created = TRUE;
$this->Behaviors->trigger($this, 'afterSave', array($created, $options));
$this->afterSave($created);
$this->_clearCache();
$this->id = false;
return true;
}
return FALSE;
}
look here
http://nuts-and-bolts-of-cakephp.com/2010/01/27/make-updateall-fire-behavior-callbacks/
here better to use save function for updating data like:
$data=array();
$data['Contact']['mobileNo']=$this->request->data['Contact']['mobileNo'];
$data['Contact']['other']=$this->request->data['Contact']['other'];
$data['Contact']['other']=$this->request->data['Contact']['other'];
........... .............. ................
$this->Contact->id = "primerykey";
$this->Contact->save($data);
where $data contains all field that you want to update with value

Resources