Symfony2 + doctrine2: Switch to metadata driver "yml" - symfony

a short question: How to switch to the another metadata driver than annotation. How to set the metadata driver "yml" in the config.yml?
I`ve searched google and the symfony2 docu, but didnt find anything :(
thanks

You should be able to do that with the doctrine:mapping:convert command
php app/console doctrine:mapping:convert --force yml ./src/
Double check all the options available before you run the command, though
php app/console help doctrine:mapping:convert

I realize this is an older question but you can explicitly require that doctrine use the yml driver for meta data like this:
doctrine:
orm:
entity_managers:
mappings:
MyBundleName:
type: yml
dir: path/to/ymlmetadata
Be aware that if you use this configuration style then you have to put the "defaults" config node under entity_managers instead of its usual location.

Related

How to generate document with Symfony 3.4 using Symfony flex

With Symfony flex, one of the change is that there is no default bundle when using the symfony/skeleton.
I don't find a way to use the command doctrine:mongodb:generate:documents in this context.
Could you please tell me how to use it ?
Exemple:
php bin\console doctrine:mongodb:generate:documents App
2018-02-17T18:35:22+00:00 [error] Error thrown while running command "doctrine:mongodb:generate:documents AppBundle --document Test". Message: "No bundle AppBundle was found."
In DoctrineODMCommand.php line 87:
No bundle AppBundle was found.
doctrine:mongodb:generate:documents [--document [DOCUMENT]] [--no-backup] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> <bundle>
This is how I setup my project
composer create-project symfony/skeleton:3.4 test
composer config "platform.ext-mongo" "1.6.16" && composer require "alcaeus/mongo-php-adapter"
composer require doctrine/mongodb-odm-bundle
Thanks
Regards,
Chris
There is a similar issue with doctrine orm and doctrine:generate:entities. The Doctrine team discourages users from using these commands and therefore does no longer want to maintain them.
I think the easiest workaround I've seen so far is:
Create a Symfony 3.3 style application using the Symfony installer.
Generate the entities in the AppBundle as you did before
Change the namespace from AppBundle to App.
Move the files into your Symfony 4 project.
Some of the Symfony team also set out to provide similar code generation in a MakerBundle. As far as I can tell there is nothing for generating ODM-style entities, but you could open an issue or contribute something for this yourself.
For reference see: https://github.com/doctrine/DoctrineBundle/issues/729
The MongoDBBundle requires you have "Bundle" but with Symfony flex you are using FrameworkBundle and not "AppBundle" unless you created it before, I think you have not created it, so it tells you that it does not exist.
You can not use the "FrameworkBundle" either beacause FrameworkBundle have other namspace and other base path, so the command to generate the documents does not know where the files are.
After a lot of time figuring out how to diry-fix it:
Create custom Bundle inside your project (EX: AppCoreBundle)
Define custom mapping inside packages/doctrine_mongodb.yaml (change type value if you use xml or yaml)
doctrine_mongodb:
auto_generate_proxy_classes: '%kernel.debug%'
auto_generate_hydrator_classes: '%kernel.debug%'
connections:
default:
server: '%env(MONGODB_URL)%'
options: {}
default_database: '%env(MONGODB_DB)%'
document_managers:
default:
auto_mapping: true
mappings:
AppCoreBundle:
is_bundle: true
type: annotation
dir: '/Document'
prefix: App\CoreBundle\Document
alias: AppCoreBundle
Change the directory "src" to "App" to avoid issues then update psr-4 namaspace inside composer.json: "App\\": "App/"
Move all Documents to the Bundle Document directory and change package names.
You will also have to change ALL references to "src" directory in your project, for example in services.yaml.
Then execute: rm -rf var/cache/* && composer install to force to clear cache and refs.
Then execute: php bin/console doctrine:mongodb:generate:documents AppCoreBundle
Important!
This solution solves compatibility problems with MongoODMBundle with Symfony 4 but it is not a correct way. The right fix involves modifying the behavior of the bundle or Symfony 4.

Unknown Entity namespace alias 'PirastruFormBuilderBundle'

It has been now 2 days that i'm looking for a solution to this error but in vain :
Unknown Entity namespace alias 'PirastruFormBuilderBundle'
in fact i installed the sonata form builder with sonata page bundle using the composer but i don't know why i'm getting this error.
i don't know which part of codes should i copie here so please don't hesitate to ask me for it
sonata_form_builder:
resource: '#PirastruFormBuilderBundle/Controller/FormBuilderController.php'
type: annotation
Thanks !!
EDIT
when i run this : php app/console config:dump-reference
PirastruFormBuilderBundle | pirastru_form_builder |
and when i run this : php app/console doctrine:mapping:info i got
[Exception] You do not have any mapped Doctrine ORM entities
according to the current configuration. If you have entities or
mapping files you should check your mapping configuration for errors.
well i managed to solve the problem ! i had just to add a getManager in my FormBuilderBlockService
This problem can be caused by some (mis)configurations :
Bundle
app/console config:dump-reference
This command let you know if the Bundle is referenced.
Mapping
app/console doctrine:mapping:info
This command let you know if the Bundle is mapped.
It's typically a mapping issue :
Unknown Entity namespace alias '***Bundle'
The better solution is to add auto_mapping to true in config.yml, like this :
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
But it can be solved by others way, you can have a look to : Symfony : What is the meaning of auto_mapping and auto_generate_proxy_classes
Best regards,

Nothing to update "Doctrine"

I'm using Symfony2 with Doctrine to try and update a table schema. I was able to create the table. I was also able to populate the table. However after updating the comments in the Entity (I wanted some fields to become nullable), those changes did NOT get picked up.
I did create the entity with the "Annotations" option chosen. But when I added this line "nullable=true" to the Entity on the field imageName nothing happens. ie: when I run "./app/console doctrine:schema:update" I get the following output "Nothing to update - your database is already in sync with the current entity metadata."
Note, I have tried deleted the table via: ./app/console doctrine:database:drop --force and then recreating it via: ./app/console doctrine:database:create and then also ./app/console doctrine:schema:create but it STILL does not add my updated nullable field to imageName.
I was able to figure this out. I first of all created my entity "Foobar" using yml as the Configuration format. I then wanted to use "annotation" as the configuration format so I manually deleted the Entity folder (I only had one table created), however I did NOT delete the configuration yml in the Resources/config/doctrine/Foobar.orm.yml.
Thus when I created the entity again, this time using the annotation as the configuration format, it was still linking to the yml configuration. Removing that solved all the troubles.
I have however decided to stick to yml as I feel it is a little easier to read than the Doctrine Metadata found in the comments.
I had been stuck with this for almost 2 days. Removing all the file in /src/AppBundle/Resources/config/doctrine resolve my issue.
For me, the key was to clear Redis cache.
php app/console redis:flushdb
I has this problem too. Have you right annotation before class declaration?
/**
*
* #ORM\Entity <- this does the trick
*/
class MyEntityName
{
...
Check doctrine.yaml config file for orm mappings:
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
As you see here all entities should have prefix (namespace) App\Entity
You have to check your entities namespace, it should be App\Entity or whatever you want in config

How to install Doctrine Extensions in a Symfony2 project

I guess that this is really trivial and stupid question, but I don't know how to install Doctrine Extensions - https://github.com/beberlei/DoctrineExtensions in my Symfony2 project. I need them because of the MONTH, YEAR functions. Where should I put their folder? And should I put the whole DoctrineExtensions folder? And where to write this:
<?php
$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', "/path/to/extensions");
$classLoader->register();
In a separate file? Where to put it and how to call it?
And then is this all I need to use them:
public function findOneByYearMonthDay($year, $month, $day)
{
$emConfig = $this->getEntityManager()->getConfiguration();
$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');
Thank you very much in advance and sorry once again for the question, but I couldn't find a tutorial (which makes me feel even more guilty, because I guess it's too trivial when there isn't even a tutorial)
You can install it via composer. Just add it to your composer.json and then php composer.phar update beberlei/DoctrineExtensions
"beberlei/DoctrineExtensions": "*",
Then you can register functions to your ORM
doctrine:
orm:
auto_generate_proxy_classes: %kernel.debug%
entity_managers:
default:
auto_mapping: true
dql:
datetime_functions:
MONTH: DoctrineExtensions\Query\Mysql\Month
YEAR: DoctrineExtensions\Query\Mysql\Year
There also is a nice fork by wiredmedia of #beberlei which includes even more datetime_functions like DATE() itself:
This is an unsanctioned fork of https://github.com/beberlei/DoctrineExtensions since he seems to have gone off grid and is not merging pull requests.
Unfortunately Version 0.1 just includes the fork and not all the functions.
We are waiting for a stable release:
Please create taged stable version for set in my composer #2
But you can add them manually unitl a stable version is out.
Here is how to use DoctrineExtensions in the context of Symfony using the DoctrineBundle.
First install the package DoctrineExtensions:
composer require beberlei/doctrineextensions
Then add to your doctrine configuration (doctrine.yaml file) the DQL functions you want to include in your application:
doctrine:
# Register types this way in the dbal config part
dbal:
types:
carbondatetime: DoctrineExtensions\Types\CarbonDateTimeType
# Register DQL functions in the ORM part
orm:
dql:
string_functions:
FIND_IN_SET: DoctrineExtensions\Query\Mysql\FindInSet
numeric_functions:
SIN: DoctrineExtensions\Query\Mysql\Sin
datetime_functions:
DATE: DoctrineExtensions\Query\Mysql\Date
This is an example, feel free to adjust to your needs (you can remove sections).

Can't enable SoftDeleteable in Symfony2 - Unrecognized options "filters"

I'm having problems trying to activate the SoftDeleteable filter in StofDoctrineExtensionsBundle. I configured it as described in the manual:
# app/config/config.yml
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
But this is what I get:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
Unrecognized options "filters" under "doctrine.orm.entity_managers.default"
Running bin/vendors update didn't help. What could be wrong?
First, using bin/vendors update is a bad idea because it sets all the vendors to their latest versions. You should use bin/vendors install only.
Second, make sure you are using the 1.0.x branch of StofDoctrineExtensionsBundle, because the master branch is not compatible with Symfony 2.0.x.
You can just do it yourself, it is not hard, saving you from installing another bundle:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/filters.html

Resources