How to share a Symfony2 model with several projects

Asked
Active3 hr before
Viewed126 times

4 Answers

modelshare
90%

Do I create a seperate bundle which holds the entities for my database, and have the four projects include those entities and work with them?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,A bundle does NOT need to have routing, controllers, views etc. I've actually seen a blueprint bundle, and all it does is ship blueprint-css resources with it so they can be easily reused in projects.,an application that retrieves the received data from a queue and puts it in our database (this is now a seperate script, but I'm thinking of reworking this as a Symfony command that is called by cron)

A bundle does NOT need to have routing, controllers, views etc. I've actually seen a blueprint bundle, and all it does is ship blueprint-css resources with it so they can be easily reused in projects.

As for adding models to the app directory... I wouldn't like that. I see the app directory as a place where all the configuration should be. Even though you can override views under app/Resources, whenever I want to override something I create a new bundle.

app / Resources
88%

For purposes like this, it’s better to create one more, separate Bundle that I called ‘CoreBundle’ that will hold all shared classes and also my entity classes for both: AdminBundle and ApiBundle.,For API, I also need same Entities used inside Admin Bundle that will handle database layer for web services too.,The reason I ask is because I’ve yet to figure out the best way to create an API for a SF2 app – at this stage I’ve also created a Core Bundle that holds all entites, etc.,Because of that, I want to build simple Symfony2 services: one for AdminBundle and second for ApiBundle – to separate application logic.

Inside CoreBundle\Resources\config I made some changes to the file called ‘services.xml’:

<?xml version="1.0" ?>
 
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
   <parameters>
      <parameter key="surgeworks_core.admin.class">Surgeworks\CoreBundle\Services\Admin</parameter>
      <parameter key="surgeworks_core.api.class">Surgeworks\CoreBundle\Services\Api</parameter>
   </parameters>
    
   <services>
      <service id="surgeworks_core.admin">
         <argument type="service" id="service_container" />
      </service>
      <service id="surgeworks_core.api">
         <argument type="service" id="service_container" />
      </service>
   </services>
</container>
load more v
72%

only one deployment, that means no unsynchronised code / model / application versions, ever;,no need to split your core Bundles in separated repositories (to use with composer and multiple projects organization);,some core bundles living in src/ (the one with our entities and business logic i.e.);,All our dependencies are in a shared vendors/ directory, so there is nothing to do for them. But look at the post-install scripts:

All files in the app/ folder can be moved to a new subfolder with a name of your choice, like pony:

mkdir app / pony
mv app / A * app / R * app / S * app / c * app / a * app / l * app / phpunit.xml.dist app / pony
mkdir app / api
cp - r app / pony
/* app/api/
    mv app/pony/AppKernel.php app/pony/PonyKernel.php
    mv app/pony/AppCache.php app/pony/PonyCache.php
    mv app/api/AppKernel.php app/api/ApiKernel.php
    mv app/api/AppCache.php app/api/ApiCache.php
    find app/pony/* -type f -print | xargs sed -i 's/AppK/PonyK/g'
    find app/pony/* -type f -print | xargs sed -i 's/AppC/PonyC/g'
    find app/api/* -type f -print | xargs sed -i 's/AppK/ApiK/g'
    find app/api/* -type f -print | xargs sed -i 's/AppC/ApiC/g'
load more v
65%

How to Work with multiple Entity Managers and Connections ,When working with multiple entity managers to generate migrations:,When working with multiple connections and entity managers, you should be explicit about which configuration you want. If you do omit the name of the connection or entity manager, the default (i.e. default) is used.,When working with multiple connections to create your databases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# config / packages / doctrine.yaml
doctrine:
   dbal:
   default_connection: default
connections:
   default:
   # configure these
for your database server
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
customer:
   # configure these
for your database server
url: '%env(resolve:DATABASE_CUSTOMER_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
   default_entity_manager: default
entity_managers:
   default:
   connection: default
mappings:
   Main:
   is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Main'
prefix: 'App\Entity\Main'
alias: Main
customer:
   connection: customer
mappings:
   Customer:
   is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Customer'
prefix: 'App\Entity\Customer'
alias: Customer
load more v

Other "model-share" queries related to "How to share a Symfony2 model with several projects"