I'm starting out with Symfony and doctrine/mongodb. I'm trying to do an aggregation query, but I get
Attempted to call an undefined method named "createAggregationBuilder" of class "Doctrine\ODM\MongoDB\DocumentManager"
with something like this:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class TestController extends Controller
{
/**
* #Route("/test", name="test")
*/
public function testAction(Request $request) {
$builder = $this->get('doctrine_mongodb')
->getManager()
->createAggregationQuery('AppBundle:Test');
var_dump($builder);
}
}
I'm quite frustrated now, since every documentation or stackoverflow questions and answers start with
$dm->createAggregationQuery();
and I can't find out what $dm stands for in this case.
I'd really appreciate some help here.
UPDATE
I was looking around in the doctrine/mongodb-odm source code and I found that my version is missing the createAggregationBuilder function from both the DocumentRepository.php and the DocumentManager.php file (both located in /vendor/doctrine/mongodb-odm/lib/Doctrine/MongoDB folder.
How can this be?
I mean composer says that I have version 1.1.6, which is the latest release and on the git repo I can cleary see these methods (DocumentRepository.php, DocumentManager.php)
Silly me. The problem was that I was using the 1.1.6 version of doctrine/mongodb-odm and the Aggregation Builder will only be available in version 1.2. Until than (for anyone being as blind as me), just use the "dev-master" version.
In your composer.json modify the doctrine/mongodb-odm line to this:
"require": {
"doctrine/mongodb-odm": "dev-master"
}
And then run composer update doctrine/mongodb-odm.
Related
I got the message
Did you forget to run "composer require symfony/security-core"? Unknown function "is_granted" in "...".
when calling template-code
{% if is_granted(constant('Rights::RGT_TOUR_ADD')) %}...{% endif %}
I am using symfony v5.3.7, symfony/security-core and symfony/twig-bridge are both v5.3.7. twig itself is v3.3.2 (just updated the whole stuff). All packages are installed in the "good way" of "symfony composer require..."
There is a bunch of extensions in /vendor/symfony/twig-bridge/Extension and most of them are loaded, but not the SecurityExtension (even though available).
Of course I don't want to hard-code a solution, due to the fact that it would be done in /vendor which is in .gitignore ;)
I already tried forced reinstalling of the package... No change.
Just in case this info is needed... PHP is v7.4.15 x64
Security bundle doesn't have logout_path.
You need controller action with the path having that name:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class SecurityController extends AbstractController
{
/**
* #Route("/logout", name="logout_path")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}
As for is_granted check if symfony/security-bundle is installed, enabled in config/bundles.php, and configured properly in config/packages/security.yaml.
You can not use the "render" method if the Templating Component or the Twig Bundle are not available. Try running "composer require symfony/twig-bundle".
class RecordsController extends AbstractController
{
/**
* #Route("/", name="single_page")
* #return Response
*/
public function singlePageAction()
{
return $this->render('single_page.html.twig', []);
}
}
Symfony 4 - Not rendering Twig Template
Same as for this guy but accepted answer does not help.
Tried also
composer require debug --dev
then got error
[InvalidArgumentException]
Could not find package debug.
Did you mean one of these?
symfony/debug
tracy/tracy
maximebf/debugbar
cakephp/debug_kit
symfony/debug-pack
so installed symfony/debug. Which does not make any sense but I did. Of course it did not fix the problem.
Here is how composer.json now looks:
{
"require": {
"symfony/maker-bundle": "^1.13",
"twig/twig": "~2.10",
"symfony/twig-bundle": "^4.3"
},
"require-dev": {
"symfony/debug": "^4.3"
}
}
I have installed twig 2 because otherwise could not install twig-bundle - was gettig error.
How to get rid of this error and show the template?
Update:
Noticed that my application has 2 composer.json files. One in root, another in myapp folder. Guess this is why after install it does not see because it is installed in that root directory. But now I am too angry and tired already to check, will need to do that later.
in order to use twig, after installing the package with composer, you should enable the bundle for your environment(s). Here you have the docs for the current version of symfony https://symfony.com/doc/current/bundles.html
Once you do that, you have to inject twig in your AbstractController.
If you already did all of these steps, I suggest you to add some more code, in order to allow the community to spot the problem.
I also suggest you to avoid using multiple composer.json files: use just one in your project root directory.
Goodbye!
I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.
So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.
I was trying to incorporate redis to my Laravel app after learning about it.
And since it is not enabled by default, i've added a line at composer.json file with this: "predis/predis": "1.1.1" under the require.
Next thing i did was run composer update from my CLI.
Once that was done i got this post update:
Then I visit my app page and got this error.
So I looked on my app\Http\Controllers\Controller.php since it was the line it was referring into and went to check if this files still exists but it was not there anymore.
app\Http\Controllers\Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests; <-this
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; <-this
use Illuminate\Foundation\Auth\Access\AuthorizesResources; <-this
class Controller extends BaseController
{
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
}
EDIT: Solved
#ceejayoz thank for poiting that out. the downgrade was what caused it. change the 5.2.0. to 5.2.39 and did composer update that now it's back. I was not aware of my own laravel version since i was just using homestead and it was the default values on the composer.json file. Thanks again!
I have THIS package (a payment gateway), which I would like to use in Symfony 3.0.1
Unfortunately I get this error:
ClassNotFoundException in AppKernel.php line 21: Attempted to load class "SofortBundle" from namespace "Sofort\SofortLib".
Did you forget a "use" statement for another namespace?
In the sofort\sofortlib-php folder i created the file SofortBundle.php with this content
<?php
namespace Sofort\SofortLib;
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
class SofortBundle extends BaseBundle
{
}
and I loaded the Bundle in AppKernel.php:
new Sofort\SofortLib\SofortBundle(),
But that only leads to above exception.
What am I missing?
Don't copy packages to your custom folder. Install package as described:
In composer.json add:
"require": {
"sofort/sofortlib-php": "3.*"
}
Run composer update sofort/sofortlib-php
In your code you can use the library like this:
use \Sofort\SofortLib\Billcode;
class MyClass
{
function doSomething($configkey) {
$SofortLibBillcode = new Billcode($configkey);
...
}
}