Symfony doctrine:migrations with multiple schema - symfony

When is declared schema that not equal to conection database name it ignored. For example i have 2 Entity:
Users:
namespace App\Entity\DbPublic;
/**
* Users
*
* #ORM\Table(name="users", schema="public")
* #ORM\Entity(repositoryClass="App\Repository\DwPublic\UsersRepository")
*/
class Users
{
UsersData:
namespace App\Entity\DbPrivate;
/**
* UsersData
*
* #ORM\Table(name="users_data", schema="private")
* #ORM\Entity(repositoryClass="App\Repository\DbPublic\UsersDataRepository")
*/
class Users
{
When i try to migrate its don't trow error but its also generate script for schema="public" and not for schema="private", how to generate scrip and relations betwen 2 tables
My doctrine.yaml was:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
and transformed into:
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_MASTER)%'
charset: utf8mb4
db_public:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_MASTER)%'
charset: utf8mb4
db_private:
# configure these for your database server
driver: pdo_mysql
host: '%env(DATABASE_HOST)%'
port: '%env(DATABASE_PORT)%'
user: '%env(DATABASE_USER)%'
password: '%env(DATABASE_PASS)%'
dbname: '%env(DATABASE_NAME_PRIVATE)%'
charset: utf8mb4
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
db_public:
connection: db_public
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
db_private:
connection: db_private
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App

Related

how to use two entity manager for one direction in symfony 4

I'm working with multiple Entity Managers, followed Symfony doc from here,
but I want to use two entity manager for one dir.
It's not working properly in findAll or findOneBy query, it's showing the result for 'default' entity manager.
in config/packages/doctrine.yaml :
dbal:
# configure these for your database server
default_connection: default
connections:
default:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(DATABASE_URL)%'
blog:
driver: 'pdo_mysql'
server_version: '5.7'
url: '%env(DATABASE_BLOG_URL)%'
charset: utf8mb4
orm:
auto_generate_proxy_classes: true
default_entity_manager: default
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
blog:
connection: blog
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
blog:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: blog
in controller:
$entityManager = $this->getDoctrine()->getManager('blog');
$University = $entityManager->getRepository(University::class)
->findOneBy(array('Code' => $Code));
i would recommend inject the connection as a service in your Controller and leverage autowiring
//CONTROLLER
public function testController(YourService $service){
return $service->test();
}
//services.yml
App\Service\YourService:
public: true
arguments: ['#doctrine.orm.entity_manager','#doctrine.orm.blog']
//src/service/YourService.php
class YourService {
private $blog,$em;
public function __construct(EntityManager $em, EntityManager $blog) {
$this->em = $em
$this->blog = $blog;
}
public function test()
{
//connect to blog
$this->blog->getRepository(your_entity::class)->findAll();
//connect to default
$this->em->getRepository(your_entity::class)->findAll();
}
Are you trying to put the name of your second DB in the second argument of the function getRepository, like this:
$University = $entityManager->getRepository(University::class, 'blog')
->findOneBy(array('Code' => $Code));
Update 2:
Use only this configuration, without specify dir, but by using this, you will obligate to use the vanilla SQL to intercate with your DB:
blog:
connection: blog
naming_strategy: doctrine.orm.naming_strategy.underscore
my last suggestion will be to create differente folders Entities for each connection, and duplicate in it the sames entities, and like this you can use the ORM DOCTRINE properly.

How in symfony, create new entity in second database

I have a problem. I was able to configure symfony to connect to two databases.
config.yml:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
customer:
driver: pdo_mysql
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AppBundle: ~
customer:
connection: customer
mappings:
AppBundle: ~
And here my question appears.
How create new entity in second database?
you can do this into a controller:
$customerEntityManager = $this->getDoctrine()->getManager('customer');
or this:
$customerEm = $this->get('doctrine.orm.customer_entity_manager');
and then:
$yourEntity = new YourEntity();
$customerEm->persist(yourEntity);
$customerEm->flush();

Sonata Media Bundle Installation error

'm trying to install sonata media bundle on a symfony 3.3.6
I'm folowing this doc : https://sonata-project.org/bundles/media/3-x/doc/reference/installation.html
But I have an error when i'm using this command line, to generate DB : php bin/console doctrine:schema:update --force
the error message :
[Doctrine\DBAL\DBALException] Unknown column type "json" requested.
Any Doctrine type that you use has to be registered with
\Doctrine\DBAL\Types\Type::addType(). You can get a list of all the
known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this
error occurs during database introspection then you might have forgot
to register all database types for a Doctrine Type. Use
AbstractPlatform#registerDoctrineType Mapping() or have your custom
types implement Type#getMappedDatabaseTypes(). If the type name is
empty you might have a problem with the cache or forgot some mapping
information.
my config.yml is taken from the documentation. I reviewed all such articles and nowhere can I find an answer. Why does this error occur? Help please, I'm starting to be disappointed
You have to add json type in your doctrine configuration:
doctrine:
dbal:
types:
json: Sonata\Doctrine\Types\JsonType
In your config file, you have doctrine section twice.
The first one is applied:
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.project_dir%/var/data/data.sqlite'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
and you didn't define the JSON type there.
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#AppBundle/Resources/config/admin.yml" }
- { resource: sonata_classification.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: ru
framework:
#esi: ~
translator: { fallbacks: ['%locale%'] }
secret: '%secret%'
router:
resource: '%kernel.project_dir%/app/config/routing.yml'
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: '%locale%'
trusted_hosts: ~
session:
# https://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
fragments: ~
http_method_override: true
assets: ~
php_errors:
log: true
# Twig Configuration
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.project_dir%/var/data/data.sqlite'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
#Cache
doctrine_cache:
providers:
my_markdown_cache:
type: file_system
file_system:
directory: /tmp/doctrine_cache
sonata_admin:
title: My Blog Admin
sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
doctrine:
orm:
entity_managers:
default:
mappings:
ApplicationSonataMediaBundle: ~
SonataMediaBundle: ~
dbal:
types:
json: Sonata\Doctrine\Types\JsonType
sonata_media:
# if you don't use default namespace configuration
#class:
# media: MyVendor\MediaBundle\Entity\Media
# gallery: MyVendor\MediaBundle\Entity\Gallery
# gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
default_context: default # you need to set a context
contexts:
default: # the default context is mandatory
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
cdn:
server:
path: /uploads/media # http://media.sonata-project.org/
filesystem:
local:
directory: "%kernel.root_dir%/../web/uploads/media"
create: false
sonata_media:
providers:
image:
resizer: sonata.media.resizer.square
doctrine:
orm:
entity_managers:
default:
mappings:
ApplicationSonataMediaBundle: ~
SonataMediaBundle: ~

Config entries for DoctrineExtensions SoftDeleteable: gedmo/doctrine-extensions

I'm trying to use softdelete option of gedmo/doctrine-extensions but for some reason when I call romove(), the record in database gets removed instead of updating deletedAt field.
In here, doc tells us to update config with:
$config->addFilter('soft-deleteable',
'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
This is just one of the examples I tried:
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
References (just a few of them):
DoctrineExtensions SoftDeleteable
http://knplabs.com/en/blog/gedmo-doctrine-extensions-on-symfony2
Can't enable SoftDeleteable in Symfony2 - Unrecognized options "filters"
So the question in simple terms, how do I configure it in config.yml?
CONTROLLER
public function delete($id)
{
$profile = $this->profileRepository->findOneBy(['id' => $id]);
if (!$profile instanceof Profile) {
throw new ........
}
$this->entityManager->remove($profile);
$this->entityManager->flush();
return true;
}
ENTITY
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity()
* #ORM\Table(name="profile")
* #Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Profile
{
/**
* #ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
......
}
COMPOSER.JSON
"require": {
"symfony/symfony": "2.6.*",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"gedmo/doctrine-extensions": "2.3.*#dev",
......
},
CONFIG.YML
doctrine:
dbal:
default_connection: front
connections:
front:
driver: %database_driver%
host: %database_host%
........
back:
driver: %database_driver%
host: %database_host%
........
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: front
entity_managers:
front:
connection: front
mappings:
MyWebsiteBundle:
dir: Entity
FOSUserBundle: ~
back:
connection: back
MAPPING INFO:
inanzzz#inanzzz:/var/www/html/local$ php app/console doctrine:mapping:info
Found 8 mapped entities:
[OK] My\Bundle\Entity\AbstractMerchantProfile
[OK] My\Bundle\Entity\AbstractIntegration
[OK] My\Bundle\Entity\APIConsumer
[OK] My\Bundle\WebsiteBundle\Entity\User
[OK] My\Bundle\WebsiteBundle\Entity\Profile
[OK] My\Bundle\WebsiteBundle\Entity\Integration
[OK] FOS\UserBundle\Model\Group
[OK] FOS\UserBundle\Model\User
This is how I configured it
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
Solution:
Included stof/doctrine-extensions-bundle in composer.json
"stof/doctrine-extensions-bundle": "1.2.*#dev",
Package is here. Documentation is here.
Enable bundle in AppKernel:
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()
Since I have more than one entity managers in config.yml I did:
stof_doctrine_extensions:
orm:
em1:
softdeleteable: true
doctrine:
dbal:
default_connection: em1
connections:
em1:
driver: %database_driver%
host: %database_host%
.......
em2:
driver: %database_driver%
host: %database_host%
.......
em3:
driver: %mws_database_driver%
host: %mws_database_host%
.......
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: em1
entity_managers:
em1:
connection: em1
mappings:
MyWebsiteBundle:
dir: Entity
FOSUserBundle: ~
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
em2:
connection: em2
em3:
connection: em3

A2lixTranslationFormBundle with Stof DoctrineExtensionsBundle doesnt Show Any Tab

I'm new to Symfony and Sonata. After following the instructions i installed Translatable, but i dont get any Tab in the form.
Composer.json
"stof/doctrine-extensions-bundle": "1.1.x-dev",
"a2lix/translation-form-bundle": "2.x-dev"
config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
gedmo_translatable:
type: annotation
prefix: Gedmo\Translatable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
alias: GedmoTranslatable # this one is optional and will default to the name set for the mapping
is_bundle: false
stof_doctrine_extensions:
default_locale: "%locale%"
orm:
default:
translatable: true
a2lix_translation_form:
locales: [es, pr] # [1]
default_required: true # [2]
manager_registry: doctrine # [3]
templating: "A2lixTranslationFormBundle::default.html.twig" # [4]
Sonata AdminClass
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('descripcion')
->add('unidadMedida')
->add('translations', 'a2lix_translations')
;
}
I had so much trouble trying to use those 2 bundles that I switched to using only a2lix bundles and it worked like a charm for me...
This is what I am using now (sf 2.4)
"a2lix/i18n-doctrine-bundle": "dev-master",
"a2lix/translation-form-bundle": "2.*#dev",

Resources