Whats wrong with my codes, I always got fatal error when I change my URL. I can't remove those if else below which have undefined, I will not get 2nd URL. I am beginner creating MVC please help
class event_trap
{
function __construct()
{
$url = $_GET['url'];
$url = rtrim($url. '/');
$url = explode('/', $url);
//print_r($url);
$file = 'event_mvc/controllers/' .$url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
require 'event_mvc/controllers/error.php';
$controller = new Error();
return false;
}
$controller = new $url[0];
if (isset($url[2])) {
$controller->{$url[1]}($url[2]); //<-- Undefined method
} else {
if(isset($url[1])) {
$controller -> {$url[1]}(); //<-- Undefined method
} else {
}
}
}
}
var_dump($url)
array (size=2)
0 => string 'event' (length=5)
1 => string '' (length=0)
Fatal error: Call to undefined method event::() in
C:\wamp\www\tabulation\event_mvc\libs\Bootstrap.php on line 33
Call Stack
# Time Memory Function Location
1 0.0004 142728 {main}( ) ..\event.php:0
2 0.0012 149096 event_trap->__construct( ) ..\event.php:4
Your variable $url is an array which you use to create an object and call methods with or without parameters. With the array ou have var_dumped 0=>event, 1=>'' you are creating an object of class event and call the method '' which does not exist. Correct your code or pass a valid function name.
You can try this which checks for empty string too.
if (isset($url[2]) && !empty($url[2]) {
$controller->{$url[1]}($url[2]);
} else {
if(isset($url[1]) && !empty($url[1]) {
$controller -> {$url[1]}();
}
}
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
How to solve this problem with call_user_func
When I call the function I have this error generated by php. Below the error.
Warning: call_user_func() expects parameter 1 to be a valid callback, function 'getChildsInMenuCount' not found or invalid function name i
the line inside my files with the function
while ($Qcategories->fetch() ) {
$categories_count++;
$rows++;
if ((!isset($_GET['cID']) && !isset($_GET['pID']) || (isset($_GET['cID']) && ((int)$_GET['cID'] === $Qcategories->valueInt('id')))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {
$category_childs = ['childs_count' => AdministratorMenu::getChildsInMenuCount($Qcategories->valueInt('id'))];
$cInfo_array = array_merge($Qcategories->toArray(), $category_childs);
$cInfo = new objectInfo($cInfo_array);
}
The result of var_dump(__FUNCTION__); is string(20)"getChildsInMenuCount"
class AdministratorMenu {
// Count how many subcategories exist in a category
public static function getChildsInMenuCount($id) {
$OSCOM_Db = Registry::get('Db');
$categories_count = 0;
$Qcategories = $OSCOM_Db->prepare('select id
from :table_administrator_menu
where parent_id = :parent_id
');
$Qcategories->bindInt(':parent_id', $id );
$Qcategories->execute();
while ($Qcategories->fetch() !== false) {
$categories_count++;
$categories_count += call_user_func(__FUNCTION__, $Qcategories->valueInt('id'));
}
return $categories_count;
}
}
Since it's a class method, you need to use __METHOD__, not __FUNCTION__. This will include the class prefix.
$categories_count += call_user_func(__METHOD__, $Qcategories->valueInt('id'));
change call_user_func parameters like below:
call_user_func(__CLASS__ . '::' . __FUNCTION__, $Qcategories->valueInt('id'));
You must also specify the class in first parameter of call_user_func
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);
I have a MongoDBException:
"Invalid find by call Bundle\Document\Property::$fieldName
(criteriaWith)".
I don't understand what is wrong here. Can someone help me please?
Here's the Stack Trace:
1 . in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php at line 38
public static function invalidFindByCall($documentName, $fieldName, $method) {
return new self(sprintf('Invalid find by call %s::$fieldName (%s)', $documentName, $fieldName, $method));
}
at
MongoDBException ::invalidFindByCall ('\Bundle\Document\Property',
'criteriaWith', 'findByCriteriaWith')
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php at line 222
if ($this->class->hasField($fieldName)) {
return $this->$method(array($fieldName => $arguments[0]));
} else {
throw MongoDBException::invalidFindByCall($this->documentName, $fieldName, $method . $by);
}
at DocumentRepository ->__call ('findByCriteriaWith', array(array('name' => 'ho'))) in src/Bundle/Controller/PropertyController.php at line 286
else {
criteria['name'] = $name;
$entities = $repository->findByCriteriaWith($criteria);
}
The Log Messages:
CRITICAL request Uncaught PHP Exception
Doctrine\ODM\MongoDB\MongoDBException: "Invalid find by call
Bundle\Document\Property::$fieldName (criteriaWith)" at
vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/MongoDBException.php
line 38 Context: { "exception":
"Object(Doctrine\ODM\MongoDB\MongoDBException)" }
Let's go through ODM's document repository magic __call method (as that's what is called since findByCriteriaWith does not exist) and highlight interesting parts.
First we see
if (strpos($method, 'findBy') === 0) {
$by = substr($method, 6, strlen($method));
$method = 'findBy';
}
Your method name starts with "findBy" so we will enter this block leaving us later with $method = 'findBy' and $by = 'CriteriaWith' Later on $by gets camelized, as that's standard for field/property name, and leaves us with $fieldName = 'criteriaWith'. Now next thing we see is $this->class->hasField($fieldName) check which results in exception you got because your class does not have a criteriaWith field.
Summing up $repository->findBySomething($value); is equivalent to $repository->findBy(['something' => $value]) and throws exception if field does not exist in your doocument.
I am have written a helper function to "cleanup" callback variables for input into MySQL. This is the function that I wrote:
public function string($object, $objectPath) {
if (!empty($object->$objectPath) || $object->$objectPath !== '') {
$value = $object->$objectPath;
} else {
return 'NULL';
}
if (!empty($value) || $value != '') {
return "'".str_replace("'","''",$value)."'";
} else {
return 'NULL';
}
}
Now, $object is always an object returned by the call, and $objectPath is always a string to points to a given value. Here's where the problem comes in. This works:
$value = $this->db->string($object, 'foo');
However, this does not work:
$value = $this->db->string($object, 'foo->bar->foo1->bar1');
Whenever $objectPath is more than "one layer" deep, I get the following error from (Amazon's) client library:
Fatal error: Call to undefined method MarketplaceWebServiceOrders_Model_Order::getFoo->Bar() in /path/to/Model.php on line 63
The code block that the error refers to is this:
public function __get($propertyName)
{
$getter = "get$propertyName";
return $this->$getter(); // this is line 63
}
$object is not XML, so I can't use SimpleXMLElement and XPath.
What is the problem with my code? Is it that am I concatenating an object and a string? If so, how can I make that possible? How can I get this function to do what I intended it to do?
By the way, I'm using PHP 5.4.27.
PHP doesn't automatically resolve a string containing multiple path levels to children of an object like you are attempting to do.
This will not work even if $obj contains the child hierarchy you are expecting:
$obj = ...;
$path = 'level1->level2->level3';
echo $obj->$path; // WRONG!
You would need to split up the path and "walk" through the object trying to resolve the final property.
Here is an example based on yours:
<?php
$obj = new stdClass();
$obj->name = 'Fred';
$obj->job = new stdClass();
$obj->job->position = 'Janitor';
$obj->job->years = 4;
print_r($obj);
echo 'Years in current job: '.string($obj, 'job->years').PHP_EOL;
function string($obj, $path_str)
{
$val = null;
$path = preg_split('/->/', $path_str);
$node = $obj;
while (($prop = array_shift($path)) !== null) {
if (!is_object($obj) || !property_exists($node, $prop)) {
$val = null;
break;
}
$val = $node->$prop;
// TODO: Insert any logic here for cleaning up $val
$node = $node->$prop;
}
return $val;
}
Here it is working: http://3v4l.org/9L4gc
With #itsmejodie's help, I finally got a working solution:
public function string($node, $objectPath) {
$value = NULL;
$path = explode('->', $objectPath);
while (($prop = array_shift($path)) !== NULL) {
if (!$node->$prop) {
break;
}
$value = $node->$prop;
$node = $node->$prop;
}
if (is_string($value)) {
return "'".str_replace("'","''",$value)."'";
} else {
return 'NULL';
}
}
The key for me was to see that, as #itsmejodie put it, "PHP doesn't automatically resolve a string containing multiple path levels to children of an object." In a string like, 'foo->bar->foo1->bar2', PHP won't convert the ->'s into the T_OBJECT_OPERATOR, thus appending a string to an object, e.g., $object->foo->bar->foo1->bar2, just won't work.