Using Laradock (basically a set og Docker images for Laravel development), I keep getting Class 'MongoId' not found FatalThrowableError errors when calling new \MongoId( $id ) in PHP.
This post Class 'MongoId' not found (Zend Framework with MongoDB Doctrine) suggests that the reason for given error is that the PHP Mongo extension isn't enabled.
However, if I look at the phpinfo() output, I can see mongodb section. Doesn't that mean it's enabled?
What else could possibly cause this error?
I assume that you are using php 7 version.
In php 7 version a new MongoDB extension is used.
So instead of legacy MongoId you should use MongoDB\BSON\ObjectID
Related
I just had this error when I updated to PHP 8.1:
Fatal Error: Cannot use 'Object' as a class name as it is reserved on line 77
I guess it used a reserved word as an actual class. How can I solve this?
The solution depends where exactly the error occurs.
Error is in file that belongs to Yii framework itself
You have to upgrade framework to newer version. Version 2.0.13 introduced yii\base\BaseObject as base class instead of yii\base\Object.
Error is in some 3rd party library
You have to look for upgrade of that library.
Error is in class that belongs directly to your application
That probably means that your class extends yii\base\Object. You have to modify classes like that to extend yii\base\BaseObject instead. This requires the version of Yii framework to be at least 2.0.13. But because the error didn't occur in framework its version should be probably ok.
You need to follow the instructions on this page to upgrade.
For example, to use php 7, 8, you must follow the instructions in this section.
For compatibiliy with PHP 7.2 which does not allow classes to be named Object anymore, we needed to rename yii\base\Object to yii\base\BaseObject.
This also applies to php 8.
Upgrade from Yii 2.0.12
If you want to upgrade PHP to version 7.2 in your project you need to remove all cases that extend yii\base\Object and extend from yii\base\BaseObject instead:
Good luck
I am using PHP 7.4, Laravel, and the most recent version of Symfony.
I have a webapp template running on Nginx, but it can't load routes due to a bug existing in Symfony since 2017 that leads to the error:
Cannot declare class Acme, because the name is already in use
This comes up when troubleshooting 404 Not Found by running php artisan route:list. I have also tried clearing all caches, dump autoload, etc. I read the following resources, which are related but have not yet led to a solution for me:
https://github.com/symfony/symfony/issues/25484
https://github.com/symfony/symfony/issues/25334
https://github.com/symfony/symfony/issues/20532
https://github.com/symfony/demo/issues/411
https://github.com/symfony/symfony/pull/25363
https://symfony.com/doc/current/performance.html
https://symfony.com/blog/new-in-symfony-4-4-preloading-symfony-applications-in-php-7-4
One of the developers suggested that this is due to a "conflict of inlining strategies" and offered the suggestion of not using opcache.
The only instructions I've found for disabling it seem to involve .ini files that are not used in my combination of software (Debian, Nginx, Laravel, PHP 7.4, artisan, composer - all at their most recent stable versions).
How can I disable it or otherwise resolve the quasi-conflict of "Cannot declare class Acme..."?
I have MongoDB installed in my cPanel/WHM CentOS server.
I have the PHP Drivers installed.
I have Port 27017 opened.
This works $m = new MongoDB\Driver\Manager();
These do not work:
$m = new Mongo();
$m = new MongoClient();
Both result in Fatal error: Class 'Mongo' not found in ... and Fatal error: Class 'MongoClient' not found in ....
Does anyone know why?
Not a duplicate. That SO Q is for a Windows machine. My server is Linux/CentOS
From My Hosting Provider's Sys Admin:
It looks like "mongo" is a seperate php module from "mongodb", and that "mongodb" is the newer one:
root#host [~/support/642192]# pecl search mongo
Retrieving data...0%
.Matched packages, channel pecl.php.net:
Package Stable/(Latest) Local
mongo 1.6.12 (stable) MongoDB database driver (legacy)
mongodb 1.1.2 (stable) 1.1.2 MongoDB driver for PHP
Did you need the legacy module "mongo" instead of "mongodb"? Here is hte pecl page for the package you have:
https://pecl.php.net/package/mongodb
and here is the one for the legacy module:
https://pecl.php.net/package/mongo
Short answer
You cannot use the following classes with the new mongodb driver:
$m = new Mongo();
$m = new MongoClient();
This corresponds to the legacy mongo driver. Instead you should use MongoDB\Client through the MongoDB PHP Library.
Long answer
All right. I lost some hair on this story as well, because the documentation about Mongo and PHP is extremely confusing. The options are then to get mad, bald, or both. But i found courage and finally got it. So this might help you.
1. MongoDB driver: mongo vs mongodb
First of all, you must clarify which MongoDB driver you use: either mongo (legacy) or mongodb (new). Note the smaller case. First source of confusion, the mongo driver is sometimes referred to as MongoDB (legacy) PHP driver.
https://docs.mongodb.com/ecosystem/drivers/php/
Second source confusion, the version numbers are not logical, as mongo driver had 1.5, 1.6 but mongodb starts again from 1.0. So, going forward, but backwards, it's just insane... Imo they should have started from 2.0!
The choice of the driver depends on your PHP version:
PHP5.3: you can only use the mongo legacy driver (1.5, 1.6)
PHP7.0: you can only use the new mongodb driver (1.1+)
PHP5.4, 5.5, 5.6: here you have the choice between the old mongo (1.5, 1.6) or the new mongodb (1.0, 1.1+)
But it's not over. The driver is just a low-level interface (aka PHP extension). Now we come to the programmer's API and it becomes even worse.
2. API: MongoClient vs MongoDB\Driver
Third source of confusion, the low-level drivers and the API classes have overlapping names.
mongo legacy driver -> classes MongoClient, MongoDB (!), ...
mongodb driver -> classes MongoDB\Driver, MongoDB\BSON, ...
But it's not over yet. The old mongo driver could be used directly. The new mongodb driver provides classes (such as MongoDB\Driver) but it is actually a low-level API. You are not supposed to use it directly, you could, but it's not convenient. Instead you should use the MongoDB PHP Library which gives an API similar to the old MongoClient classes...!
3. MongoDB PHP Library (with mongodb) -> MongoDB\Client
https://docs.mongodb.com/php-library/master/
So if you installed mongodb, you should install this MongoDB PHP Library in order to use MongoDB\Client. This class is supposed to be similar to the old MongoClient, but there are some differences such as the sort and projection.
To install this library you are advised to use the tool called Composer which allows to you download these classes into your repo. Then use the autoloader provided with Composer.
And here we come to the 4th source of confusion, MongoDB PHP Library is versioned from 1.0 even though you are using the last mongodb driver in version 1.1 ! It's certainly obvious for those who developed this stuff but very hard to follow for lambda users. Crazy confusing stuff.
There are also many other libraries above the drivers, but i don't know them at all so i won't go more into them (https://docs.mongodb.com/ecosystem/drivers/php-libraries/).
TL;DR
Depending on your PHP version, clarify which MongoDB driver to use: mongo (legacy) or mongodb (new) ?
with mongo you can use MongoClient class directly.
with mongodb you should also install MongoDB PHP Library to use MongoDB\Client class.
Good luck! :)
Looks like you haven't enabled or installed php_mongo extension.
After enabling it restart apache and check phpinfo() to see if its properly enabled.
You have use the correct namespace. As stated in their docs the namespace for the client is MongoDB\Client. Alterantivly you can use a use statement like this use MonogoDB\Client.
Route::get('/', function () {
$tweets = Tweet::all();
return view('welcome', ['tweets' => $tweets]);
});
I am making a laravel app using mongodb.
When I go to '/', I get an error in the mongod terminal that says
AssertionException handling request, closing client connection: 10304 Client Error: Remaining data too small for BSON object
This is my tweet model (in App\Tweet):
namespace App;
use Jenssegers\Mongodb\Model as Eloquent;
class Tweet extends Eloquent {
protected $collection = 'tweets_collection';
}
There are at least two reasons why this issue (Client Error: Remaining data too small for BSON object) appears:
1. PHP MongoDB driver is not compatible with MongoDB installed on the machine.
(originally mentioned in the first answer).
Examine PHP driver version set up on your machine on <?php phpinfo(); page:
Retrieve MongoDB version in use with:
mongod --version\
# db version v3.2.0
Use compatibility table on MongoDB website to see whether examined PHP MongoDB driver version is compatible with MongoDB version:
If versions are not compatible, it is required to uninstall one of the existing parts and install compatible version. From my own experience, it is much easier to change PHP MongoDB driver, since only different .so extension file is required.
2. Two PHP MongoDB drivers are installed on the machine.
Since MongoClient is deprecated, many tutorials and articles online (including official mongo-php-driver repository on Github) now guides to install mongodb, not mongo PHP driver. Year+ before, everyone was pointing at mongo extension, however.
Because of this change from mongo to mongodb, we might get both extensions defined in php.ini file. Just make sure, only one extension is defined under "Dynamic Extension" section:
Hope somebody gets this answer useful when looking for a solution to fix "Remaining data too small for BSON object" error working with MongoDB through PHP MongoDB driver.
The issue was that Laravel was unable to communicate with MongoDB because I was using the mongodb-1.1 php driver and MongoDB 3.2 together. According to the table found on this page: https://docs.mongodb.org/ecosystem/drivers/php/, these two versions are not compatible. I uninstalled MongoDB 3.2 and installed MongoDB 3.O, and the problem was solved.
I'm working with Zend Server CE on a Windows 7 (64bit) System.
I'm developing a website with Zend Framework 2.0. In this website I use DOMDocument to analyse an external website (given by URL).
I started this project with Zend Framework 1.12 and Zend Server CE 4 (PHP 5.2). Now I installed the Zend Server CE 5.6.0 (apache2.2, PHP 5.3.14, ZF2-Support). I rebuild my project with ZF2.0. Everything works fine...except one function.
I get this Error-Message (php-error.log), when I try to instantiate DOMDocument:
Fatal error: Class 'Application\Controller\DOMDocument' not found in
_Path_\module\Application\src\Application\Controller\SearchbarController.php on line 81
With PHP 5.2 (ZF 1.12) and Zend Server CE 4, this function worked perfectly. But now...without any change, it throws this error.
The dom-extension and the xml-extension are built-in in PHP 5.3. I checked phpinfo and my php.ini, that these extensions are running.
I read, that i should install php-xml. But dom, xml and libxml are running...why should i re-install them?
I don't know, why DOMDocument could not be instantiated after upgrading the Zend Server CE. May be, that the ZF2.0 is a possible reason for this error. But I can't find anything pointing to this.
In short, it's introduction of namespaces (feature of PHP 5.3 quite heavily used by ZF2 - but not ZF 1.x) that caused this bug. With this line...
$dom = new DOMDocument(...)
... only the currently imported namespace is checked for this class by ZF. And it results in failure, because DOMDocument class actually belongs to the global namespace.
The solution is simple, when you see the reason: use the global namespace specifier - \ symbol - in front of the class name. Like this:
$dom = new \DOMDocument(...)