Expected int, but got string for redis port - php

I have this configuration for docker:
# docker-compose.yml
environment:
REDIS_SERVER: redis
REDIS_PORT: 6379
And then in symfony I use this to configure parameters:
# parameters.yml
redis_port: %env(REDIS_PORT)%
And then I use the parameter to configure Picmore's cache:
```yaml
# pimcore.yml
cache:
pools:
redis:
enabled: true
connection:
port: '%redis_port%'
But I get the error
Invalid type for path "pimcore.cache.pools.redis.connection.port". Expected int, but got string.
The error is clear but I can't solve it, I tried different options:
redis_port: '%env(REDIS_PORT)%'
redis_port: '%env(int:REDIS_PORT)%'
But I get the same error. How can I fix this?

You are using Symfony 3.3, but the int environment variable preprocessor was not added until Symfony 3.4, according to this.
I don't think you'll be able to use env vars to configure this value, since environment variables are strings by default.
Your options are to either use different parameters files for different environment, as things used to be done back then (which I understand can be inconvenient when using containers); or upgrade to a more recent version of Symfony.

Related

Doctrine insists on trying to use PostgreSQLDriver despite contrary config

I am setting up a Symfony 5.2 app, using doctrine/doctrine-bundle 2.4 and doctrine/orm 2.9. My doctrine.yaml config file looks like this:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_mysql'
server_version: '5.7'
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
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
The problem is that when I run doctrine:fixtures:load, I get the following output:
In AbstractPostgreSQLDriver.php line 102:
An exception occurred in driver: could not find driver
In Exception.php line 18:
could not find driver
In PDOConnection.php line 39:
could not find driver
It looks like Doctrine is trying to use the Postgre driver, though I have specified MySQL. Is there another place where I need to configure this? If so, how?
===
EDIT: It looks like DATABASE_URL is not available inside my Docker container, which is probably the source of the problem. I've verified that DATABASE_URL is defined in my .env file, and that my .env file is in the same folder as my docker-compose.yml file. Also, I have a setting like this for the environment variable in that docker-compose file:
environment:
- DATABASE_URL
So I'm not quite sure what's wrong here.
I think you have a wrong DATABASE_URL in your .env file. Please check if it starts with mysql://.
I believe symfony uses dotenv package to load the values from .env to the S_ENV superglobal. You need to debug whether your dotenv (and there is a .env in your project and it is not .env.local or .env.dist) is loaded properly.
It has happened to me sometimes when I was starting, typically when cloning a Github project which has a default .env file created by Doctrine when it's installed through composer that by default Doctrine uses PostgreSQL URL.
See how many .env files have you document and I'm sure that someone overrides your URL connection.
See this StackOverflow post to know the difference between the .env.* files What is the difference between .env.local and .env.development.local

Nginx fpm Laravel container inside kubernetes not picking up the environment variables

I have one laravel docker container which is build with a custom nginx + php-fpm docker image.
I have deployed successfully to a k8s cluster and can access properly, also logging into the pod and running env I can see all the environment variables being set successfully from my k8s configmap
In the laravel code I read the environment variables like this:
For example at SomeController.php have the following code:
$apiCode = env('API_CODE');
// also tried like this $apiCode = getenv('API_CODE'); still not successful in fetching
My problem and this question is that the env vars are always read empty inside the php code even though inside the pod with env command I can see them properly set, somehow the php code cannot find them.
(I am not caching laravel config so that case we can exclude, also tried with the command php artisan config:clear beforehand, still same result cannot fetch the env vars within the php)
In the kubernetes definition yml I attach the configmap to env variables like below and see them defined properly inside pod:
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
labels:
tier: api
spec:
replicas: 1
selector:
matchLabels:
tier: api
template:
metadata:
labels:
tier: api
spec:
containers:
- name: api
image: somenging-fpm-image:latest
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: api-config-env-file
For the moment I am lost and without any idea why this might happen.
I thought initially that maybe configmap was created late after pod started (php fpm somehow process then did not pick on start the env)
To verify and exclude that case, I destroyed the pod and recreated the deployment+pod in order to use the already existing configmap, and still the result was the same php did not pick up the env vars that were present in pod
I could log into pod by kubectl exec -it [podnamehere] /bin/bash and run env there and could see properly env vars set from my configmap api-config-env-file, but the code would always have them empty not be able to read them
I encountered this problems only when there are redis or caching system.
But you already state that
(I am not caching laravel config so that case we can exclude, also tried with the command php artisan config:clear beforehand, still same result cannot fetch the env vars within the php)
So double check if you run any command like php artisan optimize. It will save the cache to redis, and the laravel application will pick the setting up from there.
Just to make sure you don't miss anything.
Try:
php artisan optimize:clear
This command will do the following:
Cached events cleared!
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!

How to use mysql instead of mariadb in symfony 5

i think this might be a stupid question for some, but i swear that i can't find the answer by myself actually.
I'm still learning Symfony so please be nice.
Actually i have set up the access to my database in the .env file :
[...]
DATABASE_URL=mysql://root:db_password#127.0.0.1:3308/db_name
[...]
And then i have define the environnement variable in the config/doctrine.yaml :
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: Symfony
user: root
password: null
host: localhost
server_version: '8.0'
driver: pdo_mysql
charset: UTF8
So i run the following command :
> php bin/console doctrine:database:create
But the 'Symfony' database have been created under mariadb.
:( I don't understand why because i have define precisely that i use a 8.0 version and this version is not a mariadb version.
I've also try this :
# .symfony/services.yaml
mydatabase:
# supported versions: 5.7, 8.0
type: oracle-mysql:8.0
disk: 1024
As the symfony documentation saying to do, but it still not working at all.
I have got the following error message as mydatabase is not a symfony services :
C:\Users\vincp\Symfony\charming_tuto>php bin/console doctrine:database:create
In FileLoader.php line 173:
There is no extension able to load the configuration for "mydatabase" (in "C:\Users\vincp\Symfony\charming_tuto\con
fig/services.yaml"). Looked for namespace "mydatabase", found ""framework", "sensio_framework_extra", "twig", "twig
_extra", "web_profiler", "monolog", "debug", "webpack_encore", "doctrine", "doctrine_migrations", "maker"" in C:\Us
ers\vincp\Symfony\charming_tuto\config/services.yaml (which is being imported from "C:\Users\vincp\Symfony\charming
_tuto\src\Kernel.php").
I wanna use mysql because this is the DBMS that i use usually.
(If you think that i should use mariadb instead of mysql i'm open to that, but give me some arguments. I'd be really interseted in)
Maybe it is a server issue. So first, make sure that MySQL is running on your server besides MariaDB (how to do it depends on the type of server you are using).
If yes, it might be a port setting issue: I see that you set the database port to 3308. That is probably the port on which MariaDB is listenning. Try to determine if there is another db running on your server listening to another port (for example 3306 is very often the default MySQL port). To do so, on a Linux server you could try the following command :
netstat -tlnp | grep mysql
If you find only x x.x.x.x:3308 address or so, I bet that MySQL is not running on your server and you only run a MariaDB instance. But if you find addresses on other ports (for example x x.x.x.x:3306) chances are that a MySQL instance is running and listens that other port. If that's the case, you should consequently change the DATABASE_URL in your .env file.

doctrine:schema:update doesn't retrieve production configuration?

I've declared a configuration in app/config/config_prod.yml so I can execute commands on the remote database.
imports:
- { resource: config.yml }
doctrine:
dbal:
driver: %database_driver%
host: the-ip
dbname: the-database
user: the-user
password: the-password
But when I run php app/console doctrine:schema:update --dump-sql -e=prod it still uses the configuration set in parameters.yml.
A var_dump($this->getApplication() in the execute method in Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand shows the right environment doesn't get the configuration from config_prod.yml
public $parameters =>
array(428) {
...
'kernel.environment' =>
string(4) "prod"
...
'database_host' =>
string(9) "127.0.0.1"
Did I encounter a bug or is this command not capable of dealing with different environments?
Short: You have to clear the cache after every configuration change in the prod environment. In the dev environment the cache is rebuild (some parts not) on every request. But not in prod.
Indeed, configuration is compiled into cache. Loading the configuration from YAML/XML on every request would be too heavy. Look for the method getDefaultParameters() in app/cache/prod/appProdProjectContainer.php and you can see, all configuration parameters are merged in a huge big array.
You will notice, the parameters in this array, are not often the same, you configure in your config.yml. The injecting of these parameters depends on the bundle and their specific extension class. See Semantic Configuration for further infos.

Codeception - pointing DB to MAMP MySQL server

Is there a way to point the DB module for Functional tests in codeception to your MAMP MySQL server rather than the built in server?
class_name: TestGuy
modules:
enabled: [Db, Filesystem, TestHelper]
config:
Db:
dsn: 'mysql:host=localhost;dbname=testdb'
user: 'root'
password: 'root'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: false
Codeception uses PDO to connect to the database. It will pass the "dsn" string straight into the PDO constructor. Thus, according to http://www.php.net//manual/en/ref.pdo-mysql.connection.php, your configuration should be the following in order to connect to the MAMP MySQL Server running on port 8889:
class_name: TestGuy
modules:
enabled: [Db, Filesystem, TestHelper]
config:
Db:
dsn: 'mysql:host=localhost;port=8889;dbname=testdb'
user: 'root'
password: 'root'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: false
This is what fixed the issue for me.
In MAMP I ticket the box "Allow network access to MySQL"
My dsn setting in codeception.yml is:
dsn: 'mysql:host=127.0.0.1;port=3306;dbname=la51'
Have you tried this at all - might work for you too..
class_name: TestGuy
modules:
enabled: [Filesystem, TestHelper, WebHelper, PhpBrowser]
config:
PhpBrowser:
url: 'http://localhost/yourapp/'
curl:
CURL_RETURNTRANSFER: true
Let me know how that works for you :)
http://codeception.com/docs/modules/PhpBrowser
I'd double check your mysql table within the dump
Once setup just add something like this to your tests
$I->seeInDatabase('users', array('name' => 'Davert', 'email' => 'davert#mail.com'));
Thiss will check the 'users' table and look for the row with the name & email as supplied in the array - works for me.
Two things—I believe—would fix this your problem
Make sure the module for which you are setting the database parameters is the same. DB is different from Db
MAMP runs its MySQL on port 8889. If you only specify localhost in the dsn string, Codeception will try to connect to a MySQL instance running on port 3306—which is MySQL's default port. Change localhost to localhost:8889
I just fixed this problem myself. Comment for further clarifications if need be.
Cheers
EDIT
Recently, I revisited the project which I used Codeception. This solution did not work for me. I ran into two issues
Timezone Exception You can fix this by...
Making sure the timezone is set correctly in your php.ini file, which is usually in /Applications/MAMP/bin/php/php5.x/conf/
AND/OR aliasing php to use MAMP's by doing alias phpm="/Applications/MAMP/bin/php5.x/bin/php"
[Codeception\Exception\Module] (Exception in Db) This is primarily because of the configuration of the module. I removed the port number from my configuration and everything seems to work fine.

Categories