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)
Related
yii2 advanced
I have created a Controller - console/components/OneController.php
//One
namespace console\controllers;
use Yii;
use yii\console\Controller;
Class OneController extends Controller{
public function actionIndex(){
echo 'one!';
return true;
}
}
//command in console:
php yii one
//result:
one!
its working!
I am creating my folder for my test controllers - components
I am creating my Controller - console/components/TwoController.php
//Two
namespace console\controllers;
use Yii;
use yii\console\Controller;
Class TwoController extends Controller{
public function actionIndex(){
echo 'two!';
return true;
}
}
console\config\main-local.php
'controllerMap' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures',
],
'two' => [
'class' => 'yii\console\components\TwoController',//++
],
],
//command in console:
php yii two
its not working. How to run my Controller in my Folder?
Administrator#WIN-XXXXXXXXXXX c:\Administrator\OpenServer\OpenServer\domains\mysite.us
$ php yii two
An Error occurred while handling another error:
exception 'ReflectionException' with message 'Class yii\console\components\TwoController does not exist' in C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\di\Container.php:424
Stack trace:
#0 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\di\Container.php(424): ReflectionClass->__construct('yii\\console\\com...')
#1 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\di\Container.php(364): yii\di\Container->getDependencies('yii\\console\\com...')
#2 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('yii\\console\\com...', Array, Array)
#3 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\BaseYii.php(344): yii\di\Container->get('yii\\console\\com...', Array, Array)
#4 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\base\Module.php(578): yii\BaseYii::createObject(Array, Array)
#5 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\UnknownCommandException.php(79): yii\base\Module->createController('test')
#6 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\ErrorHandler.php(35): yii\console\UnknownCommandException->getSuggestedAlternatives()
#7 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\console\ErrorHandler->renderException(Object(yii\console\UnknownCommandException))
#8 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\console\UnknownCommandException))
#9 {main}
Previous exception:
exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "two".' in C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\base\Module.php:532
Stack trace:
#0 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\Application.php(180): yii\base\Module->runAction('two', Array)
#1 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('two', Array)
#2 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\base\Application.php(380): yii\console\Application->handleRequest(Object(yii\console\Request))
#3 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\yii(27): yii\base\Application->run()
#4 {main}
Next exception 'yii\console\UnknownCommandException' with message 'Unknown command "two".' in C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\Application.php:183
Stack trace:
#0 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\console\Application.php(147): yii\console\Application->runAction('two', Array)
#1 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\vendor\yiisoft\yii2\base\Application.php(380): yii\console\Application->handleRequest(Object(yii\console\Request))
#2 C:\Administrator\OpenServer\OpenServer\domains\mysite.us\yii(27): yii\base\Application->run()
#3 {main}
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;
So I have an App\bindings.php file that I have added to my composer.json file as so:
"autoload": {
"files": [
"app/bindings.php"
]
},
In this file I am trying to set up bindings for my repositories like so:
<?php
function getRepoBinding($id)
{
$repo = "{$id}Repository";
$clientName = strtoupper(config('client.name'));
$implementation = config('app.repo_implementation', 'Eloquent');
$clientOverride = "App\Overrides\\{$clientName}\Repositories\\{$implementation}\\{$repo}";
$repo = class_exists($clientOverride) ? $clientOverride : "App\Repositories\\{$implementation}\\{$repo}";
return $repo;
}
// Repository Interface Bindings
dd(getRepoBinding('Contribution'));
App::bind('ContributionIneterface', getRepoBinding('Contribution'));
However, i run a composer dump-auto and try to run my application and I get the following error:
Fatal error: Uncaught Error: Call to a member function make() on null in /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:54
Stack trace: #0 /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(158): app('config')
#1 /home/vagrant/Code/famsapi/app/bindings.php(9): config('client.name')
#2 /home/vagrant/Code/famsapi/app/bindings.php(16): App\getRepoBinding('Contribution')
#3 /home/vagrant/Code/famsapi/vendor/composer/autoload_real.php(55): require('/home/vagrant/C...')
#4 /home/vagrant/Code/famsapi/vendor/composer/autoload_real.php(45): composerRequire03fe235c8b3156f0c5fcebbc0d696734('90f93262f3a0ac8...', '/home/vagrant/C...')
#5 /home/vagrant/Code/famsapi/vendor/autoload.php(7): ComposerAutoloaderInit03fe235c8b3156f0c5fcebbc0d696734::getLoader()
#6 /home/vagrant/Code/famsapi/bootstrap/autoload.php(17): require('/home/vagrant/C...')
#7 /home/vagrant/Code/famsapi/public/index.php(22): require('/home/vagrant/C...')
#8 {main} thrown in /home/vagrant/Code/famsapi/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 54
It seems like it is having with the global config helper method...but im not sure how to fix that. Better yet, if you have an idea of how I can do this another way I am all ears.
It is because you are loading that file before the application starts.
Use it inside your AppServiceProvider
I'm using Zend Framework 2 and trying to create console command for filling db config
php public/index.php env install stable --dbu=root --dbp=root --dbn=panel100
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'username'#'localhost' (using password: YES)' in /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
Stack trace:
#0 /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(43): PDO->__construct('mysql:host=loca...', 'username', 'password', Array)
#1 /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php(45): Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=loca...', 'username', 'password', Array)
#2 /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(360): Doctrine\DBAL\Driver\PDOMySql\Driver->connect(Array, 'username', 'password', Array)
#3 /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(429): Doctrine\DBAL\Connection->connect()
#4 /home/seyfer/www/site.loc/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(3 in /home/seyfer/www/site.loc/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 946
So no I have chicken and egg problem. Command fails because there is no DB config, but command should create this config.
How to not initialize services when call this command?
My controller code is
use Zend\Mvc\Controller\AbstractActionController;
use ZendMover\Copier;
class EnvController extends AbstractActionController
{
public function installAction()
{
$env = $this->getRequest()->getParam('env');
$dbu = $this->getRequest()->getParam('dbu');
$dbp = $this->getRequest()->getParam('dbp');
$dbn = $this->getRequest()->getParam('dbn');
try {
$environment = new Environment($env, new Copier());
$result = $environment->installEnv($env, $dbu, $dbp, $dbn);
return $result ? "Success\n" : '';
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
}
}
I was solved it with Symfony Console component.
But how to solve it using Zend Console?
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