Symfony "Invalid env" - only "word" characters are allowed - php

I'm currently trying to get a Symfony project up and running. The problem is when I try to setup the database I keep getting
[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
Invalid env(resolve:DATABASE_URL) name: only "word" characters are allowed.
I edited the .env file to have my local database credentials. The DATABASE_URL variable looks like the following:
DATABASE_URL="mysql://user:pass#192.168.10.10:3306/db_name?charset=utf8mb4&serverVersion=5.7"
Where of course, user, pass and db_name are the credentials.
Any idea on how to solve this problem?

I found something in doctrine recipe. It's likely because you are using symfony 3.3 with symfony/flex so you would need to remove the resolve keyword, ie. in your config/packages/doctrine.yaml :
doctrine:
dbal:
url: '%env(DATABASE_URL)%'
See this news for more information

Related

Symfony 3.2.1 removes underscore from yml file value

I tried to upgrade from Symfony 3.1.8 to 3.2.1 using composer. During the cache clear I received an error stating
An exception occured in driver: SQLSTATE[HY000] [1045] Access denied
for user '########'#'IpAddress' (using password: YES)
however my database user name should be #######_#, notice the underscore in the second to last character position. Looking in the parameter.yml file I found that my database_user had been changed to remove the _ from the username (the underscore was stripped). I then added back my underscore, deleted the dev cache, and tried the application again. I received the same error as before.
Since composer also updated doctrine and the FOSuser bundle at the same time I reverted the update and went back to my prior configuration by reverting my composer.lock file. I then ran my Symfony application again in the version 3.1.8 and it worked fine.
To isolate the problem further I used composer and upgraded ONLY symfony from 3.1.8 to 3.2.1. The upgrade produced errors at the cache clear with the above error message, and in my parameters.yml file my database_user again had the underscore removed from the username. I re-inserted the underscore, however I continue to get the error messages:
An exception occured in driver: SQLSTATE[HY000] [1045] Access denied
for user '########'#'IpAddress' (using password: YES)
If I add a letter to the end of my database_user so it is "#######_#a" the error message shows the _
An exception occured in driver: SQLSTATE[HY000] [1045] Access denied
for user '#######_#a'#'IpAddress' (using password: YES)
If I then delete the letter the error message reverts to no underscore
An exception occured in driver: SQLSTATE[HY000] [1045] Access denied
for user '########'#'IpAddress' (using password: YES)
This leads me to believe that some change in Symfony between 3.1.8 and 3.2.1 strip underscores from the database_user in the parameters.yml file. Unfortunately I am not skilled enough to figure out where that code may be.
With help from github and a bit of reading I found that this is related to changes in how numeric values are handled in Symfony 3.1 (Blog Post). They now automatically strip underscore characters from all number values in yml files.
However in my case my username (provided by my web hosting company) was all numbers with an underscore in the second to last character and should be parsed as a string. This now gets interpreted by Symfony (in compliance with the YAML specifications) as a number and the underscore is stripped.
To solve this I simply added quotes around my username in the yml file and Symfony started handling the value correctly and I was able to run my application.

Sylius Database Credentials Not Updating

I'm new to Sylius, and am configuring a site for the first time on MAMP Pro. I've installed Sylius via Composer, but realized that the database credentials I provided during install were not correct. After this, I went into app/config/parameters.yml as per the documentation, and updated the following credentials:
database_driver: pdo_mysql
database_host: localhost
database_port: null
database_name: mydb
database_user: root
database_password: root
What's happening is every time I try to access the site, it throws a 500 error. Upon checking my log, each time I try to access it, I get an error about access being denied to the DB:
[15-Aug-2016 13:49:34 America/New_York] PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES) in vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:43
The strange thing is the error log also shows this:
vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php(41): Doctrine\DBAL\Driver\PDOConnection->__construct('mysql:host=127....', 'root', 'secret', Array)
These are the old credentials (notice the password 'secret' instead of 'root', and the host '127.0.0.1' instead of 'localhost'. It appears that despite updating my parameters.yml with the correct credentials, Sylius won't stop trying to connect with the original credentials I used during setup. Is there another file that this could be stored in that's not mentioned in the documentation? Could it be caching the old credentials, and in need of some clearing? I'm at a loss for what's happening here.
EDIT:
I've also tried updating app/config/parameters.yml.dist with the proper credentials, to no effect.
EDIT 2:
I've done an entire search of the project for the name of my old database, and have updated each instance to the new database name. There are no files that reference the old DB name. Then I try and run:
php app/console cache:clear --env=dev --no-debug
and it throws errors:
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[08006] [7] FATAL: database "old_database_name" does not exist
despite old_database_name not being referenced in any file. Then if I do a search for old_database_name AFTER running the cache clear, it overwrites my new database name in app/cache/de_/ap_DevProjectContainer.php with the old name again, every time. I can't even figure out where it's getting the old database name from, after I overwrite every instance of it. What's happening here??
The default Sylius configuration uses a different database for production and development. In your case, this would be sylius from prod and for the dev environment _dev is appended to the datbase name. This is expected behavior and not a bug.
You can find the configuration at the end of app/config/config_dev.yml. If you remove the following lines, both environments use the same database.
doctrine:
dbal:
dbname: %sylius.database.name%_dev
https://github.com/Sylius/Sylius/issues/2362
In this case, old_database_name was the same as my new database name, only it contained the suffix '_dev'. I've now realized that this is because I'm in the dev environment. There doesn't seem to be any easy way I've found to remove this suffix, so I'm just going to roll with it. Would still like an answer on how to remove this, but for now I've answered my own question.

Symfony 2 Doctrine MS SQL Error

I have started working on a Symfony 2.3 project where I need to convert a financial website written in classic Asp to Php.
The client has Ms-Sql database which is to be used with Symfony 2.3 and doctrine, I have made the database and loaded the empty tables from the schema files.
Now whenever i try to generate entities from the database(Ms-Sql) I get the following error:
Doctrine\DBAL\DBALException
Unknown database type timestamp requested, Doctrine\DBAL\Platforms\SQLServer2008Platform may not support it
Does anyone know how to solve the problem or what should I do to avoid such a situation?
Kindly help me as I am new to Symfony 2.3, Thank you
You can add your own types following doctrine doc about the subject :
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/mysql-enums.html
this is concerning enum type but you can use it with any type like timestamp and link it to string type or whatever other supported type by your database which fit you best.
src/My/Project/MyProjectBundle.php
public function boot()
{
$em = $this->container->get('doctrine.orm.entity_manager');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('timestamp','string');
}
and normally all your timestamp types should be translated as string.
The configuration below works on Synfony 3.2
1) in config.yml add the new type under types and map it under mssql connection
# Doctrine Configuration
doctrine:
dbal:
types:
timestamp: AppBundle\Type\Timestamp
..
..
connections:
mssql:
driver: sqlsrv
host: "%mssql_host%"
port: "%mssql_port%"
dbname: "%mssql_db_name%"
user: "%mssql_user%"
password: "%mssql_password%"
mapping_types:
timestamp: timestamp
2) Create the class AppBundle\Type\Timestamp as for the following url:
https://github.com/mmerian/doctrine-timestamp/blob/master/lib/DoctrineTimestamp/DBAL/Types/Timestamp.php
3) generate entities from the database with the following command:
php bin/console doctrine:mapping:import --force AppBundle xml --em=mssql

"Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem

I had an entity class in Aib\PlatformBundle\Entity\User.php
I had no problems trying to create its form class through
php app/ console doctrine:generate:form AibPlatformBundle:User
Now I have change the namespace to Aib\PlatformBundle\Entity\Identity\User, but when I try to generate the form with the task I said before
it says:
"Class Aib\PlatformBundle\Entity\User is not a valid entity or mapped
super class."
This is the file content:
<?php
namespace Aib\PlatformBundle\Entity\Identity;
use Doctrine\ORM\Mapping as ORM;
/**
* Aib\PlatformBundle\Entity\Identity\User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Aib\PlatformBundle\Entity\Identity
\UserRepository")
*/
class User
{
...
Any idea?
symfony2.0.4
Had this problem - don't forget the annotation * #ORM\Entity like below:
/**
* Powma\ServiceBundle\Entity\User
*
* #ORM\Entity
* #ORM\Table(name="users")
*/
Had this problem yesterday and found this thread. I created the entity with the mapping in a new bundle (e.g. MyFooBundle/Entity/User.php), did all the configuration according to the docs but got the same error from above when trying to load the app.
In the end I realized that I wasn't loading MyFooBundle in AppKernel:
new My\FooBundle\MyFooBundle()
A great way to debug this is to run this command:
app/console doctrine:mapping:info
Do check your config.yml file, should be containing something like this:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
types:
json: Sonata\Doctrine\Types\JsonType
orm:
auto_generate_proxy_classes: %kernel.debug%
# auto_mapping: true
entity_managers:
default:
mappings:
FOSUserBundle: ~
# ApplicationSonataUserBundle: ~
YourUserBundle: ~
SonataUserBundle: ~
Add your own bundle to the mappings list.
I resolved this issue by setting $useSimpleAnnotationReader=false when creating the MetaDataConfiguration.
I solved this by passing false as the second parameter to Doctrine\ORM\Configuration::newDefaultAnnotationDriver.
It took me a while of digging through Google and source code.
My case was sort of special since I was using a mapping pointing to another directory unrelated to the Symfony installation as I also had to use legacy code.
I had refactored legacy entities and they stopped working. They used to use #Annotation instead of #ORM\Annotation, so after refactoring it simply failed to read the metadata. By not using a simple annotation reader, everything seems to be okayish.
In my case the problem was solved by changing my servers cache from eAccelerator to APC.
Apparently eAccelerator strips all the comments from files which breaks your annotations.
big thx to Mark Fu and mogoman
I knew it had to be somewhere in the config.yml... and being able to test it against the
app/console doctrine:mapping:info
really helped!
In fact, this command just simply stops at an error... no feedback, but when everything is fine you should be able to see all your entities listed.
I resolved the same exception by deleting a conflicting autogenerated orm.php file in the bundle's Resources/config/doctrine folder; according to the documentation: "A bundle can accept only one metadata definition format. For example, it's not possible to mix YAML metadata definitions with annotated PHP entity class definitions."
Very high possibility that you have PHP 5.3.16 (Symfony 2.x will not work with it). Anyway you should load check page on http://you.site.name/config.php
If you had project not worked on hosting server, next lines must be removed in "config.php":
if (!in_array(#$_SERVER['REMOTE_ADDR'], array(
'127.0.0.1',
'::1',
))) {
header('HTTP/1.0 403 Forbidden');
exit('This script is only accessible from localhost.');
}
Goodluck!
Entity must have proper Entity and Table annotations (order may matter so try both)
If there is custom repository it MUST be accessed via Entity class itself ($entityManager->getRepository('your ENTITY class name')), as calling it via Repository class name will trick Doctrine into thinking Repo class must be an entity itself. Silly, but Doctrine does not make hard distinction.
My mistake was I was doing
$em->getRepository(EntityRepository::class)
instead of
$em->getRepository(Entity::class)
In my case, I was too zealous during a refactor and had deleted a doctrine yml file!
In my case on my mac I was using src/MainBundle/Resource/Config/Doctrine, of course it worked on Mac but it didn't work on production Ubuntu server. Once renamed Config to config and Doctrine to doctrine, the mapping files were found and it started working.
In my case after making make:entity i tried the following command
php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
which generates the entity from the database
However, this Command don t provide the getters and setters that will cause you unknown method error (getId for example) if you are using it inside a controller or you ll use it later So i decided to go back to the generated entity from the
php bin/console make:entity
so i can bring back the missing methods but unfortunately this caused me the error class is not a valid entity or mapped superclass.
next to prevent this error i haven 't patience to read this documentation
[1]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/tutorials/override-field-association-mappings-in-subclasses.html#override-field-association-mappings-in-subclasses especially i m using attributes in the place of annotation,
therefore i brought back the generated entity and just adding the following command which generates getters and setters $ php bin/console make:entity --regenerate App
saved my life ,though this solved my problem but i didn t figure out why in the first case it caused me the error of this topic
I got rid of the same error message as in your case by using app/console_dev instead of just app/console

Symfony Jobeet Tutorial Day 3, databases.yml error

I'm new to Symfony and I'm going through the Jobeet tutorial v1.4 for
Doctrine. I am currently stuck on Day 3. I've followed all the
instructions on configuring the database and building models and
modules; however, when I try to access
"http://localhost:8080/frontend_dev.php" I receive the following
error:
'Configuration "config/databases.yml" does not exist or is unreadable.'
My config/databases.yml file looks like this:
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: 'mysql:host=localhost;dbname=jobeet'
username: root
password: mysecret
Creating the tables and loading the fixtures seem to work fine after checking the database with phpmyadmin. Any help would be appreciated.
Thank you!
I tried what's in the Jobeet from Day 1 to 3 and it works on my local machine, maybe what you have there is some file permission issue that is preventing your symfony to load the config/databases.yml so please change permissions as needed (chmod in linux).
Not sure if this will solve the problem but I think this is worth to try, execute php symfony cc or simply clear the cache folder and load up again your app in the browser, since the config/databases.yml is to be cached as config_databases.yml.php (you can take a look on /cache/frontend/dev/config/)
phpmyadmin? arrggggghhhhhhh =P
Anyway, your identation is wrong.
It should be like this:
all:
doctrine:
class: sfDoctrineDatabase
param:
dsn: "mysql:host=localhost;dbname=jobeet"
username: root
password: mysecret
As for the error, if you're on a mac, change the permissions using chmod. :-)
If you're stuck on a problem with databases.yml, this symfony databases.yml configuration reference could be useful: www.symfonyreference.com/databases-yml

Categories