When I want to create a Shipment I get the error:
An exception has been thrown during the rendering of a template ("No locale has been set and current locale is undefined.") in SonataAdminBundle::standard_layout.html.twig at line 148.
I think I need to set the default locale for sylius, but I tried alot of examples, none of them helped..
I currently have this setup:
Config.yml:
sylius_shipping:
driver: doctrine/orm # Configure the Doctrine ORM driver used in documentation.
classes:
shipping_method:
model: Application\Sylius\ShippingBundle\Entity\ShippingMethod
translation:
model: Application\Sylius\ShippingBundle\Entity\ShippingMethodTranslation
shipping_method_rule:
model: Application\Sylius\ShippingBundle\Entity\Rule
shipment:
model: Application\Sylius\ShippingBundle\Entity\Shipment
shipment_item:
model: Application\Sylius\ShippingBundle\Entity\ShipmentItem
shipping_category:
model: Application\Sylius\ShippingBundle\Entity\ShippingCategory
#stof_doctrine_extensions:
# orm:
# default:
# timestampable: true
parameters:
sylius.locale: %locale%
#sylius_locale:
# driver: doctrine/orm
sylius_translation:
default_locale: "%locale%"
#sylius_translation:
# default_locale: %sylius.locale%
Parameters.yml:
sylius.currency_importer.ecb.base_currency: EUR
sylius.currency_importer.open_exchange_rates: EDITME
sylius.locale: en_US
Nothing works..
UPDATE
By changing my config to this:
framework:
#esi: ~
translator: { fallback: %sylius.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: "%sylius.locale%"
parameters:
sylius.locale: 'en'
#sylius_locale:
# driver: doctrine/orm
sylius_translation:
default_locale: %sylius.locale%
I still get the same error. I cleared my cache.
Try %sylius.locale%. It looks like you've just not put in the full name of the parameter in your config file.
If you've updated the value and it's not working, make sure you clear your cache.
$ app/console cache/clear --env=dev
$ app/console cache/clear --env=prod
You will also require something like this:
imports:
- { resource: parameters.yml }
Otherwise the parameters you enter will not be loaded into the configuration file.
Related
New to symfony i installed Sonata composer require sonata-project/admin-bundle and translator service composer require symfony/translation
As desciribed on symfony manual (manual)
I added
# config/packages/framework.yaml
framework:
translator: { fallbacks: ['%locale%'] }
but when I make a php bin/console cache:clear i get the error
*In ParameterBag.php line 100:
You have requested a non-existent parameter "locale".*
Thank you for your help,
to solve add in services.yaml:
parameters:
locale: 'en' (en -> english, it -> italian ecc..)
and in framework.yaml
framework:
default_locale: '%locale%'
translator:
fallbacks:
- '%locale%'
My symfony application (3.4.8) seems to ignore any attempts to prolong the session. What would be the best course of action to troubleshoot this issue? The documentation is very vague.
app/config/security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
providers:
fos_userbundle:
id: fos_user.user_provider.username
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
default_target_path: /
logout: true
anonymous: true
remember_me:
secret: '%secret%'
lifetime: 28000
path: /
access_denied_handler: app.security.access_denied_handler
config.yml
framework:
lock: 'semaphore'
#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: session.handler.native_file
save_path: "/tmp"
cookie_lifetime: 28800
fragments: ~
http_method_override: true
assets: ~
Maybe if you try this in your config:
session:
cookie_lifetime: 18000
gc_maxlifetime: 18000
Hope it helps !
Judging by this:
handler_id: session.handler.native_file
you're using a native session handler. From Symfony documentation:
So-called native handlers, are save handlers which are either compiled into PHP or provided by PHP extensions, such as PHP-Sqlite, PHP-Memcached and so on.
All native save handlers are internal to PHP and as such, have no public facing API. They must be configured by php.ini directives, usually session.save_path and potentially other driver specific directives.
Inspecting the NativeFileSessionHandler I've found no methods relating to session duration. That leads me to the conclusion that you have to set the duration on the PHP level, not on the Symfony level.
So, try setting the session.gc-maxlifetime (in your php.ini or calling ini_set) to 3600.
your setting will log out if you are inactive for more than 30 minutes.
You can add following in your yml file
#app/config/config.yml
session:
cookie_lifetime: 86400
gc_maxlifetime: 1800
gc_probability: 1
gc_divisor: 1
Using Symfony 3.4.2 , I am trying to build a relationship between two entities of two different bundles which uses their own databases located on different servers.
In other words, we have two databases, and then two entities managers :
One existing MariaDB database [person] which store one "person" table, used by many other applications.
One new MariaDB database [app] dedicated for the application.
Note that I can't change anything on that situation (legacy applications must still running). Althought it is planned "in the future" to replace the [person] database by a REST Api.
Following the Symfony doc (https://symfony.com/doc/3.4/doctrine/multiple_entity_managers.html), this gives the following app/config/config.yml file :
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
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
framework:
#esi: ~
#translator: { fallbacks: ['%locale%'] }
secret: '%secret%'
router:
resource: '%kernel.project_dir%/app/config/routing.yml'
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
default_locale: '%locale%'
trusted_hosts: ~
session:
# https://symfony.com/doc/current/reference/configuration/framework.html#handler-id
handler_id: session.handler.native_file
save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
fragments: ~
http_method_override: true
assets: ~
php_errors:
log: true
# Twig Configuration
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
# charset: UTF8
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.project_dir%/var/data/data.sqlite'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
acme_another_db :
driver: pdo_mysql
host: '%acme_another_database_host%'
port: '%acme_another_database_port%'
dbname: '%acme_another_database_name%'
user: '%acme_another_database_user%'
password: '%acme_another_database_password%'
# charset: UTF8
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: '%kernel.project_dir%/var/data/data.sqlite'
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
#path: '%database_path%'
orm:
# Let's disable auto_mapping since we have several entity_managers
# auto_mapping: true
default_entity_manager: default
entity_managers:
default:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: default
mappings:
AppBundle: ~
acme_another_em:
naming_strategy: doctrine.orm.naming_strategy.underscore
connection: acme_another_db
mappings:
AcmeAnotherBundle: ~
auto_generate_proxy_classes: '%kernel.debug%'
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
Since the "person" table is to be used by many other applications, I choosed to create a separate bundle.
In terms of entity, I have then 3 entities :
Acme/AnotherBundle/Entity/Person
AppBundle/Entity/Book
AppBundle/Entity/BookInterest
The first entity was generated without any error.
The AppBundle/Entity/Book and AppBundle/Entity/BookInterest entities were generated via bin\console generate:doctrine:entity without any problem.
Before updating the database schema, I added the relationship in the AppBundle/Entity/BookInterest entity class file (relation to Acme/AnotherBundle/Entity/Person and to AppBundle/Entity/Book), following the Symfony doc (http://symfony.com/doc/3.4/doctrine/associations.html).
/**
* #ORM\ManyToOne(targetEntity="Book", inversedBy="book")
* #ORM\JoinColumn(name="fk_book_id", referencedColumnName="id", nullable=false)
*/
private $book;
/**
* #ORM\ManyToOne(targetEntity="Acme\AnotherBundle\Entity\Person")
* #ORM\JoinColumn(name="fk_person_id", nullable=false)
*/
private $person;
But, when I try to update the schema via bin\console doctrine:schema:update --force --em=default, I have the following error message :
In MappingException.php line 37: The class
'Acme\AnotherBundle\Entity\Person' was not found in the chain
configured namespaces AppBundle\Entity
Adding "use Acme\AnotherBundle\Entity\Person;" in the top of \src\AppBundle\Entity\BookInterest.php and clearing the cache via bin\console cache:clear does not solve the problem.
Did I miss something in the app/config/config.yml configuration file, or in the \src\AppBundle\Entity\BookInterest.php entity class file ?
Doctrine2 unable to handle one relationship between two entity managers ?
I also found a (quite old) post on a similar problem (Using Relationships with Multiple Entity Managers), which indicates that Doctrine2 is unable to do that (due to the use of two entity managers).
If this is still the case, what is the best way to handle that situation ?
Is using one simple integer field without relation in the AppBundle/Entity/BookInterest for storing person_id and create manuals validation checks (then manual queries) in the BookInterestController is a good practice ?
Thanks a lot in advance for any tips (and good practice advices) to solve that case ! :-)
I have my model entities saved in src/AppBundle/Model/Entity.
You can understand, that i have own custom folder for entities (in model folder).
This is my orm settings in config.yml:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false
mappings:
user:
type: annotation
dir: %kernel.root_dir%/../src/AppBundle/Model/Entity
alias: 'Entity'
prefix: AppBundle\Model\Entity
is_bundle: false
I have a problem with these lines in my security.yml:
providers:
our_db_provider:
entity:
class: AppBundle:User
property: username
# if you're using multiple entity managers
# manager_name: customer
There is some error:
Unknown Entity namespace alias 'AppBundle'.
I don't what ref I have to use (AppBundle:User probably not).
Thank you for your answers.
By default, all Symfony bundles will have a nice alias: XxxBundle (aliasing the NamespaceOfXxxBundle\Entity namespace). As you're bundle doesn't follow this convention and stores it in Model\Entity instead, you have 2 options:
Don't use the alias feature and pass the FQCN: AppBundle\Model\Entity\User
Create a new alias, give it a nice name and use it:
doctrine:
orm:
# ...
mappings:
user:
type: annotation
dir: %kernel.root_dir%/../src/AppBundle/Model/Entity
alias: App # <-- the alias name
prefix: AppBundle\Model\Entity
is_bundle: false
App:User
There is something I do not understand with locale management in Symfony2. I want to write month name in French with date Twig method. Is it possible? I can't do it.
It seems that my locale is not taken into account.
Here is my app/config/config.yml file:
framework:
#esi: ~
translator: { fallback: fr }
secret: %secret%
charset: UTF-8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enable_annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: fr
auto_start: true
Session locale seems good:
echo $this->get('session')->getLocale(); // Returns "fr"
die;
Yet, when I am using, in my view, the following:
<td class="month">{{ history.date|date('F Y') }}</td>
It returns me "July 2011" for instance, instead of "Juillet 2011".
What am I misunderstanding? Shouldn't the date filter be localized? If not, how can I do to retrieve correct terms? I used to use I18N to translate all the month names, but I do not think it is the best practice.
date does not return localized string. You have to use strftime for that. Alternatively you can use SonataIntlBundle if you want more control.