I am not sure why but I am getting this error
Fatal error: Call to a member function update() on a non-object in /home/XXXXXXXXXXXXXXXXX/classes/core.php on line 22
On that page I have this
public function updatemongo($from,$data)
{
$this->db = $m->exchange_rates;
$collection = $this->db->exchangeDB;
$collection->update(array("from" => $from), $data);
}
this is how I am calling this function
foreach ($currencies as $to)
{
if($from != $to)
{
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
$handle = fopen($url, 'r');
if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
}
$newdata = array('$set' => array("exchangehistory.{$result[1]}.{$result[2]}" => array("to" => $to, "rate" => $result[0], "updated" => $result[2])));
$fetch->updatemongo($from,$newdata);
$newdata = array('$set' => array("currentexchange" => array("to" => $to, "rate" => $result[0], "updated" => $result[2])));
$fetch->updatemongo($from,$newdata);
}
}
and yes the file needing to access this is also has require_once("core.php");
Please let me know why this is not working.
The updatemongo() function doesn't have access to the $m variable. Please pass it to the function like this:
$fetch->updatemongo($m, $from, $newdata);
And change your function definition to:
public function updatemongo($m, $from, $data)
{
Alternatively, you can set the m property of the object to the connection after you've created it. For example with:
public function __construct)
{
$this->m = new Mongo();
}
...
public function updatemongo($from, $data)
{
$this->db = $this->m->exchange_rates;
$collection = $this->db->exchangeDB;
$collection->update(array("from" => $from), $data);
}
Or perhaps you can just use $this->exchange_rates already above... in any case, you're not making $m available to the function.
Looks like a typo : the object "$m" is not instantiated in function updatemongo()
Either create it here or gain access with global $m; if it exists.
$this->db->exchangeDB contains null or similar when it gets placed into $collection? It's hard to tell for us since we don't know where that variable gets instantiated.
$collection->update(array("from" => $from), $data);
is the cause of your error and the error is clear: $update is not an object. otherwise it would complain "PHP Fatal error: Call to undefined method YourClass::yourMethod() in /my/php/file.php on line xx"
Related
Hi tried refreshing the modification cache cache in opencart and since then i am getting a blank page in front end with this error message.
public function trigger($event, array $args = array()) {
foreach ($this->data as $value) {
if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($value['trigger'], '/')) . '/', $event)) {
$result = $value['action']->execute($this->registry, $args);
if (!is_null($result) && !($result instanceof Exception)) {
return $result;
}
}
}
}
Thanks,
It seems your have an OC version 3.0.2.x or above.
In your $this->data of the Event Class, you have an event registered that is missing an action parameter.
$this->data[] = array(
'trigger' => $trigger,
'action' => $action, // <-- this must be an Action Object with a method execute()
'priority' => $priority
);
All events are registered via the register() method which explicitly requests that an Action object is being passed as a parameter.
Since the error is pointing to "Call to undefined method Action::execute()", I can assume, you have an issue with the action class.
Most likely you need to check the Modifications of the system/engine/action.php file in your system/storage/modifications.
It could be that the method execute() is either missing or somehow corrupt.
Debug
try to var_dump the $value to see what is there:
public function trigger($event, array $args = array()) {
foreach ($this->data as $value) {
//log out the $value before the error to see if the Action object is actually there and see what trigger causes this.
var_dump($value);
if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($value['trigger'], '/')) . '/', $event)) {
$result = $value['action']->execute($this->registry, $args);
if (!is_null($result) && !($result instanceof Exception)) {
return $result;
}
}
}
}
Hope this helps
public function update($table, $where = array(), $data_arr = array()){
print_r($data_arr);
$adapter = $this->tableGateway->getAdapter();
$projectTable;
if($table != null){
$projectTable = new TableGateway($table, $adapter);
}else{
$projectTable = new TableGateway('account_master', $adapter);
}
echo "158";
try {
echo "123";
$rowset = $projectTable->update(function(Update $update) use ($where, $data_arr) {
$update->set(array('statement_no' => '01010'));
$update->where($where);
echo $update->getSqlString();
});
} catch (\Exception $e) {
print_r($e);
}
print_r($rowset);
die();
}
my Output print : 158123
it's give me pass array in set() function that i already pass as argument. also i have tried to convert object to array ((arrya)$objetc) but it's not work for me.
[10-Jul-2017 05:11:34 America/Denver] PHP Catchable fatal error: Argument 1 passed to Zend\Db\Sql\Update::set() must be of the type array, object given, called in /home2/flywing1/vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php on line 336 and defined in /home2/flywing1/vendor/zendframework/zend-db/src/Sql/Update.php on line 93
You may do that by implementing a Zend\Db\Sql\Update object. You may create that object using the TableGateway. You should be able to do the following in your model
public function update($set, $where)
{
// Here is the catch
$update = $this->tableGateway->getSql()->update();
$update->set($set);
$update->where($where);
// Execute the query
return $this->tableGateway->updateWith($update);
}
Try it,
I was with the same issues, I tried with this, and it worked.
$rowset = $projectTable->update(array('statement_no' => '01010'), $where);
On the backend of a PrestaShop site I'm using this function:
public function hookAjax($action, $id_product, $id_lang, $title, $descript, $order, $id = NULL)
{
/* various code*/
$this->context->smarty->assign(
array(
'block_define' => $this->getFormDesc($id_product)
)
);
return $this->context->smarty->fetch($this->local_path.'views/templates/hook/admin_extra_desc.tpl');
}
public function getFormDesc($id_product) {
$array = array();
foreach (Language::getLanguages() as $lang) {
/*various code*/
foreach($result as $k=> $r) {
$files = array();
$helper = new HelperImageUploader();
$helper->setMultiple(false)->setUseAjax(true)->setName('thumbnail_'.$r['id'].'_'.$r['id_lang'])->setFiles($files)->setMaxFiles(3)->setUrl('../modules/module-name/imgAjaxCall.php?');
$result[$k]['img-form'] = $helper->render();
$result[$k]['img'] = $result[$k]['img'] ? _PS_BASE_URL_.__PS_BASE_URI__.'modules/module-name/upload/'.$result[$k]['img'] : '';
}
$array[$lang["id_lang"]] = array(
'lang_data' => $lang,
'count' => count($result),
'data' => $result
);
}
return $array;
}
HookAjax is called by:
<?php
include(dirname(__FILE__).'/../../config/config.inc.php');
$context = Context::getContext();
$addDesc = Module::getInstanceByName('module-name');
echo $addDesc->hookAjax($_POST['action'],$_POST['id_prodotto'],$_POST['lang'],$_POST['title'], $_POST['text_desc'], NULL, $_POST['row']);
?>
But I struggle with this error:
Fatal error: Call to a member function addJs() on a non-object in {my_site}/classes/helper/HelperUploader.php on line 257
You need to include init.php in HookAjax after including config.inc.php so that controller is initialized in context.
include(dirname(__FILE__).'/../../init.php');
Note that this is just bad practice, respect the MVC and use proper controllers for your AJAX calls and data validation/processing in them, not inside main module class.
I am sending two parameter From=2015-08-01 and To=2015-08-31 is working fine. Then how to also passing parameter (From2=2015-04-01 and To2 =2015-08-31) and getting the both output at same time. Please do not write any other functions. Please advise me.
This is my output.php
$res1=$con->selectnonschoolround1($From,$To);
while($row=pg_fetch_array($res1))
{
$r1=$row['non_slsc_qty'];
$r2=$row['non_slst_qty'];
$r3=$row['non_slot_qty'];
$r4=$row['non_slsc_ben'];
$r5=$row['non_slst_ben'];
$r6=$row['non_slot_ben'];
$Total_qty_non_r1=$row['total_qty'];
$Total_ben_non_r1=$row['total_ben'];
}
This is the class.php
class DB_con
{
function __construct()
{
$db = pg_connect("host=localhost port=5432
dbname=mydb user=postgres password=123");
}
public function selectnonschoolround1($From,$To)
{
$res1=pg_query("SELECT
SUM(non_slsc_qty) as non_slsc_qty,
SUM(non_slst_qty) as non_slst_qty,
SUM(non_slot_qty) as non_slot_qty,
SUM(non_slsc_ben) as non_slsc_ben,
SUM(non_slst_ben) as non_slst_ben,
SUM(non_slot_ben) as non_slot_ben,
SUM(non_slsc_qty+non_slst_qty+non_slot_qty) AS total_qty,
SUM(non_slsc_ben+non_slst_ben+non_slot_ben) AS total_ben
FROM table where date BETWEEN '$From' AND '$To'");
return $res1;
}
}
Pass function parameter as an array and then you need to check if parameter is array or not using PHP function is_array(). If its an array then loop through that array and execute the query with "from" and "to" indexes but, in this case you need to return an array from the executed query. If its not an array the keep the code as it is in your else part. In this you can use the same function for multiple value or for single values. Just keep in mind that the params should be proper. Hope this will help you out :)
Try this code and make the changes as per your need :).
class DB_con
{
function __construct()
{
//code
}
public function selectNonSchoolRound1($arr, $from = '', $to = '')
{
if (is_array($arr)) {
$returnArr = array();
foreach ($arr as $ars) {
$res1 = pg_query("your query using ".$ars['from'].$ars['to']);
array_push($returnArr, $res1);
}
return $returnArr;
} else {
$res1 = pg_query("your query using ".$from.$to.'variabls');
return $res1;
}
}
}
try {
$arr = array('0' =>
array('from' => '2015-08-01', 'to' => '2015-08-31'),
'1' => array('from' => '2015-08-01', 'to' => '2015-08-31')
);
$obj = new DB_con();
$result = $obj->selectNonSchoolRound1($arr);
// loop here with the $result variable
} catch (Exception $ex) {
echo $ex->getMessage();
}
SOLVED
I have route that does a POST route towards store() in the controller.
I'm trying to test if the action is working properly.
Controller:
public function store() {
$d= Input::json()->all();
//May need to check here if authorized
$foo= new Foo;
$d = array();
$d['name'] = $d['name'];
$d['address'] = $d['address'];
$d['nickname'] = $d['nickname'];
if($foo->validate($d))
{
$new_foo= $foo->create($d);
return Response::json(Foo::where('id','=',$new_foo->id)->first(),200);
}
else
{
return Response::json($foo->errors(),400);
}
}
Now I'm trying to test this using a new class called FooTest.php
Here is the function i'm currently trying to do to make the check work:
public function testFooCreation()
{
$jsonString = '{"address": "82828282", "email": "test#gmail.com", "name":"Tester"}';
$json = json_decode($jsonString);
$this->client->request('POST', 'foo');
$this->assertEquals($json, $this->client->getResponse());
}
when I run phpunit in my cmd, I get an error stating that "name" is undefined. I know i'm not actually passing anything to the request so I'm positive that nothing is actually being checked, but my question is how do I actually pass my json strings to check?
Everytime I put the $json inside the client request, it asks for an array, but when I convert my json string to an array, json_decode wants a string.
UPDATE
I was messing around with the passing of input data and I came across this:
$input = [
'name' => 'TESTNAME',
'address' => '299 TESTville',
'nickname' => 't'
];
Input::replace($input);
Auth::shouldReceive('attempt')
->with(array('name' => Input::get('name'),
'address' => Input::get('address'),
'nickname' => Input::get('nickname')))
->once()
->andReturn(true);
$response = $this->call('POST', 'foo', $input);
$content = $response->getContent();
$data = json_decode($response->getContent());
But whenever I run the test, i still get "name:undefined" It's still not passing the input i've created.
$d= Input::json()->all();
The above statement gets Input in $d.
$d = array();
Now the last statement again initialises $d as an empty new array.
So there is no: $['name'] . Hence, Undefined.
I think, that's the problem with the above code.
Hope it helps :)
I was able to pass the input into a POST route from the test.
public function testFooCreation(){
$json = '{"name":"Bar", "address":"FooLand", "nickname":"foobar"}';
$post = $this->action('POST', 'FooController#store', null, array(), array(), array(), $json);
if($this->assertTrue($this->client->getResponse()->isOk()) == true && $this->assertResponseStatus(201)){
echo "Test passed";
}
}
Turns out that in order for me to actually pass input into the controller through test POST, I have to pass it through the 7th parameter.
I hope this helps others.
of course you get an error , just look at your code
$aInputs = Input::json()->all();
//May need to check here if authorized
$foo= new Foo;
$d = array();
$d['name'] = $d['name'];
$d['address'] = $d['address'];
$d['nickname'] = $d['nickname'];
your assigning the array to it self, which is empty