Referencing an entity from another bundle in Doctrine in Symfony 3 - symfony

Firstly, I have already searched this and tried a few solutions but none work - I may well have missed something so this is my issue.
I have three different Bundles in my Symfony 3 project, AppBundle (shared one), ClientBundle (for clients only) and AdminBundle (for admin users only). The only Entity present in the AppBundle is User.php which is extended from the FOSUserBundle in the project, as I needed extra fields. I need to be able to map a field in the User entity to a field in the Site entity which is located in the AdminBundle, but whenever I try to update the schema, I get the following error:
[Doctrine\ORM\Mapping\MappingException] The target-entity
AppBundle\Entity\Site cannot be found in
'AppBundle\Entity\User#siteId'.
Currently my mapping looks like this, in User.php:
/**
* #ORM\ManyToOne(targetEntity="\AdminBundle\Entity\Site")
* #ORM\JoinColumn(name="site_id", referencedColumnName="id")
*/
private $siteId;
As you can see, I have used the full path to the Site entity here. I have even included a use statement for the Entity, just in case:
use AdminBundle\Entity\Site;
But still the same error occurs upon schema update.
Can anyone shed any light on this? I need to be able to get this mapping to work and I cannot move the user entity to the AdminBundle as it is required by both admin and client users, and with my config set up, clients are restricted to the ClientBundle and the AppBundle.

change to targetEntity="AdminBundle\Entity\Site" (remove the leading slash)

Related

Is there a way to move Symfony 6's controllers to several other directories?

I'm starting to develop a project that will be quite big (hear lots of files) and I want to have an organization that's different from Symfony's default one: instead of having one dir for all my controllers, another for all my forms, etc, I want to have one dir per functionality, ie a directory for my homepage which will contain the controller, the templates, another dir for the blog page with the controller, forms and templates, and so on.
I tried what was explained in this (old) answer, and it didn't work : my routes weren't recognized, and a php bin/console debug:router showed they weren't listed anymore.
I feel I will have something to adapt in my routes.yaml file, which looks like this for now, and which explains why it doesn't work because it explicitely searches in the src\Controller directory:
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
I looked in the documentation I found, but I didn't find anything useful (I think I will have to remove the path and change it to something else, but I don't know what yet.
The above solutions are only for differentiating controller and template files. But For Entity, Events, and Forms it will not work.
It seems like you want to separate files for each module or functionality. For example, settings.yaml, controller, entity, forms, events, etc...
For that Symfony provides a Bundle feature. You can create a new Bundle in Symfony and use it as a separate feature. For more details please read this link

Unable to find template - moved Resources from AppBundle to app folder

I moved the Resources/ twig templates from AppBundle to the app folder and now I got:
Unable to find template "ItemBundle:Category:index.html.twig".
I already changed the Routes in the Annotations for the Controllers, but where can I changed the default behaviour for index.html.twig, because the alway want to look at the Bundle, but there is no Bundle anymore, it's just the app folder.
/**
* Lists all Category entities.
*
* #Route("/", name="category")
* #Method("GET")
* #Template(":Category:index.html.twig")
*/
I don't want to change all #Template - Just explain, that all stuff is now under app folder and than the normal CRUD way. For the normal CRUD way, I don't need to write down each #Template where to find the twig template.
I think you're misunderstanding how those template references resolve. Here's the relevant documentation
When you use something like ItemBundle:Category:index.html.twig that means Symfony will try to find that template in two places, in this order
app/Resources/ItemBundle/views/Category/index.html.twig
src/ItemBundle/Resources/views/Category/index.html.twig
So this is going to depend on where you moved them to. So let's say for example that you moved this one twig file to app/Resources/views/Category/index.html.twig. Make note of how this is different than #1 above - the template is no longer in a bundle directory - just a sub-directory of the root views directory.
Therefore, the proper references is ::Category/index.html.twig

Crud with FOSUserBundle

I 'm a new with Symfony2!. Can you tell me please if I can user CRUD with FOSUserBundle?? Is that even possible ? Actually, i want create a Manager role who can edit and delete users form database !
FosUserBundle doesn't provide such functionality. You can generate CRUD using Symfony standard edition built in task:
php app/console doctrine:generate:crud
for your User entity.
For more complex purposes you need to check some admin bundles, maybe SonataAdminBundle . This will provide admin generator in which you can manage (CRUD) your entities.
Also note, that using all kinds of code generators is considered as not-so-good-practice
For those who are facing this issue :
- if you override the parent classe with custom fields (let's say "firstname" + "lastname") in you User entity
- and then re-generate the CRUD
you will see that the 2 custom field became editable.
The Doctrine CRUD generator looks incompatible with FOSUserBundle in the present state, as we create inheritance with a User master class.
There is a same issue if you try to use the Group Model of FOSUserBundle.

Generating a CRUD Controller in a different bundle

I want to generate some CRUD controllers with the following command:
php app/console generate:doctrine:crud
my problem is that I made 2 bundles (1 front end and 1 for the admin section) my entities are in the defaultbundle, but I'd like to generate the CRUD controllers in my admin bundle, is there a way to do this?
Generating CRUD controllers in a bundle different of the entity's one is, as far as I know, impossible.
Indeed, CRUD controllers generator is just here for test. But if you really want to use it, you can actually copy the controller, views and form in the other bundle. You'll just need to change the templates' paths and the namespaces at the beginning of the controller and the Form type.

Detect routes and entities from Symfony2 vendor

i'm creating a bundle that adds an entity and a few routes to be use in a Symfony2 application.
I want my bundle to be used as a vendor, so I've created all the files, published it on GitHub and packagist and everything works fine.
My problem is now when I require the bundle in another project, my entities and my routes are not detected:
1) php app/console doctrine:schema:update doesn't detect any modification
2) When I try to hit a GET route from the vendor, here is the error I get:
No route found for "GET ..."
Any idea is welcome, what's the process to really do these things in a bundle?
Cheers.
Cyril
Take a look at the installation steps of the FOSUserBundle for example. Everyone who uses your published bundle, have to activate it in the appkernel and import the routes. For the entity I think the bundle user have also to subclass the entity.

Resources