I am trying to create a new block service using Sonata Block Bundle. It is running atop Symfony 3.3.
I have cloned my existing TextBlockService.php file into a new file called CenteredOverlineBlockService.php. Both files reside in the src/AppBundle/Resources/config/ directory.
I have added the following text to my blocks.yml file:
vgms.block.centeredoverline:
class: AppBundle\Block\CenteredOverlineBlockService
arguments:
- "Centered Overline"
- "#templating"
- "#sonata.media.manager.media"
- "#sonata.admin.pool"
tags:
- { name: sonata.block }
... and I have added the following reference in sonata_block.yml:
vgms.block.centeredoverline:
... and I now get the following error:
Attempted to load class "CenteredOverlineBlockService" from namespace
"AppBundle\Block". Did you forget a "use" statement for another
namespace?
So my question is: What step am I missing here?
The application loads the previously existing TextBlockService without complaint. It's only this new service that seems to create a problem.
===
Edit #1: Here is the top of the class involved:
namespace AppBundle\Block;
use ...
class CenteredOverlineBlockService extends \Sonata\BlockBundle\Block\Service\TextBlockService
{
...
For what it's worth, my "fix" was to clone a service from a different bundle and then use that class instead.
That doesn't really answer the "why" question I posed, but maybe it will hopeuflly help someone.
Related
I'm trying to create a crud in Symfony 4.4 with bin/console make:crud command but I'm getting the following error
In MakeCrud.php line 103:
Call to a member function getRepositoryClass() on null
this is part of the class I'm trying to use for the crud
<?php
namespace Domain\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Segment
{
private $id;
private $uidentifier;
private $name;
I type the class name with the namespasce oto genterate crud but I get the error, what could be wrong?
So I got a little bit curious and confirmed that it is a namespace issue.
I made myself a Domain\Entity\Segment class and then ran
bin/console make:crud 'Domain\Entity\Segment'
And got the same error message.
The make:crud command expects entities to live under App\Entity. Does not seem to be any way to convince it to look under Domain\Entity just for entities.
However the Maker Bundle itself allows you to change the name of the App's root namespace:
# config/packages/maker.yaml Need to add this file
maker:
root_namespace: Domain
# Then run
bin/console make:crud Segment
And it works. The problem is that the root namespace is used for generating all the files so, for example, you end up with a Domain\Controller\SegmentController class which is probably not where you want them to be.
I checked the source and there is no easy way to adjust just the entity namespace. If you plan on using crud quite a bit then you either need to copy your entities to App\Entity or specify the root_namespace and then copy/refactor your generated controllers and forms. Either way it will be a pain.
I suppose you could also fork the maker bundle and edit the source code directly. Might actually be worth the effort if you have a bunch of crud to generate. But that will be left as an exercise.
I'm working on a bundle for Symfony 4 that is structured like this:
\Acme
\FooBundle
\Article
\Entity
- Article.php
- Comment.php
\Form
- ArticleType.php
\Repository
- ArticleRepository.php
- CommentRepository.php
- ArticleManager.php
\User
\Entity
- User.php
\Repository
- UserRepository.php
- UserManager.php
\SomethingElse
\Entity
- SomethingElse.php
\Repository
- SomethingElseRepository.php
- SomethingElseManager.php
There are many more folders and entities, but is irrelevant for the question.
Autowiring all the classes in that folder can be created with a config like this one:
Acme\FooBundle\:
resource: '../../*/{*Manager.php,Repository/*Repository.php}'
exclude: '../../{Manager/BaseManager.php,Repository/BaseRepository.php}'
autowire: true
But when you need to add service tags like doctrine.repository_service, this kind of configuration won't help no more. Without the tag, when using in controller like:
$this->getDoctrine()->getRepository(Bar::class)
or
$this->getDoctrine()->getManager()->getRepository(Bar::class)
it throws an error:
The "Acme\FooBundle\SomethingElse\Repository\SomethingElseRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service".
The thing is that, since they all reside in the same root folder I'm not allowed to use a config like the following one, because it would have duplicated Acme\FooBundle\ keys:
Acme\FooBundle\:
resource: '../../*/{*Manager.php}'
exclude: '../../{Manager/BaseManager.php}'
autowire: true
Acme\FooBundle\:
resource: '../../*/{Repository/*Repository.php}'
exclude: '../../{Repository/BaseRepository.php}'
autowire: true
tags: ['doctrine.repository_service']
So, I was wondering if there's a workaround that I couldn't find or I should just manually add each and every service?
Edit:
It would have been a nice feature to be able to use an annotation in the class so when it's loaded it "knows" it's tag, but I'm thinking it works the other way around, loading a class because is was tagged with a certain tag.
I had the same error message after upgrading to symfony 4.4 from 3.4.
The issue seemed to be that the entity had an annotation to #ORM\Entity(repositoryClass="App\Repository\MyRepository")
while the repository extends ServiceEntityRepository and in the constructor points to the entity parent::__construct($registry, MyEntity::class);.
Removing the annotation on the entity fixed the issue.
I encountered the same error message after refactoring (renaming) some entities and the related repositories using PhpStorm 2019.2 The refactor did not update the repository class name in the doc block for the entity:
* #ORM\Entity(repositoryClass="App\Repository\OldRepository")
So I used right-click > Copy Reference to get the fully qualified name of NewRepository and pasted it in to the doc block reference:
* #ORM\Entity(repositoryClass="\App\Repository\NewRepository")
PhpStorm prefixed the class with a backslash and I didn't notice until after trying many combinations of suggested solutions for this error. I only needed to remove the backslash and the error is gone:
* #ORM\Entity(repositoryClass="App\Repository\NewRepository")
You can autoconfigure tags in your Kernel / Main Bundle Class:
https://symfony.com/doc/current/service_container/tags.html#autoconfiguring-tags
<?php
namespace Acme\FooBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class FooBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->registerForAutoconfiguration(EntityRepository::class)
->addTag('doctrine.repository_service');
}
}
You can tag all of your repositories, like this:
App\Repository\:
resource: '../src/Repository'
autowire: true
tags: ['doctrine.repository_service']
Thanks #t-van-den-berg and #arleigh-hix !
I had this problem after migrating from Symfony 3.4 to 4.4, when I wanted to use old Repositories with new services.
My solution was a little variation:
use App\Repository\NewRepository;
//...
/**
* #ORM\Entity(repositoryClass=NewRepository::class)
*/
And service declaration (to use Interface):
App\Repository\NewRepository:
arguments:
- "#doctrine"
App\Repository\NewRepositoryInterface: '#App\Repository\NewRepository'
I have some classes that I want to add to the project in Symfony but it's a bit diffucult. I tried in many ways but still can't use a class.
My classes are stored in Model folder : src/project/MyBundle/Model/
I added the name space in my class namespace Project\MyBundle\Model;
And in the controller the use use Project\MyBundle\Model\Vendor;
and the call $vendor = new \Vendor($cin);.
But this doesn't work. I get this error
Attempted to load class "Vendor" from the global namespace.
Did you forget a "use" statement?
Some help pls
Define it as service:
my_service:
class: Project\MyBundle\Model\MyClass
In controller you can use it like this:
$this->container->get('my_service');
I have a problem in my controller when I want to get a sevice that I create, everything work well when I'm in localhost, but when I put the website in my server I get the problem.
Here is the line that make the error in my controller:
$myService = $this->get('Acme_test.service');
The services.yml:
services:
Acme_test.service:
class: Acme\testBundle\Services\Testservice
arguments: [%folder%, #service_container]
And the error:
ClassNotFoundException: Attempted to load class "Testservice" from namespace "Acme\TestBundle\Services" in /home/www/acme/app/cache/dev/appDevDebugProjectContainer.php line 736. Do you need to "use" it from another namespace?
I tried to clear the cache, still having the same error !!
You need to set nested your service to the services node in the services.yml file like:
services:
acme_test.service:
class: Acme\TestBundle\Service\TestService
arguments: [%folder%, #service_container]
And I think better write service name in lowercase. And then get service in controller like:
$myService = $this->get('acme_test.service');
NOTE: I think that you meant TestBundle and TestService names in camelCase
NOTE2: And commonly services dir name is Service, so check your namespace, it need to be Acme\TestBundle\Service, class name need to be TestService and file Acme\TestBundle\Service\TestService.php must exists.
NOTE3: After all recommendations and modifications manually clear cache dir.
I just started learning Symfony2 and I'm following the examples from "The Cookbook" from Symfony's website.
When trying the code from the chapter about loading users from database (Entity Provider) (Link to the chapter) I get the following error:
MappingException: Class Acme\UserBundle\Entity\User is not a valid entity or mapped super class.
... and can't find out wthat I am doing wrong. I do think I followed all the steps provided in the chapter.
Thanks for any help,
I finally found the problem when revising step by step the whole code.
I forgot to register UserBundle in AppKernel.php.
I have the same problem. I looked at symfony+Mapping error but that solution not works for me. Then I found, that Michi solution works https://stackoverflow.com/a/10935672/2910183
So, here is what I do at all:
register bundle in AppKernel.php:
new Acme\UserBundle\AcmeUserBundle(),
create this bundle (it is just a copy of FOS\UserBundle\FOSUserBundle) and save as src/Acme/UserBundle/AcmeUserBundle.php
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\DoctrineCouchDBMappingsPass;
class AcmeUserBundle extends Bundle {
}