Config entries for DoctrineExtensions SoftDeleteable: gedmo/doctrine-extensions - symfony

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

Related

Symfony doctrine:migrations with multiple schema

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

Functional Testing Symfony with mulitple Doctrine EntityManagers

In a Symfony 4 application I have configured multiple entity managers.
I want my functional tests to automatically create DB tables for both managers.
However, when I run my test; PHP unit encounters an error that says
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146
Table 'test.example' doesn't exist
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
orm:
default_entity_manager: default
auto_generate_proxy_classes: '%kernel.debug%'
entity_managers:
default:
connection: default
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
Model:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Model'
prefix: 'App\Entity\Model'
alias: Model
custom:
connection: custom
naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
Custom:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity/Custom'
prefix: 'App\Entity\Custom'
alias: Custom
To create tables and load Fixtures I'm currently using the Liip Functional Test Bundle.
A simple test looks like this:
tests\Functional\GeneralControllerTest.php:
class GeneralControllerTest extends WebTestCase
{
/**
* {#inheritdoc}
*/
protected function setUp()
{
$this->loadFixtures([
SomeFixtures::class,
MoreFixtures::class,
]);
}
/**
* #dataProvider providePageUris
* #test
* #param string $uri
*/
public function checkThatPageLoads($uri)
{
$client = static::createClient();
/* ... more code ... */
$client->request(Request::METHOD_GET, $uri);
static::assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
}
/**
* #return array
*/
public function providePageUris()
{
return [
["/dashboard"],
];
}
}
How can I make my Test Case create database tables for the other custom Doctrine EntityManagers?
I have tried adding:
$this->loadFixtures([], true, "custom");
and also:
$this->runCommand("doctrine:schema:create --env=test --em=custom");
to the setUp() method of the Test Case, but this did not result in what is needed. The database tables all need to be created before any Fixture is loaded into the database.
To not have tables in a different entity manager and connection removed when loading data fixtures I changed the doctrine configuration with distinct environment variables.
config/packages/doctrine.yaml:
doctrine:
dbal:
connections:
default:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
custom:
url: '%env(resolve:DATABASE_URL_CUSTOM)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
According to the code for the WebTestCase on GitHub the second argument should contain the name of the object manager. I am not sure if specifying custom is enough or if you have to specify the full name doctrine.entity_manager.custom though.

Symfony 3 - EntityManager dependency injection with multiple db connections

I have setup a Custom Authenticator using guard and auto wired the service. This is tested and works fine with just MySQL configured.
I have now specified a second database connection (oracle), but Symfony will now not allow autowiring in my service configuration, because it does not know which database connection to use when injecting EntityManager in to the custom Authenticator class.
Any idea how I can Configure the Dependency Injection to use a specific database connection so I can continue to use AutoWire.
Unable to autowire argument of type "Doctrine\ORM\EntityManager" for the service "user.security.login_form_authenticator". Multiple services exist for this class (doctrine.orm.prism_entity_manager, doctrine.orm.baan_entity_manager).
Here is my Doctrine config in config.yml
doctrine:
dbal:
connections:
prism:
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.root_dir%/../var/data/data.sqlite"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: "%database_path%"
baan:
driver: oci8
host: "%baan_host%"
port: "%baan_port%"
dbname: "%baan_db_name%"
user: "%baan_user%"
password: "%baan_password%"
charset: AL32UTF8
orm:
default_entity_manager: prism
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
auto_mapping: true
prism:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: prism
mappings:
UserBundle:
type: annotation
baan:
connection: baan
mappings:
BaanBundle:
type: annotation
Here is the constructor in my Authenticator class
private $formFactory;
private $em;
private $router;
public function __construct(FormFactoryInterface $formFactory, EntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
You can extend Doctrine's EntityManagerDecorator, which implements EntityManagerInterface and accepts an instance of EntityManager in its constructor.
First extend the EntityManagerDecorator class once for each of your connections.
namespace MyBundle\Service\Database;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
class PrismEntityManager extends EntityManagerDecorator {}
class BaanEntityManager extends EntityManagerDecorator {}
Then in your service configuration, you need to manually wire these two services.
MyBundle\Service\Database\PrismEntityManager:
arguments:
$wrapped: '#doctrine.orm.prism_entity_manager'
MyBundle\Service\Database\BaanEntityManager:
arguments:
$wrapped: '#doctrine.orm.baan_entity_manager'
Now you just have to type-hint for one of these services.
public function __construct(FormFactoryInterface $formFactory, PrismEntityManager $em, RouterInterface $router)
{
$this->formFactory = $formFactory;
$this->em = $em;
$this->router = $router;
}
I don't know if i understood your question correctly or not but you can set different configurations for different database connection as below:
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
mapping_types:
enum: smallint
custom:
driver: pdo_mysql
host: '%database_host2%'
port: '%database_port2%'
dbname: '%database_name2%'
user: '%database_user2%'
password: '%database_password2%'
charset: UTF8
mapping_types:
enum: smallint
orm:
default_entity_manager: default
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
auto_mapping: true
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
mappings:
EntityBundle:
type: annotation
alias: DBAlias
custom:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: custom
mappings:
EntityBundle:
type: annotation
alias: DBAlias
Now you can passed your custom EntityManager using doctrine.orm.custom_entity_manager.
I think, I had the same issue with DBAL Connections as you have with EntityManager. I have solved this issue with some proxy class - described in this answer:
https://stackoverflow.com/a/46265170/6357312
Any question let me know.

How to configure Symfony to use Entity Manager other than default in Entity Provider?

Somehow I need to use aux connection / entity manager...
security.yml:
security:
providers:
administrators:
entity: { class: Hoax\PartnerBundle\Entity\Partner, property: username }
config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database1_driver%
host: %database1_host%
port: %database1_port%
dbname: %database1_name%
user: %database1_user%
password: %database1_password%
charset: UTF8
mapping_types:
enum: string
aux:
driver: %database2_driver%
host: %database2_host%
port: %database2_port%
dbname: %database2_name%
user: %database2_user%
password: %database2_password%
charset: UTF8
mapping_types:
enum: string
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
HoaxNotificationsBundle: ~
HoaxPartnerBundle: ~
vpnserver:
connection: aux
mappings:
HoaxPartnerBundle: ~
vpnpayment:
connection: payment
mappings:
HoaxPartnerBundle: ~
Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:
class EntityUserProvider implements UserProviderInterface
{
private $class;
private $repository;
private $property;
private $metadata;
public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null)
I tried setting it like this: { class: Hoax\PartnerBundle\Entity\Partner, property: username, managerName: aux }
But having error: InvalidConfigurationException: Unrecognized options "managerName" under "security.providers.administrators.entity"
Read a little bit further: symfony.com/doc/current/cookbook/doctrine/…. If you still have trouble then update your question with the orm section of doctrine.
You also need to add the entity manager name to the provider section of the security file: symfony.com/doc/current/reference/configuration/security.html
Make sure you use manager_name and not managerName

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