Adding property to Symfony BaseModel - php

I have the below symfony model and I need to add a name property (see below) to the existing model and then update the database accordingly. What is the proper way to accomplish this? I appreciate any suggestions.
I tried running php bin/console doctrine:database:drop and then php bin/console doctrine:database:create, hoping it'll make the necessary mappings, without any success, I am getting the following error: You have requested a non-existent service "doctrine".
This seems to be overly complicated and I am sure theres a simpler way to accomplish this. I appreciate any suggestions.
composer.json snippet
"php": ">=5.5.9",
"symfony/symfony": "3.0.*",
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"symfony/swiftmailer-bundle": "^2.3",
"symfony/monolog-bundle": "^2.8",
"sensio/distribution-bundle": "^5.0",
"sensio/framework-extra-bundle": "^3.0.2",
NoticeSettings.php
<?php
namespace AppBundle\Model;
class NoticeSettings extends BaseModel
{
protected $table = "notice_settings";
/**
* #Assert\Type(type="string")
*/
protected $notice;
/**
* #Assert\Type(type="string")
*/
protected $name;
}
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
parameters:
locale: en
uploads_directory: '%kernel.root_dir%/../uploads'
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%"
fragments: ~
http_method_override: true
assets:
version: 15
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes: ["form/fields.html.twig"]
globals:
user: "#app.user"
badges: '#app.sidebar_badges'
paths:
'%kernel.root_dir%/../src/AppBundle/Resources/views': ~
'%kernel.root_dir%/../src/AppBundle/Resources/TwigBundle/views': 'Twig'

It looks like Doctrine is not fully configured in your application. This is likely also why the service can not be found.
Please make sure that inside your AppKernel.php the DoctrineBundle is registered (search for new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()).
Next you will need the doctrine configuration. You can check the repository if you need a reference configuration:
# 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
Also make sure, that your parameters.yml and parameters.yml.dist contain the values needed by the configuration. Again you can take the file from the official repository as a reference.

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

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?

Symfony2 FosUserBundle is loosing session

I am using FosUserBundle to register and authenticate users in my website. But i am having troubles with it cause user session is loosen very soon. Sometimes 5 minutes, sometimes 10 minutes, and then user is redirected to the login form again, causing many users to left my website.
This is what i currently have in my project:
composer.json
{"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.6.*",
"doctrine/orm": "~2.2,>=2.2.3,<2.5",
"doctrine/dbal": "<2.5",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"symfony/yaml": "2.7.*#dev",
"symfony/filesystem": "~2.6",
"sensio/distribution-bundle": "~3.0,>=3.0.12",
"sensio/framework-extra-bundle": "~3.0,>=3.0.2",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle": "~2.0#dev",
"friendsofsymfony/jsrouting-bundle": "#stable",
"knplabs/knp-snappy-bundle": "dev-master",
"jms/serializer-bundle": "0.13.*"
},
"require-dev": {
"sensio/generator-bundle": "~2.3"
}
}
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: "#MoovityUserBundle/Resources/config/services.yml" }
- { resource: "#MoovityBackendBundle/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_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
cookie_lifetime: 0
gc_maxlifetime: 14400
fragments: ~
http_method_override: true
# translator: ~
# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
# Assetic Configuration
assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ MoovityBackendBundle ]
#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
# 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: Moovity\UserBundle\Entity\User
registration:
confirmation:
enabled: true
from_email:
address: noreply#mywebsite.com
sender_name: mywebsite
template: MoovityBackendBundle:Mail:mail_fos_user_register_template.html.twig
# template: FOSUserBundle:Registration:email.html.twig
form:
type: moovity_user_registration
resetting:
email:
from_email:
address: resetpass#mywensite.com
sender_name: My Website Reset Password
template: FOSUserBundle:Resetting:email.html.twig
form:
type: moovity_user_resetting
service:
mailer: fos_user.mailer.twig_swift
#fos_rest:
# routing_loader:
# default_format: json
security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
# id: fos_user.user_provider.username
id: fos_user.user_provider.username_email
firewalls:
main:
pattern: ^/
form_login:
login_path: fos_user_registration_register
check_path: fos_user_security_check
provider: fos_userbundle
csrf_provider: form.csrf_provider
# always_use_default_target_path: true
# Redirección despues de loguear
default_target_path: moovity_backend_homepage
logout:
path: fos_user_security_logout
target: fos_user_registration_register
# target: fos_user_security_login
anonymous: true
security: true
remember_me:
key: 57361cdee0bb68f4e4b71f06b28fc07e
lifetime: 6000
What am i doing wrong?
Ok, i have noticed that fosuserbundle is loosing session with Symfony 2.6, so i switched to 2.3 and now it seems to be working.
Thank you very much.
I think the problem is the translation.
Got the same errors, try to remove the locale in your parameter and reset it with ~
Please post if it was the problem

Symfony 2 production app unable to find any Twig template

I made Symfony 2 app what is working well on production and dev mode in my localhost server, but when i pushed it live to my ftp there was a problem. I checked log's and it's looks like Twig has problems with finding templates.
There is log from my server:
[2015-05-18 11:10:21] request.INFO: Matched route "homepage" (parameters: "_controller": "AppBundle\Controller\DefaultController::indexAction", "_route": "homepage") [] []
[2015-05-18 11:10:21] security.INFO: Populated SecurityContext with an anonymous Token [] []
[2015-05-18 11:10:21] request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "Unable to find template "AppBundle:Home:index.html.twig"." at /vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 128 {"exception":"[object] (InvalidArgumentException(code: 0): Unable to find template \"AppBundle:Home:index.html.twig\". at /vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php:128, Twig_Error_Loader(code: 0): Unable to find template \"AppBundle:Home:index.html.twig\". at /vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php:91, InvalidArgumentException(code: 0): Unable to find template \"AppBundle:Home:index.html.twig\" : \"Unable to find file \"#AppBundle/Resources/views/Home/index.html.twig\".\". at /vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php:81, InvalidArgumentException(code: 0): Unable to find file \"#AppBundle/Resources/views/Home/index.html.twig\". at /app/bootstrap.php.cache:2456)"} []
[2015-05-18 11:10:21] security.DEBUG: Write SecurityContext in the session [] []
Have anyone idea what's the problem?
#edit I also put my config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: "#AppBundle/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_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
# 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 }
#for user
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: AppBundle\Entity\User
registration:
form:
type: new_user_registration
#vich file uploader
vich_uploader:
db_driver: orm
mappings:
download_file:
uri_prefix: /files/download
upload_destination: %kernel.root_dir%/../web/files/download
offer_file:
uri_prefix: /files/offer
upload_destination: %kernel.root_dir%/../web/files/offer
inject_on_load: false
delete_on_update: false
delete_on_remove: true
Controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction()
{
return $this->render('AppBundle:Home:index.html.twig');
}
}
#edit2
I don't know it does matter but i use for my project this template https://github.com/juliomatcom/charisma-symfony-php
Ok i found solution. When i copied files from this repository https://github.com/juliomatcom/charisma-symfony-php I have not noticed the 'Views' folder starts from uppercase. When i change it to 'views' everything works fine.
Thanks for everyone who tried help :)!

Compile Error: Declaration of FOS\RestBundle\Request\RequestBodyParamConverter::apply() must be compatible with that of Sensio\Bundle

I'm using symfony2. I'm trying to use the Request Body convert listener from FriendsOfSymfony bundle. I have enabled the request converters on sensio_framework_extra and when enabling the body converter:
fos_rest:
body_converter:
enabled: true
I get this exception:
FatalErrorException: Compile Error: Declaration of FOS\RestBundle\Request\RequestBodyParamConverter::apply() must be compatible with that of Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface::apply() in /var/www/xxx/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Request/RequestBodyParamConverter.php line 32
in /var/www/xxx/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Request/RequestBodyParamConverter.php line 32
at ErrorHandler->handleFatalError(object(ExceptionHandler), array('type' => '64', 'message' => 'Declaration of FOS\RestBundle\Request\RequestBodyParamConverter::apply() must be compatible with that of Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface::apply()', 'file' => '/var/www/xxx/vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/Request/RequestBodyParamConverter.php', 'line' => '32')) in /var/www/xxx/vendor/symfony/symfony/src/Symfony/Component/Debug/ErrorHandler.php line 212
Here is my full config.yml file
imports:
- { resource: parameters.yml }
- { resource: security.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
# 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 }
# Friends of Symfony configuration
fos_rest:
body_converter:
enabled: true
routing_loader:
default_format: json
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force'
# Sensio Framework Extra Configuration
sensio_framework_extra:
request: { converters: true }
view: { annotations: false }
router: { annotations: false }
and this is my require libraries on composer.json
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "~1.0",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~2.3",
"sensio/framework-extra-bundle": "~3.0",
"sensio/generator-bundle": "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/rest-bundle": "1.1.0",
"jms/serializer-bundle": "dev-master"
},
anybody has any ideas?
thanks

Categories