I am trying to make a query using ci4. But I keep getting the same error. I don't know where the cause is. Can anyone help me.
This is my controller
public function search(){
$this->db = \Config\Database::connect();
$user_id= $this->request->getPOST('search');
$sql = "SELECT pay_daftarid.di_id, pay_daftarid.di_nama, pay_daftarid.di_nokp, pay_daftarid.di_telefon, pay_daftarid.di_emel,
pay_daftarid.di_alamat1, pay_daftarid.di_poskod,pay_daftarid.di_kod_bandar, pay_daftarid.di_kod_negeri,
pay_daftarid.di_kod_negara,
hrp_ruj_negara.keterangan as negara,
hrp_ruj_negeri.keterangan as negeri,
hrp_ruj_bandar.nama_bandar
FROM pay_daftarid
JOIN hrp_ruj_negara ON hrp_ruj_negara.kod = di_kod_negara,
JOIN hrp_ruj_negeri ON hrp_ruj_negeri.kod = di_kod_negeri,
JOIN hrp_ruj_bandar ON kod_bandar = di_kod_bandar
WHERE di_nokp = '$user_id'";
$result = $this->db->query($sql);
$data = $result->getRow();
return $this->respond($data);
}
The result should return a one record based on the searched id ($user_id)
And this is my vue.js
get_id(){
var self = this;
$.post(base_url + '/api/seminar/search', {
search: self.search, //self.search
}, function(res){ //res adalah daripada API
self.selected = res.data[0];
console.log(self.contents[0].di_nama);
});
},
And this is my error
> <br />
<b>Fatal error</b>: Uncaught CodeIgniter\Format\Exceptions\FormatException: Failed to parse json string, error: "Type is not supported". in C:\ci4_project\paygate\vendor\codeigniter4\framework\system\Format\JSONFormatter.php:41
Stack trace:
#0 C:\ci4_project\paygate\vendor\codeigniter4\framework\system\Format\JSONFormatter.php(41): CodeIgniter\Format\Exceptions\FormatException::forInvalidJSON('Type is not sup...')
#1 C:\ci4_project\paygate\vendor\codeigniter4\framework\system\API\ResponseTrait.php(348): CodeIgniter\Format\JSONFormatter->format(Array)
#2 C:\ci4_project\paygate\vendor\codeigniter4\framework\system\API\ResponseTrait.php(99): CodeIgniter\Debug\Exceptions->format(Array)
#3 C:\ci4_project\paygate\vendor\codeigniter4\framework\system\Debug\Exceptions.php(132): CodeIgniter\Debug\Exceptions->respond(Array, 500)
#4 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException))
#5 {main}
thrown in <b>C:\ci4_project\paygate\vendor\codeigniter4\framework\system\Format\JSONFormatter.php</b> on line <b>41</b><br />
{
"title": "ErrorException",
"type": "ErrorException",
"code": 500,
"message": "Uncaught CodeIgniter\\Format\\Exceptions\\FormatException: Failed to parse json string, error: \"Type is not supported\". in C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\Format\\JSONFormatter.php:41\nStack trace:\n#0 C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\Format\\JSONFormatter.php(41): CodeIgniter\\Format\\Exceptions\\FormatException::forInvalidJSON('Type is not sup...')\n#1 C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\API\\ResponseTrait.php(348): CodeIgniter\\Format\\JSONFormatter->format(Array)\n#2 C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\API\\ResponseTrait.php(99): CodeIgniter\\Debug\\Exceptions->format(Array)\n#3 C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\Debug\\Exceptions.php(132): CodeIgniter\\Debug\\Exceptions->respond(Array, 500)\n#4 [internal function]: CodeIgniter\\Debug\\Exceptions->exceptionHandler(Object(ErrorException))\n#5 {main}\n thrown",
"file": "C:\\ci4_project\\paygate\\vendor\\codeigniter4\\framework\\system\\Format\\JSONFormatter.php",
"line": 41,
"trace": [
{
"function": "shutdownHandler",
"class": "CodeIgniter\\Debug\\Exceptions",
"type": "->",
"args": []
}
]
}
Related
I'm working on a PHP/Lumen 8.x foreach loop to manage the update/insert of records from a mysql DB table in which I'm trying to catch any errors during the update/insert process so that it can continue with subsequent logs in case of error. As I am with Lumen and we are in a certain namespace environment in the catch statement I use "\Exception" to identify the type of exception. However, in no case in which an error occurs can I catch it and in the controller output, that is, in the controller response, an error indicating "Recursion detected" is triggered. I can't catch this last error either.
This is the code snippet for the foreach(...) { ... try..catch ...} block:
foreach($prdsData as $prod){
$cr_sku = $prod['SKU'];
try{
$res_prod_uc[$cr_sku]['err'] = false;
$res_prod_uc[$cr_sku]['res'] = wc_product_update($pd);
} catch( \Exception $e ){
$res_prod_uc[$cr_sku]['err'] = true;
$cr_err_dt = array(
'code' => $e->getCode(),
'msg' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace()
);
$res_prod_uc[$cr_sku]['err_data'] = $cr_err_dt;
continue;
}
}
return response()->json($res_prod_uc,200); // this is where the "Recursion detected" error is triggered
This is the output (JSON) of the "Recursion detected" error:
{
"message": "Recursion detected",
"exception": "InvalidArgumentException",
"file": "..../blog/vendor/illuminate/http/JsonResponse.php",
"line": 88,
"trace": [
{
"file": "..../blog/vendor/symfony/http-foundation/JsonResponse.php",
"line": 54,
"function": "setData",
"class": "Illuminate\\Http\\JsonResponse",
"type": "->"
},
{
"file": "..../blog/vendor/illuminate/http/JsonResponse.php",
"line": 32,
"function": "__construct",
"class": "Symfony\\Component\\HttpFoundation\\JsonResponse",
"type": "->"
},
{
"file": "..../blog/vendor/laravel/lumen-framework/src/Http/ResponseFactory.php",
"line": 40,
"function": "__construct",
"class": "Illuminate\\Http\\JsonResponse",
"type": "->"
},
/* this is my controller */
{
"file": "..../blog/app/Http/Controllers/ProductsController.php",
"line": 145,
"function": "json",
"class": "Laravel\\Lumen\\Http\\ResponseFactory",
"type": "->"
},....
How can I go about catching any errors?
Thanks in advance
What was happening is exactly what you say #aynber. I did check and, yes, exceptions are being cached, but the line 'trace' => $e->getTraceAsString() does indeed allocate data where at deeper levels there is object recursion. And by the time the thread reaches return response()->json($res,200); (Which is outside of the try..catch block) the "Recursion detected" error is raised. This error is generated by a call to the json_encode() PHP function found within the Lumen 8 framework.
The solution I used was to replace the getTrace() method with the getTraceAsString() Exception object method.
I am using slim framework. But when i refresh the page i got the following error. My all paths are correct but still getting this error.
{
"message": "Slim Application Error",
"exception": [
{
"type": "DI\\DependencyException",
"code": 0,
"message": "Error while injecting dependencies into App\\Action\\User\\UserListDataTableAction: No entry or class found for 'App\\Domain\\User\\Service\\UserListDataTable'",
"file": "/opt/lampp/htdocs/slim/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php",
"line": 147
},
{
"type": "DI\\NotFoundException",
"code": 0,
"message": "No entry or class found for 'App\\Domain\\User\\Service\\UserListDataTable'",
"file": "/opt/lampp/htdocs/slim/vendor/php-di/php-di/src/Container.php",
"line": 135
}
]
}
<?php
namespace App\Action\User;
use App\Domain\User\Service\UserListDataTable;
use App\Responder\JsonResponder;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
final class UserListDataTableAction
{
private $userListDataTable;
private $responder;
public function __construct(
UserListDataTable $userListDataTable,
JsonResponder $responder
) {
$this->userListDataTable = $userListDataTable;
$this->responder = $responder;
}
}
Please guide me where i am wrong.
Any solution appreciated!
I have the problem to use Slim/Flash/Messages, to do a flash message. I have this error
Fatal error:
Uncaught DI\NotFoundException: No entry or class found for 'Slim\Flash' in C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Container.php:119
Stack trace:
#0 C:\laragon\www\cart\app\container.php(52): DI\Container->get('Slim\\Flash')
#1 [internal function]: DI\Definition\Source\DefinitionFile->{closure}(Object(DI\Container)) #2 C:\laragon\www\cart\vendor\php-di\invoker\src\Invoker.php(82): call_user_func_array(Object(Closure), Array)
#3 C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Definition\Resolver\FactoryResolver.php(81): Invoker\Invoker->call(Object(Closure), Array)
#4 C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Definition\Resolver\ResolverDispatcher.php(58): DI\Definition\Resolver\FactoryResolver->resolve(Object(DI\Definition\FactoryDefinition), Array)
#5 C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Container.php(285): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\FactoryDefinition), Array)
#6 C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Container.php(122): DI\Contai in C:\laragon\www\cart\vendor\php-di\php-di\src\DI\Container.php on line 119
In the bootstrap/app.php
$container->set('flash', function($container) {
return new \Slim\Flash\Messages($container);
});
in the container.php
Twig::class => function (ContainerInterface $c) {
$twig = Factory::getEngine();
$twig->addExtension(new TwigExtension(
$c->get('router'),
$c->get('request')->getUri()
));
$twig->getEnvironment()->addGlobal('basket', $c->get(Basket::class));
$twig->getEnvironment()->addGlobal('auth', $c->get(Auth::class));
$twig->getEnvironment()->addGlobal('user', $c->get(Customer::class));
$twig->getEnvironment()->addGlobal('flash', $c->get(Flash::class));
return $twig;
},
$twig->getEnvironment()->addGlobal('flash', $c->get(Flash::class)); means that you're looking for a key called Slim\Flash within the container, but you registered it with the key flash.
Therefore change:
$twig->getEnvironment()->addGlobal('flash', $c->get(Flash::class));
to
$twig->getEnvironment()->addGlobal('flash', $c->get('flash'));
Alternatively, you can use the fully qualified class name everywhere:
app.php:
use Slim\Flash\Messages as Flash;
$container->set(Flash::class, function($container) {
return new Flash($container);
});
and add this to the top of container.php:
use Slim\Flash\Messages as Flash;
the following code is causing Uncaught exception:
class visual
echo(ProdutoController::listaProdutoVitrineFiltroOrdenadoPeloMenor("TV", "nome", "precodevenda", "ASC"));
class controller
$produtos = daogenerico::findByStringOrderBy("Produto", $tipopesquisa, $nome, $parametro, $ordem);
dao
public static function findByStringOrderBy($classname, $field, $string, $order, $orderparam){
$bd = new bd();
$bd->conectar();
$result = $bd->getEntityManager()->getRepository($classname)->createQueryBuilder('e')
->where('e.'.$field.' LIKE :'.$field)
->setParameter($field, '%'.$string.'%')
->orderBy($order, $orderparam)
->getQuery()
->getResult();
return $result;
}
error:
Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message 'SELECT e FROM Produto e WHERE e.nome LIKE :nome ORDER BY precodevenda ASC' in C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php:41 Stack trace: #0 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(483): Doctrine\ORM\Query\QueryException::dqlError('SELECT e FROM P...') #1 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(708): Doctrine\ORM\Query\Parser->semanticalError(''precodevenda' ...', Array) #2 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(283): Doctrine\ORM\Query\Parser->processDeferredResultVariables() #3 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php(351): Doctrine\ORM\Query\Parser->getAST() #4 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query.php(281): Doctrine\ORM\Query\Parser->parse() #5 C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctri in C:\xampp\htdocs\TrabalhoA2\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php on line 63
Somebody can help?
Seems like entity alias 'e' is missing in front of order column 'precodevenda'.
Try replacing:
->orderBy($order, $orderparam)
with
->orderBy('e.'.$order, $orderparam)
I have a method where I've set the type of first argument to be passed (array):
public function create(array $values, $options=array())
{
// set datetime columns
if(! isset($values['date_created']) or empty($values['date_created'])) {
$values['date_created'] = date("Y-m-d H:i:s");
}
if(! isset($values['date_updated']) or empty($values['date_updated'])) {
$values['date_updated'] = date("Y-m-d H:i:s");
}
return parent::create($values);
}
How do I test this using PHPUnit, it keeps throwing a fatal error which I guess is to be expected but it doesn't allow my script to run:
1) UserTableTest::testCreateMethodWithInvalidArguments
Failed asserting that exception of type "PHPUnit_Framework_Error" matches expected exception "InvalidArgumentException". Message was: "Argument 1 passed to app\models\UserTable::create() must be of the type array, string given, called in /var/www/phpdev/tests/models/UserTableMockTest.php on line 82 and defined" at
#0 /var/www/phpdev/app/models/UserTable.php(16): PHPUnit_Util_ErrorHandler::handleError(4096, 'Argument 1 pass...', '/var/www/phpdev...', 16, Array)
#1 /var/www/phpdev/tests/models/UserTableMockTest.php(82): app\models\UserTable->create('invalid argumen...')
#2 [internal function]: UserTableTest->testCreateMethodWithInvalidArguments()
#3 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(962): ReflectionMethod->invokeArgs(Object(UserTableTest), Array)
#4 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(826): PHPUnit_Framework_TestCase->runTest()
#5 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase->runBare()
#6 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(760): PHPUnit_Framework_TestResult->run(Object(UserTableTest))
#7 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestSuite.php(699): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#8 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(426): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#9 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(179): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#10 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(132): PHPUnit_TextUI_Command->run(Array, true)
#11 /var/www/phpdev/vendor/phpunit/phpunit/phpunit(55): PHPUnit_TextUI_Command::main()
#12 {main}.
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Should I instead remove the type hint and use is_array($values) to tell whether the correct type is passed then throw an exception? In PHPUnit I am expecting an exception is thrown anyway. By the way, below is my test:
/**
* #expectedException InvalidArgumentException
*/
public function testCreateMethodWithInvalidArguments()
{
// pass invalid argument, should be an array of values
$result = $this->userTable->create('invalid argument type');
}
PHPUnit just has to be made aware that you are expecting and exception so it can properly handle it and report upon it.
public function testExceptionHasRightMessage()
{
$this->setExpectedException(
'InvalidArgumentException', 'Right Message'
);
throw new InvalidArgumentException('Some Message', 10);
}
This was pulled from the example 2.11 at PHP Unit Documentation - Writing Unit Tests