I struggle to understand how i'm supposed to do a custom filter in easyadmin 3.3 . The documentation isnt helping. I alway got an error
Error: Class App\Entity\entity1 has no field or association named "field1"
So i tried to put the mapping as false to prevent this, following the doc and i'm getting this
Attempted to call an undefined method named "mapped" of class "App\Controller\Admin\Filter\customfilter".
here my code :
Crud controller :
public function configureFilters(Filters $filters): Filters
{
return $filters
->add(getAutoclaveFilter::new('fiedWithNoAssociation')->mapped(false))
;
}
custom filter :
class GetAutoclaveFilter implements FilterInterface
{
use FilterTrait;
public static function new(string $propertyName, $label = null): self
{
return (new self())
->setFilterFqcn(__CLASS__)
->setProperty($propertyName)
->setLabel($label);
}
public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
{
$queryBuilder->andWhere(sprintf('%s.%s = :value', $filterDataDto->getEntityAlias(), $filterDataDto->getProperty()))
->setParameter('value',$filterDataDto );
}
And the entity :
public function fiedWithNoAssociation()
{
return $this->getEntityAssociated()->getEntity2();
}
What i'm doing wrong ? Is the mapped function not implemented yet ?
Use instead of mapped:
->setFormTypeOption('mapped', false)
Related
I am currently working with a controller, OnboardingController.php. This controller calls another method in a different class, let's call it OnboardingService.php, so for example:
OnboardingController
public function doThing()
{
return $this->doAnotherThing();
}
OnboardingService
public function doAnotherThing()
{
return redirect('/')->with(['propertyA' => 'valueA']);
}
Would I be able to access propertyA in OnboardingController before returning the redirect? And if so, how would I access that property?
e.g.
OnboardingController
public function doThing()
{
$doAnotherThing = $this->doAnotherThing();
Log::info($doAnotherThing->propertyA);
return $doAnotherThing;
}
I am currently using Laravel 6.
You're using redirect(...)->with(...) to flash propertyA, so propertyA should be accessible from the session
public function doThing()
{
$doAnotherThing = $this->doAnotherThing();
Log::info(session()->get('propertyA'));
return $doAnotherThing;
}
I'm not understanding how to use the initialization function in the code below. Can you explain it to me?
Class A
{
protected $classB;
public function __construct()
{
$this->classB = new ClassB();
}
}
Class A
{
protected $classB;
public function __construct(ClassB $classB)
{
$this->classB = $classB;
}
}
in the second way you always have to make your object like this.
$clasa = new ClassA($classb);
in the first way you are creating the object inside of the constructor.
the second way is an injection. and could be usefull when for example you need some attributes already seted in the object.
I develop lumen package and I don't know test this.
In my package, I use global method config() and abort() but this methods exist with bootstrap/app.php and I have'nt this file in my package.
I'm thinking redefine this methods with dummies class but I have to write only one test method in test class when I test a method with a changement in the config to can re-call an antoher config dummy class .
It's not practical and I guess there's better.
I can share code if you want.
--- Edit
This is example :
Class CheckAuthorizationTest
public function testCanSeeOtherUserRoles()
{
$this->assertTrue(CheckAuthorization::canSeeOtherUserRoles($user, $user));
}
Class CheckAuthorization
static public function canSeeOtherUserRoles(Model $user_parent, Model $user_child)
{
return self::roleIsParentOfDirectChild($user_parent, $user_child);
}
static public function canShowGroup(array $parent_group, string $child_group)
{
$groupsHelper = new GroupsHelper();
foreach ($parent_group as $group) {
if (in_array($child_group, config('roles.roles'))) {
return true;
}
}
abort(403);
}
Result :
There was 1 error:
1) ::testCanSeeOtherUserRoles
ReflectionException: Class config does not exist
I want to set the session after user login in opencart-3.0.2.0
I am new to opencart, I have just created this two files only in the corresponding folder.anything else I need to be done to trigger the event.
I am referring this link to trigger the event in opencart:https://isenselabs.com/posts/opencart2-event-system-tutorial
I have searched a lot on google still no result found.
Code that I am using to trigger event in opencart.
path : admin/controller/module/mymodule.php
Code :
public function install() {
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
$this->model_extension_event->addEvent('mymodule', 'post.customer.login', 'module/mymodule/post_customer_login_customtoken');
$this->model_extension_event->addEvent('mymodule', 'post.customer.logout', 'module/mymodule/post_customer_logout_function');
}
public function uninstall() {
$this->load->model('extension/event');
$this->model_extension_event->deleteEvent('mymodule');
}
public function on_store_delete($store_id) {
$this->load->model('setting/store');
$store_info = $this->model_setting_store->getStore($store_id);
$admin_mail = $this->config->get('config_email');
mail($admin_mail, "A store has been deleted", "The store " . $store_info['url'] . " was deleted.");
}
}
path : catalog/controller/module/mymodule.php
Code :
<?php
class ControllerModuleMyModule extends Controller {
public function post_customer_login_customtoken() {
$str = 'abcdefghigklmnopqrstuvwxyz';
$shuffled = str_shuffle($str);
$this->session->data['custom_token'] = $shuffled;
}
public function post_customer_logout_function(){
$this->log->write("post_customer_logout_function");
unset($this->session->data['custom_token']);
}
}
That tutorial is for OpenCart 2.0 - 2.1, in OpenCart 2.2 and above Event system has been changed.
For OpenCart 3.0.2.0 instead of:
$this->load->model('extension/event');
// and
$this->model_extension_event->addEvent
use:
$this->load->model('setting/event');
// and
$this->model_setting_event->addEvent
Instead of:
'post.customer.login'
use:
'catalog/controller/account/login/after'
Instead of:
deleteEvent
Use:
deleteEventByCode
So it must be:
admin\controller\extension\module\mymodule.php
public function install(){
$this->load->model('setting/event');
$this->model_setting_event->addEvent('mymodule', 'catalog/controller/account/login/after', 'extension/module/mymodule/after_customer_login_customtoken');
}
public function uninstall(){
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('mymodule');
}
catalog\controller\extension\module\mymodule.php
class ControllerExtensionModuleMyModule extends Controller {
public function after_customer_login_customtoken(){
$this->log->write('test');
}
}
Usually it is possible to put any domain object into session ($GLOBALS['TSFE']->fe_user) as they get automatically serialized and deserialized correctly. But in some cases this fails (i suppose caused by circurlar references or the serialized data exceeds the limit of the database blob field).
I found a better approach, converting the domain objects to integer "ids" on serialization and getting the real domain objects back from repository on deserialization:
class PutMeIntoSession implements \Serializable {
protected $project = null;
public function getProject() {
return $this->project;
}
public function setProject(\Vendor\Ext\Domain\Model\Project $project = NULL) {
$this->project = $project;
}
public function serialize() {
$serialized = serialize(array(
'project' => $this->project ? $this->project->getUid() : 0
));
return $serialized;
}
public function unserialize($serialized) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$unserialized = unserialize($serialized);
$this->project = $objectManager->get(ProjectRepository::class)->findByUid($unserialized['project']);
}
}
This seems to work fine. But i don't know if there are smarter ways to achieve this or if there are any possible problems with my approach?