I get the following fatal error while calling find method of entityRepository in a custom entityRepository class
Fatal error: Uncaught exception 'Doctrine\ORM\OptimisticLockException' with message 'Cannot obtain optimistic lock on unversioned entity Entities\Comment' in C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\OptimisticLockException.php:62 Stack trace: #0 C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\EntityRepository.php(140): Doctrine\ORM\OptimisticLockException::notVersioned('Entities\Commen...') #1 C:\Users\user\Desktop\projects\interview\application\models\Repositories\CommentRepository.php(24): Doctrine\ORM\EntityRepository->find('Entities\Commen...', 1) #2 C:\Users\user\Desktop\projects\interview\application\controllers\CommentController.php(65): Repositories\CommentRepository->activateByIds(Array) #3 [internal function]: CommentController->approveComments() #4 C:\Users\user\Desktop\projects\interview\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #5 C:\Users\user\Desktop\projects\interview\index.php(203): require_once('C:\Users\user\D...') in C:\Users\user\Desktop\projects\interview\application\libraries\Doctrine\ORM\OptimisticLockException.php on line 62
Here is method in which i call find
public function activateByIds($arrayOfIds){
if (count($arrayOfIds)>=1) {
for ($i=0; $i<count($arrayOfIds); $i++){
$comment = parent::find('Entities\Comment', $arrayOfIds[$i]);
$comment->setIsactive(1);
$this->_em->merge($comment);
$this->_em->flush();
}
return true;
}
else return false;
}
What i'm doing wrong??
From what i read you have an OptimisticLockException
As said in this documentation:
An OptimisticLockException is thrown when a version check on an object
that uses optimistic locking through a version field fails.
You can find out more about Optimistic lock here
My guess is that their is a conflict with the $comment variable:
the first time you initialize $comment ($i=0) comment#1 is loaded
the second time (i=1, you find comment#2 but comment is already an entity and is manged) $comment =... tries to give comment#1 the values of comment#2 even the id that is uniq, so you are creating a conflict.
try this instead :
public function activateByIds($arrayOfIds){
$comments =array();
if (count($arrayOfIds)>=1) {
foreach($arrayOfIds as $i=>$id){
$comments [$i] = $this->getEntityManager()->find('Entities\Comment', $id); //new comment, not the old one!
$comments [$i]->setIsactive(1);
$this->_em->merge($comments[$i]);
$this->_em->flush();
}
return true;
}
else return false;
unset ($comments);
}
That way you are sure that you are not trying to re-use the previous comment instead of a new one.
Related
when accessing the www.hostname/phpmyadmin/ page i get the blank page. Tried to reinstall PHP,HTTPD and MYSQL.
After running php index.php in the phpmyadmin folder I get the following error:
[root#ansible1 phpmyadmin]# php index.php
PHP Fatal error: Uncaught Error: Call to a member function getCookie() on null in /usr/share/phpmyadmin/libraries/classes/Url.php:219
Stack trace:
#0 /usr/share/phpmyadmin/libraries/classes/Url.php(171): PhpMyAdmin\Url::getCommonRaw(Array, '?')
#1 /usr/share/phpmyadmin/libraries/classes/Core.php(762): PhpMyAdmin\Url::getCommon(Array)
#2 /usr/share/phpmyadmin/libraries/classes/Core.php(338): PhpMyAdmin\Core::linkURL('https://secure....')
#3 /usr/share/phpmyadmin/libraries/classes/Core.php(364): PhpMyAdmin\Core::getPHPDocLink('book.json.php')
#4 /usr/share/phpmyadmin/libraries/classes/Core.php(1006): PhpMyAdmin\Core::warnMissingExtension('json', true)
#5 /usr/share/phpmyadmin/libraries/common.inc.php(110): PhpMyAdmin\Core::checkExtensions()
#6 /usr/share/phpmyadmin/index.php(23): require_once('/usr/share/phpm...')
#7 {main}
thrown in /usr/share/phpmyadmin/libraries/classes/Url.php on line 219
On the line 219 in the corresponding file:
public static function getCommonRaw($params = [], $divider = '?')
{
/** #var Config $PMA_Config */
global $PMA_Config;
$separator = Url::getArgSeparator();
// avoid overwriting when creating navi panel links to servers
if (isset($GLOBALS['server'])
&& $GLOBALS['server'] != $GLOBALS['cfg']['ServerDefault']
&& ! isset($params['server'])
&& ! $PMA_Config->get('is_setup')
) {
$params['server'] = $GLOBALS['server'];
}
if (empty($PMA_Config->getCookie('pma_lang')) && ! empty($GLOBALS['lang'])) {
$params['lang'] = $GLOBALS['lang'];
}
$query = http_build_query($params, '', $separator);
if ($divider != '?' || strlen($query) > 0) {
return $divider . $query;
}
Not sure what I am doing wrong but havent found this error on the internet....
The blowfish secret is set:
$cfg['blowfish_secret'] = 'H2OxcGXxflSd8JwrwVlh6KW6s2rER63i';
Any idea what could I be missing? Even a hint what direction to look will be appreciated.
EDIT1:
Downgraded to version 4.9.4 - getting the similar error:
So after downgrading to version 4.9.4 I get the very similar....
[root#ansible1 phpmyadmin]# php index.php
PHP Fatal error: Uncaught Error: Call to a member function getCookie() on null in /usr/share/phpmyadmin/libraries/classes/Url.php:217
Stack trace:
#0 /usr/share/phpmyadmin/libraries/classes/Url.php(169): PhpMyAdmin\Url::getCommonRaw(Array, '?')
#1 /usr/share/phpmyadmin/libraries/classes/Core.php(749): PhpMyAdmin\Url::getCommon(Array)
#2 /usr/share/phpmyadmin/libraries/classes/Core.php(330): PhpMyAdmin\Core::linkURL('https://secure....')
#3 /usr/share/phpmyadmin/libraries/classes/Core.php(353): PhpMyAdmin\Core::getPHPDocLink('book.json.php')
#4 /usr/share/phpmyadmin/libraries/classes/Core.php(987): PhpMyAdmin\Core::warnMissingExtension('json', true)
#5 /usr/share/phpmyadmin/libraries/common.inc.php(106): PhpMyAdmin\Core::checkExtensions()
#6 /usr/share/phpmyadmin/index.php(27): require_once('/usr/share/phpm...')
#7 {main}
thrown in /usr/share/phpmyadmin/libraries/classes/Url.php on line 217
The page now shows: Access Denied - simply written, no pictures or anything.
What am I missing? :/
[Browser view1
I don't know if my previous post is visible, so I post it again:
I had the same error message, and row #4 says: "PhpMyAdmin\Core::warnMissingExtension('json', true)".
After installing php-json package, problem was solved for me.
I am trying to submit a comment on a guestbook application based on the Yii 2 Framework. On localhost on my PC works everything fine, but on the shared hosting when I want to submit a comment in View, I get this error.
Here is the error:
An error occurred while handling another error:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))
#4 {main}
Previous exception:
exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /home/mahdikas/public_html/guestbook/controllers/PostController.php on line 117.' in /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php:366
Stack trace:
#0 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders()
#1 /home/mahdikas/public_html/guestbook/vendor/yiisoft/yii2/base/Application.php(392): yii\web\Response->send()
#2 /home/mahdikas/public_html/guestbook/web/index.php(12): yii\base\Application->run()
#3 {main}
In the postController I have this code:
public function actionAdd_comment()
{
//print_r($_POST);
$model = new \app\models\Comments;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
echo 'Thanks for your comment.';
} else {
echo 'Failed!';
}
}
}
which line 117 in the error is:
echo 'Thanks for your comment.';
How can I solve this problem?
Since Yii 2.0.14 you cannot echo in a controller. A response must be returned:
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->comment_date = date('Y-m-d H:i:s');
if ($model->save()) {
return 'Thanks for your comment.';
} else {
return 'Failed!';
}
}
}
You may also call exit at the end of your method to prevent further processing or wrap your code with ob_start() and ob_get_clean(), if you're not able to avoid echo.
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$this->someMagicWithEcho();
exit;
}
}
or
public function actionAdd_comment() {
$model = new \app\models\Comments();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
ob_start();
$this->someMagicWithEcho();
return ob_get_clean();
}
}
Although I accept rob006's solution as correct, I have encountered a situation where there was no echo in the controller, but I also got the error. After going through several sites looking for a solution, I discovered an alternative.
You can check the php.ini file and ensure the output buffer is enabled. If not, you can enable it by adding this line in php.ini if it does not exist:
output_buffering = on
And turn it off for just the script - the script where it is not required by either...
calling ob_end_flush(), or
calling ob_end_clean()
I've attempted to extend the Phalcon\Db\Adapter\Pdo\Mysql to create a log every time a query function returns false.
Even though I'm not creating any new connections except the one in parent::__construct I'm getting the following exception:
Fatal error: Uncaught PDOException: SQLSTATE[08004] [1040] Too many connections in ...Internal/Database/Mysql.php:14 Stack trace: #0 [internal function]: PDO->__construct('mysql:adapter=M...', '...', '...', Array) #1 [internal function]: Phalcon\Db\Adapter\Pdo->connect(Array) #2 .../Internal/Database/Mysql.php(14): Phalcon\Db\Adapter\Pdo->__construct(Array) #3 .../apps/bootstrap/app.php(378): Internal\Database\Mysql->__construct(Array) #4 [internal function]: Closure->{closure}() #5 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault)) #6 .../apps/bootstrap/core_services.php(7): Phalcon\Di->get('logs') #7 [internal function]: Closure->{closure}() #8 [internal function]: Phalcon\Di\Service->resolve(NULL, Object(Phalcon\Di\FactoryDefault)) #9 .../apps/libs/Internal/Database/Mysql.php(15): Phalcon\Di->get('logger') #10 .../apps/bootstrap/app.php(37 in .../apps/libs/Internal/Database/Mysql.php on line 14
Code:
namespace Internal\Database;
use Phalcon\Db\Adapter\Pdo\Mysql as PhalconMysql;
use Phalcon\Di;
class Mysql extends PhalconMysql
{
public $isLogger = false;
public function __construct(array $descriptor)
{
parent::__construct($descriptor);
$this->oLogger = Di::getDefault()->get('logger');
}
public function query($sqlStatement, $bindParams = null, $bindTypes = null)
{
$oResult = parent::query($sqlStatement, $bindParams, $bindTypes);
if ($oResult === false && $this->isLogger === false) {
$trace = debug_backtrace();
$aCaller = array_shift($trace);
$sFile = $aCaller['file'];
$sLine = $aCaller['line'];
$this->oLogger->error('MySQL query failed. File: ' . $sFile . ', Line: ' . $sLine, ['error' => $this->getErrorInfo()]);
}
return $oResult;
}
}
The line that triggers the error is parent::__construct($descriptor);.
The only other change I've made was to replace the usage of Phalcon\Db\Adapter\Pdo\Mysql with Internal\Database\Mysql. All the connections are created in the same way they were when I were using Phalcon\Db\Adapter\Pdo\Mysql.
I've looked at the parent classes of Phalcon\Db\Adapter\Pdo\Mysql. The only place I see the connection created is here
I'd very much appreciate your help with this issue.
It is not a right thing to extend classes in phalcon, that are actually interacting with other php extensions. Learned it the hard way.
The easy way I would recommend is to override Mvc/Model class as a eg. ModelCommon or BaseModel, and implement there Model::initialize and Model::afterFetch.
But a proper way would to be to use event mechanism developed out in Phalcon. The trick is to attach to beforeQuery and/or afterQuery events.
Shortcut for that, done in DI:
$di->set('db', function() use ($di) {
$config = $di->getConfig();
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
// ...
]);
$eventsManager->attach('db', function($event, $connection, $params) use ($config) {
if ($event->getType() == 'beforeQuery') {
// ...
}
if ($event->getType() == 'afterQuery') {
// ...
}
});
$connection->setEventsManager($eventsManager);
return $connection;
});
Looks like the module is trying to connect to database more than once. Perhaps make sure you initialise database service just once in your Module.php
Maybe you can db service as shared when you declare db in $di
$di->setShared('db', function() {
// return your db instance
});
Problem solved, although I still don't understand why it occurred in the first place.
The logger logs to the database, but since it's taken from Di I assumed it reuses existing connection. Removing that line from constructor, and putting it right after the logger is called solved the problem.
I need to verify the existence of a method in a controller from a model with codeigniter 2.0.2. and HMVC.
I'm trying to do with ReflectionClass:hasMethod(), without success.
My code in Model:
function hasPanel($controller){
$rc = new ReflectionClass($controller);
if($rc::hasMethod("panel_base")){
return true;
}
return false;
}
And the unwelcome error:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class administracion does not exist' in D:\xampp\htdocs\sea\application\models\auth\permisos.php:368
Stack trace:
#0 D:\xampp\htdocs\sea\application\models\auth\permisos.php(368): ReflectionClass->__construct('administracion')
#1 D:\xampp\htdocs\sea\application\models\auth\permisos.php(357): Permisos->hasPanel('administracion')
#2 D:\xampp\htdocs\sea\application\controllers\auth\identificar.php(101): Permisos->getControladores('administracion')
#3 [internal function]: Identificar->modulo()
#4 D:\xampp\htdocs\sea\application\core\Admin_controller.php(317): call_user_func_array(Array, Array)
#5 D:\xampp\htdocs\sea\system\core\CodeIgniter.php(305): Admin_controller->_remap('modulo', Array)
#6 D:\xampp\htdocs\sea\www\index.php(252): require_once('D:\xampp\htdocs...')
#7 {main}
thrown in D:\xampp\htdocs\sea\application\models\auth\permisos.php on line 368
EDITED
This solves the above...
function hasPanel($controller,$route){
include_once($route);
$rc = new ReflectionClass($controller);
if($rc::hasMethod("panel_base")){
return true;
}
return false;
}
But causes this:
Fatal error: Non-static method ReflectionClass::hasMethod() cannot be called statically, assuming $this from incompatible context in D:\xampp\htdocs\sea\application\models\auth\permisos.php on line 373
Some idea?
I'm not entirely sure, but this might be as simple as $rc->hasMethod("panel_base"); You did instantiate a new class. . .
Change
$rc::hasMethod("panel_base")
to
$rc->hasMethod("panel_base")
I using Zend Gdata library for search video Youtube API.
If I make a bad request, then a zend exception..
Example:
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' in D:\Webserver\domains\loc\controller\Zend\Gdata\App.php:710
Stack trace:
#0 D:\Webserver\domains\loc\controller\Zend\Gdata.php(221): Zend_Gdata_App->performHttpRequest('GET', 'http://gdata.yo...', Array, NULL, NULL, NULL)
#1 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(876): Zend_Gdata->performHttpRequest('GET', 'http://gdata.yo...', Array)
#2 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(764): Zend_Gdata_App->get('http://gdata.yo...', NULL)
#3 D:\Webserver\domains\loc\controller\Zend\Gdata\App.php(220): Zend_Gdata_App->importUrl('http://gdata.yo...', 'Zend_Gdata_YouT...', NULL)
#4 D:\Webserver\domains\loc\controller\Zend\Gdata.php(187): Zend_Gdata_App->getEntry('http://gdata.yo...', 'Zend_Gdata_YouT...')
#5 D:\Webserver\domains\loc\controller\Zend\Gdata\YouTube.php(293): Zend_Gdata->getEntry('http://gdata.yo...', 'Zend_Gdata_YouT...')
#6 D:\Webserver\domains\loc\controller\Yo in D:\Webserver\domains\loc\controller\Zend\Gdata\App.php on line 710
But I do not want users of my site have seen this error ... I need to catch this error and cause no Exception and just want to get the text of the error .. What should I do?
Put the code that causes the exception into a try/catch statement
try {
// code calling Zend_Gdata_App->performHttpRequest
} catch(Zend_Gdata_App_HttpException $e) {
$message = $e->getMessage();
// do something with $message now
}
More information: http://de3.php.net/manual/en/language.exceptions.php