ClassNotFoundException: Attempted to load class "Mongo" from... (with persist) symfony2 - php

I am having some issue integrating mongodb with Symfony (version 2.5.0-DEV) using the doctrine mongodb cookbook on http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html.
Everything is okay up to the 'Persisting Objects to MongoDB' stage. When I add the line "$dm->persist($script);", nothing happens to my remote database and I get the error message:
ClassNotFoundException: Attempted to load class "Mongo" from the global namespace in /var/www/Symfony/vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Connection.php line 283. Did you forget a use statement for this class?
But without the persist line, the script parses without errors (but nothing happens at the remote database).
Is this particular to my Symfony version (2.5.0) and is there a workaround? I have pasted my entire script below including the use statements. Any help would be appreciated :).
namespace Atlas\MpBundle\Controller;
use Atlas\MpBundle\Document\Scripts;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class UserjsonController extends Controller
{
public function showuserjsonAction()
{
$script = new Scripts();
$script->setName('kingstest');
$script->setDescription('just a desc test');
$script->setGroup('SMS');
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($script);
$dm->flush();
return new Response('Created New Document in scripts with script id '.$script->getId());
}
}

Thanks guys. Works now.
the extension mongo.so has to be loaded in php.ini and I edited the wrong php.ini file. Added extension=mongo.so to php.ini located in /etc/php5/apache2/ and now it works :)
Hopefully this can help someone in the future.

Related

SOLVED: Problem with attribute routing in symfony 5.4

There's a solution via RFE for this in RFE; solve problem with bin commands and php versions
I'm dealing with a new app in symfony 5.4 and php 7.4 to test the new additions and changes in symfony 6. I've used the entity maker from the console to create the entity and the crud, and the db was created perfectly. However, the generator uses the new "attributes" (according to the convention in https://symfony.com/doc/5.4/routing.html) instead of the "classic" annotations. Debugging via console to see the resulting paths, none of the routes defined in the controllers is shown (of course, a 404 error was shown when accessing the url in dev mode). I decided to replace the attributes with classic annotations, and the paths are shown and the 404 error is gone. But now, I find that the logic the generator uses is via the repository to use the Entity Manager, and when accessing to the index to start from scratch, I get:
Could not find the entity manager for class "App\Entity\Room". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.
The portion of code shown in the debugger is this:
class RoomRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Room::class); // Here is the error
}
And the entity starts with this:
namespace App\Entity;
use App\Repository\RoomRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RoomRepository::class)]
class Room
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column()]
private ?int $id = null;
...
My biggest concern is that I guess I can't rewrite the full crud reverting to annotations, which is a lot of work (just what I wanted to avoid by using the generator), so there must be something about the attributes that I'm missing. Here's a controller whose crud I haven't modified yet, so anyone can take a look and find out why the router can't find the defined paths with this kind of annotation.
namespace App\Controller;
use App\Entity\RoomFeature;
use App\Form\RoomFeatureType;
use App\Repository\RoomFeatureRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/admin/feature')]
class RoomFeatureController extends AbstractController
{
#[Route('/', name: 'admin_room_feature_index', methods: ['GET'])]
public function index(RoomFeatureRepository $roomFeatureRepository): Response
{
return $this->render('room_feature/index.html.twig', [
'room_features' => $roomFeatureRepository->findAll(),
]);
}
...
What is the problem with all this? Thanks in advance.
As stated in the first comment, forcing every bin/console (for make: commnds) and composer commands to be run especifically by prepending php7.4 to the command, everything works but with "classic" annotations, I've found no way to use attributes in controllers with php7.4. The .php-version file seems to be used only by the symfony-cli to launch the dev webserver. I hope this helps anyone who can face this scenario in the future.
I had a similar situation and on my case it was related to an incorrect namespace on a Trait (under src/Entity/Traits).
The trait wasn't even being used but apparently it will still cause this error.
Regarding your second comment:
the resulting code using annotations via attributes should be
compatible with whatever version of php is supported with symfony 5.4
This assumption is wrong, since symfony 5.4 supports php 7.2.5 and up. Although 7.2 is sunsetted officially, there are plenty of installations out there or with extended support from distros so that's besides the point: Symfony will happilly run in 7.2 or 7.3.
In any case, it's maker bundle who determines whether or not to use attributes based on the php version that is used to execute the maker commands, not the available installed versions. You can force it to use specific version features by executing composer config platform.php 7.4.x.
And be aware that maker bundle 1.44 has dropped annotation support altogether, so pin your version to an older one.

Symfony 2 namespace error only in production ("Attempted to load class...)

I am working on a Symfony 2 website, and having an issue only in production (cache is clear).
I am using the Payplug php api : I' ve put the files in the vendor folder, I use the namespace for the classes I use, and everything is ok on my local dev environment.
Once on the prod server, I get the error:
"Attempted to load class "Payplug" from namespace "Payplug". Did you forget a "use" statement for another namespace?"
I don't get why it gets a namespace error only on the prod server...
CONTROLLER :
<?php
namespace KpmBundle\Controller;
use KpmBundle\Entity\Marche;
use KpmBundle\Entity\Marcheur;
use KpmBundle\Entity\Commande;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Payplug\Payplug as Payplug;
use Payplug\Payment as Payment;
class MarcheController extends Controller
{
//code
Payplug::setSecretKey(...);
//more code
}
CLASS
<?php
namespace Payplug;
/**
* The Payment DAO simplifies the access to most useful methods
**/
class Payment
{
I really wonder why everything works fine on my local dev server ( app_dev.php and app.php ), but goes wrong on prod server...
Any idea would be appreciated,
regards
EDIT
The files where installed via composer
I was using FTP, and transfered the vendor\payplug directory alone to the remote server ; as a result the vendor\composer wasn't sync between local and remote server.
transfering the local vendor\composer solved the problem
Thx to #cerad

Symfony console component, class not found

I'm using the console component without Symfony standard edition and I'm working on my application.php file. When I run it it says:
Fatal error: Class 'GenerateTableCommand' not found in D:\ConnectCommand\vendor\application.php on line 10
My code:
<?php
require __DIR__.'\autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new GenerateTableCommand());
$application->run();
?>
My repository can be found here if needed: https://github.com/guuse/ConnectCommand
Any help is greatly appreciated.
First of all there's no \GenerateTableCommand, as stated in the error message.
This class is under AppBundle\Command namespace, so it's full name is AppBundle\Command\GenerateTableCommand.
You should add use statement at the beggining:
use AppBundle\Command\GenerateTableCommand;
Anyway, you'll probably encounter further issues with loading this class, since you do not really have any autoloaders for your custom code, so PHP won't be able to load this class.
Also mixing 3rd party code with your own (in vendor directory) is not a good idea.

PhantomJs cannot execute binary file

I am trying to use PhantomJs in my laravel 5 project. I have downloaded via composer. I have added into my providers section and aliases section within config/app.php
Okay so i have now created my controller as seen below:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use PDF;
use View;
class PdfController extends Controller {
public function index()
{
$view = View::make('home');
return PDF::createFromView($view, 'home.pdf');
}
}
I have also created my route for this method. However when i try this in my browser it throws the following error:
PhantomJS: sh: /Users/matts/sites/ManagementApp/vendor/antking/phantom-pdf/src/../bin/phantomjs: cannot execute binary file
Has anyone come across this before?
Thanks
The "cannot execute binary file" indicates that it was not able to run phantomjs at all, and it is probably not related to your code. The most likely reason for this is an architecture incompatibility. Depending on whether or not you have a 32 or 64 bit system, you should use either phantomjs-1.9.8-linux-x86_64 (64bit) or phantomjs-1.9.8-linux-i686. It is possible that you used the wrong one for your system.

Cannot use X as Y because the name is already in use, even though it's not

I'm using PHP 5.4, and have a PSR-0 class structure similar to the following.
A\Library\Session.php:
namespace A\Library;
class Session { ... }
My\Application\Session.php:
namespace My\Application;
class Session { ... }
My\Application\Facebook.php:
namespace My\Application;
use A\Library\Session;
class Facebook { ... }
When I try to run the application, I get the following error:
Cannot use A\Library\Session as Session because the name is already in use in My\Application\Facebook.php
Even though it's not, at least not in this file. The Facebook.php file declares only the Facebook class, and imports exactly one Session class, the A\Library one.
The only problem I can see is that another Session class exists in the same namespace as the Facebook class, but as it was never imported in the Facebook.php file, I thought it did not matter at all.
Am I wrong (in that case please point to the relevant documentation), or is this a bug?
There is a bug confirmed in PHP that may affect the behavior you see. It is supposed to fatal error, but with opcache enabled, it may still execute flawlessly.
https://bugs.php.net/bug.php?id=66773
If it still concerns you, please vote for the bug.
No, this is not a bug. As mentioned in Using namespaces: Aliasing/Importing
use A\Library\Session;
is the same as:
use A\Library\Session as Session;
So try using something like:
use A\Library\Session as AnotherSessionClassName;
The only problem I can see is that another Session class exists in the
same namespace as the Facebook class, but as it was never imported in
the Facebook.php file, I thought it did not matter at all.
Yes, it does matter. This is why you don't need to "import" classes from the same namespace. If you have conflicting names from different namespaces, you need to alias the class.
namespace My\Application;
use A\Library\Session as ASession; // choose a proper alias name here
class Facebook { ... }
I've read the thread about the issue, but I tested on many PHP versions (php 5.5, 5.6, 7.*, x32, x64, vc11, vc14, vc5). I'm using Laravel with Laragon. But, when I build up the server with php artisan serve (and open the server at http://localhost:8000) I have the problem of "the namespace that some Class was already used" and stuff.
I tested with and without opcache extension and nothing works, then I tested the virtual domain that Laragon provides and... voila, the error just disappeared and now I can work OK. I don't know what was happening, my namespaces were OK, I had an alias but the same code works in many machine without problem (AWS, local, prod, dev, etc) but only in my machine I had the problem just as I described it.
So, if someone is working with Laravel (5.1) and is having this issue, try the virtual host of Laragon.

Categories