I stuck with the $defaultLocale of the TranslatableListener.
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md#default-locale
I found only setup possibilities for Symphony, but not for Zend Framework 2.
There is an extension bundle for doctrine for easy setup of DoctrineExtensions named "StofDoctrineExtensionsBundle", but I didn't found something like that for ZF2.
The following link shows a best practices for setting up translatable and other DoctrineExtensions, but where should I put it and isn't there an easier approach?
https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/annotations.md#best-practices-for-setting-up-with-annotations
I only want to know how I can configure the $defaultLocale of the TranslatableListener in a ZF2 environment.
UPDATE:
I tried in my bootstrap the following:
$translatableListener = new TranslatableListener();
$translatableListener->setDefaultLocale('de-DE');
$doctrineEventManager->addEventSubscriber($translatableListener);
But still getting:
.../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/TranslatableListener.php:464 Gedmo\Translatable\Mapping\Event\Adapter\ORM->loadTranslations
$object Rental\Entity\Rental
$translationClass "Rental\Entity\RentalTranslation"
$locale "en_US"
$objectClass "Rental\Entity\Rental"
So my mistake was, that I configured the TranslatableListener twice.
In my doctrine configuration (only for explanation there is a comment before, delete the whole line):
'doctrine' => [
'eventmanager' => [
'orm_default' => [
'subscribers' => [
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
// 'Gedmo\Translatable\TranslatableListener',
],
],
],
and in bootstrap:
// sets the default locale and the actual locale
$translatableListener = new TranslatableListener();
$translatableListener->setDefaultLocale('de-DE');
$translatableListener->setTranslatableLocale(\Locale::getDefault());
$doctrineEventManager->addEventSubscriber($translatableListener);
If you want to set the DefaultLocale and the TranslatableLocale in Zend Framework 2 for the Translatable Doctrine Extension, than do it in bootstrap and don't add it a second time in the doctrine configuration.
If you want to keep:
'doctrine' => [
'eventmanager' => [
'orm_default' => [
'subscribers' => [
'Gedmo\Timestampable\TimestampableListener',
'Gedmo\Sluggable\SluggableListener',
// the line below because in future you might need it
'Gedmo\Translatable\TranslatableListener',
],
],
],
Try this:
https://stackoverflow.com/a/42859119/3661592
Related
I'm writing a couple of laravel packages and I'm wondering if it is possible to have the package write to a specific log file but only for messages related to the package?
I tried making a logging.php file in the packages/myorg/mypackage/config (below) but it doesn't seem to do anything.
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
]
];
I am using "jeroen-g/laravel-packager" to set up the packages. It appears to manually load the mypackage.config in the ServiceProvider bootForConsole
protected function bootForConsole(): void
{
// Publishing the configuration file.
$this->publishes([
mypackage.'/../config/mypackage.php' => config_path('mypackage.php'),
], 'mypackage.config');
}
I'm not sure how to add custom logging to that though. I'm still learning Laravel and I'm not quite sure what or how the main applications config/logging.php is read so I'm not quite sure how to inject a custom version for an add-on package.
EDIT:
I found a post that suggested using the following in the ServiceManager boot() method:
$this->app->make('config')->set('logging.channels.mychannel', [
/* settings */
]);
I used the package config to set a 'logging' => [ 'channels' => [ 'mychannel' => [ /* settings */ ] ] ] and could then do the same thing as above with:
$this->app->make('config')->set('logging.channels.mychannel', config('mypackage.logging.channels.mychannel');
But that still required something in the code. The next best thing I have found thus far is to change my config/logging.php to config/logging.channels.php and include something like:
return [
'mychannel' => [
'driver' => 'single',
'path' => storage_path('logs/mypackage.log'),
'level' => env('LOG_LEVEL', 'debug'),
]
];
Then in the service provider register() method add:
$this->mergeConfigFrom(__DIR__ . '/../config/logging.channels.php', 'logging.channels');
I tried doing it from the original 'logging.php' with channels array nested in a 'logging' key, but array_merge doesn't appear to merge the nested elements so my channel never showed up in logging.channels.
I'm not sure if this is ideal, however. I'd still like to know if there is a 'better' or best practices way of adding custom package logging parameters and whether there is a need to publish it in any way (and how).
I am trying to see the traceLine on Yii2 debug bar like explains in (https://github.com/yiisoft/yii2-debug#open-files-in-ide), but I can't see it.
I have Yii2 2.0.28 and debug-bar 2.1.9 with php 7.2.19
For example: is there any way, inspecting any debug bar’s panel, to know which line of my code thrown a trace/profile action in the debug bar?
Or how can I see where is located any query I am seeing in the database panel?
My config:
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'traceLine' => '{file}:{line}',
'allowedIPs' => ['*'],
'panels' => [
'db' => [
'class' => 'yii\debug\panels\DbPanel',
'defaultOrder' => [
'seq' => SORT_ASC
],
'defaultFilter' => [
'type' => 'SELECT'
]
],
],
];
There are two properties in configuration that affect how the files are displayed in logs in debug bar.
1) traceLine property of debug module. This property contains a template for displaying single line of trace.
In configuration it may look like this
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'traceLine' => '{file}:{line}',
// ... other debug module configurations
]
2) traceLevel property of log component. This affect how many calls will be displayed in trace. The calls of framework's classes are not displayed in debug toolbar, only your files are displayed.
The configuration might look like this
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
// ... other log component configurations
],
// ... other components
],
In the example the traceLevel depends on YII_DEBUG constant. This is used to avoid performance issues in production environments. This is also how traceLevel is set in default yii2 application templates.
The YII_DEBUG constant is usually set in index.php file like this
defined('YII_DEBUG') or define('YII_DEBUG', true);
Summary: Trying to add export in the for csv & pdf download.
Followed documantation to install. Grid view is otherwise working.
Also added as module in config/web.php's $config array -
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
//'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '#kvgrid/messages',
//'forceTranslation' => false
]
]
],
N.B: I am using basic template and new in yii2. I have tried other fixes like composer update etc as suggested in various posts but really stuck with the problem.
the thing causing problem is - Yii::t('kvgrid', 'Reset Grid')
Can somene give me a direction here. I guess it is very simple issue :(
You was got above error because of you have not configure i18n component in web.php file.
You need to configure i18n component like as,
'i18n' => [
'translations' => [
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
],
]
],
Or uncomment the gridview modules's i18n configuration
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#kvgrid/messages',
'forceTranslation' => true
]
]
You have to replace
Yii::t('kvgrid', 'Reset Grid')
to
Yii::t('app', 'Reset Grid')
in your view file
I suggest better to generate CRUD using the Ajax Crud Generator, it will do all the required task for CRUD and export as well...try this
I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.
I have a big application using ZF2 and Doctrine2 and using the DoctrineORMModule. In the configuration I´ve defined a custom SQL filter.
'doctrine' => [
'configuration' => [
'orm_default' => [
'filters' => [
'myFilter' => 'MyNamespace\MyFilter'
]
]
]
]
The filter is activated in the onBootstrap method.
public function onBootstrap(EventInterface $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$entityManager = $serviceManager->get('Doctrine\ORM\EntityManager');
$entityManager->getFilters()->enable('myFilter');
}
Everything is working as expected, which is nice.
However this code is executed on ALL request, even if I don't need the entity manager at all. This really affects my application performance, because all the heavy doctrine factories will be called (which adds like 100ms on every request).
It would be nice if enabled filters are added only when the EntityManager factory is called.
I'd really like to do someting like this, but this doesn't seem like an implemented feature yet.
'doctrine' => [
'configuration' => [
'orm_default' => [
'active_filters' => [
'myFilter'
]
]
]
]
As a last resort I could override the EntityManagerFactory, but I hope there is a cleaner way.
Any suggestions to make this somehow lazy are welcome.