Symfony3 Generate Entites from DB - php

New to Symfony... Trying to create Entities from an existing DB. I am attempting to following the instructions at http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html
Long story short it seems that I simply need 3 commands:
$ php bin/console doctrine:mapping:import --force AppBundle xml
$ php bin/console doctrine:mapping:convert annotation ./src
$ php bin/console doctrine:generate:entities AppBundle
Note: I Modified the name of the bundle from AcmeBlogBundle to AppBundle because I am just using the default bundle. I also am using Symfony 3, everything I look up online seems to reference symfony2
Anyway, first command seems to run fine. I get orm.xml files generated in my ./src/AppBundle/Resources/config/doctrine/ path
Commands 2 and 3 seem to be where my troubles begin.
Command 2 results in
No Metadata Classes to process
Command 3 results in (obviously command 2 didnt work correctly but tried anyway)
[RuntimeException] Bundle "AppBundle" does not contain any mapped entities.
I'm at a loss here.
Contents of config.yml are:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
Put parameters here that don't need to change on each machine where the app is deployed
http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~
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.root_dir%/data/data.db3"
# 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 }
I did end up getting past this by adding i think the "--from database" flag in one of the commands above. I think the second command. I'm pretty sure youre correct that my config is not expecting the xml files so thats why it cant find them.

you can run the following command straightaway after configuring your parameters.yml.
php bin/console doctrine:mapping:import --force AppBundle annotation
then you can run this command to generate entities
php bin/console doctrine:generate:entities AppBundle
It's also important to try your database connection before running any of those commands. Try to fetch values with
php bin/console doctrine:query:sql "SELECT * FROM your_table_name"

Related

Symfony3 - Assets not working properly in Production

My SF3 application is working just fine in dev built-in PHP server. However some assets give me an error when getting to a production Apache server : the images are showing a 404 error, and a Bundle triggers a 505 code. Here's 2 screens of the Chrome console where you can see the errors :
Dev:
Prod:
I've been trying many things (checking the Apache conf files, emptying the cache, the install:assets command even thoug I'm just using the classic Twig syntax and no Assetic, etc) and browsed tons of topics over the internet in vain.
Here's my config.yml file just in case :
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#ABundle/Resources/config/services.yml" }
- { resource: "#AnotherBundle/Resources/config/services.yml" }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
# save_path: /var/lib/php/sessions
fragments: ~
http_method_override: true
assets: ~
php_errors:
log: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
paths:
"%kernel.root_dir%/../app/Resources/views/modules": modules
# 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.root_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 }
I'm really stuck on this one, so thank you in advance guys.
have you tried: php bin/console assetic:dump --env=prod --no-debug ?
also, your assets should be in src/YourBundle/Resources/public directory. Then when you install them with php bin/console assets:install --symlink they should be copied or symlinked to web/bundles/your directory
So the problem was that my assets were in the web folder instead of the Resources. Then the assetic:dump command worked perfectly.
Thanks a lot

Doctrine cannot connect to database in Symfony2 using Mamp

I'm trying to create database through doctrine.
php app/console doctrine:database:create
When I run the command above, I receive the following errors:
[Doctrine\DBAL\Exception\ConnectionException]
An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000] [2002] Connection refused
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
What I have tried
This symbolic link creation did not work for me.
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
I've tried with both
database_host: localhost and database_host: 127.0.0.1 nothing changed.
I know that my MySQL port is 8889 as I can see on MAMP.
I could not check the option Allow network access to MySQL under MySQL tab on MAMP since I do not have MAMP Pro
Here are my configuration files
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#FatihTestBundle/Resources/config/services.yml" }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
cache: false
# 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.root_dir%/data/data.db3"
# 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 }
parameters.yml:
# This file is auto-generated during the composer install
parameters:
database_host: localhost
database_port: 8889
database_name: todo
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
secret: fgnfgmngfm
php app/console cache:clear
The problem was because of cache, I received error messages not matter what I did. The command above that clears the cache and
changing the database port to 3306 in both MAMP and paramteters.yml has solved the problem.
Check your DNS settings. If you are not using a VPN, turn it off.
You can test the connection with "Sequel PRO"
Hostname : localhost - 127.0.0.1 - IP Adress
Password : 1234 - root
If they do not solve the problem check the user permissions.
mysql database -> user table

Symfony internationalization

I am having a strange problem with translation in Symfony 2.
This is my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
framework:
#esi: ~
translator: { fallbacks: ['%locale%'] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 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 }
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\User
profile:
form:
type: app_user_profile
parameters.yml
...
locale: ru
And, of course, I have messages.en.yml and messages.ru.yml.
Even when locale is set to 'ru' it displays English text, and $request->getLocale() is always returning 'en'.
What could be the problem and where is it overset to 'en' if not in configs?
The locale parameter in the config.yml file is just a fallback in case a translation for the requested language is not available.
Calling $request->getLocale() will return the language configuration of the browser from which the HTTP-Request was sent.
If you want the translation to be forced into another language take a look at this answer: https://stackoverflow.com/a/14331838/1173391
You need to create a twig extension that allows you to replace ru with en in your path route (and vice versa) :
{# yourProject/app_dev.php/ru #}
Look at this tutorial
http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
put in your routing.yml a prefix with the locale like this:
routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
prefix: /{_locale}
requirements:
_locale: "%app.locales%"
config.yml
parameters:
locale: en
app.locales: en|ru
Did you add:
{% trans_default_domain "message" %}
on top of your twig template file where the translation keys exist and did you clear cache?

SQLSTATE[HY000] [2002] Connection refused - MAMP & Symfony 2

I've a problem with symfony.
When I try to run this command :
php app/console doctrine:schema:update --force
And I've error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Here is my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
framework:
#esi: ~
#translator: { fallback: "%locale%" }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
unix_socket: /tmp/mysql.sock
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
And my parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: sil19
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: e7f97888e382ddd2e235dc7da6fc39346b37096c
database_path: null
I don't know where the problem is from. So if someone have any idea. Thanks in advance
I'had to check "Allow network access to Mysql" to make it work on MAMP.
I'have got the same error when trying to connect SYMFONY 4 to MAMP Mysql:
An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused
For my case, it was a wrong Mysql port, just changed 3306 to 8889 to fix it
DATABASE_URL=mysql://root:root#127.0.0.1:8889/db_name
in my .env file
Run at the full path, like this:
/Applications/MAMP/bin/php/php5.4.4/bin/php bin/console
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the
https://blog.liplex.de/symfony-console-connection-refused-with-mamp/

Warning: Missing argument 1 for FOS\UserBundle\Form

Im getting this warning and cant seem to find the solution, i have tried various answers but still nothing
Warning: Missing argument 1 for FOS\UserBundle\Form\Type\RegistrationFormType::__construct(), called in C:\xampp\htdocs\symfony-bootstrap\src\sava\UserBundle\Controller\VerUsuarioController.php on line 77 and defined in C:\xampp\htdocs\symfony-bootstrap\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Form\Type\RegistrationFormType.php on line 25
Notice: Undefined variable: class in C:\xampp\htdocs\symfony-bootstrap\vendor\friendsofsymfony\user-bundle\FOS\UserBundle\Form\Type\RegistrationFormType.php on line 27
my services.yml
# src/Acme/UserBundle/Resources/config/services.yml
services:
sava_user.registration.form.type:
class: sava\UserBundle\Form\RegistroTypes
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: sava_user_registration }
this is my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "#savaUserBundle/Resources/config/services.yml" }
framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_proxies: ~
session: ~
fragments: ~
http_method_override: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
form:
resources:
- 'MopaBootstrapBundle:Form:fields.html.twig'
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
filters:
less:
node: /Arturo/bin/node
node_paths: [/Arturo/lib/node_modules]
# lessphp:
# node: ["C:\\Program Files\\nodejs\\node.exe"]
# node_paths: ["C:\\Users\\Arturo\\AppData\\Roaming\\npm\\node_modules"]
apply_to: "\.less$"
cssrewrite: ~
cssembed:
jar: %kernel.root_dir%/Resources/java/cssembed-0.3.6.jar
apply_to: "\.css$|\.less$"
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
apply_to: "\.css$"
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar
# 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
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
fos_user:
db_driver: orm
firewall_name: main
user_class: sava\UserBundle\Entity\User
registration:
form:
type: sava_user_registration
this is my controlleraction
public function modificarUserAction(Request $request,$id) {
$em = $this->getDoctrine()->getManager();
$User = $em->getRepository('savaUserBundle:User')->find($id);
$formulario = $this->createForm(new \sava\UserBundle\Form\RegistroType(),$User);
$formulario->handleRequest($request);
if ($request->getMethod() == 'POST') {
$em = $this->getDoctrine()->getManager();
$em->persist($User);
$em->flush();
return $this->forward('savaUserBundle:VerUsuario:vertodos');
}
else
{
return $this->render('savaUserBundle:Default:modificarUser.html.twig', array(
'formulario' => $formulario->createView(),'User'=> $User
));
}
its working fine but i keep getting those errors and have no clue in how to get rid of them.
This is a very common question for people who are new to dependency injection containers. You defined your service but then used new RegistroType() to create the instance. The thinking is that php will somehow realize that you want a service and use the container. Nope. The 'new' operator knows nothing about the service container and simply tries to create a new instance without any parameters. Hence the error message.
You obviously read the "defining form type as a service" section (http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services) and I can see where your confusion comes from. The manual could be a bit clearer perhaps.
Bottom line is that you need to use the form type's alias instead of the new operator:
// Replace
$formulario = $this->createForm(\sava\UserBundle\Form\RegistroType(),$User);
// With
$formulario = $this->createForm('sava_user_registration',$User);
You will get a different error the first time you try this. You have a typo in your service definition but I'll leave that for you to debug.

Categories