RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[symfony2]

Martin Hope
vexel
Asked: 2020-12-06 16:07:05 +0000 UTC

将自定义字段添加到管理员(奏鸣曲)列表 Symfony 2.8

  • 1

有一个表,其中显示来自 2 个表的数据,如何向该表添加另一个字段,该字段将根据其他 2 个表或一个表的条件包含信息,例如,我需要添加一个字段Trial,其中是基于字段的信息Price,如果字段Price大于 0,则Trial插入一个值,如果为 0,则插入另一个值。我以这种方式向方法中configureListFields添加了一个字段Trial:->add('trial')它已添加,现在如何用数据填充它,这是整个代码:

namespace AdminBundle\Admin;

use IntlDateFormatter;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;

class PurchaseAdmin extends AbstractAdmin
{
    protected $datagridValues = [
        '_page'       => 1,
        '_sort_order' => 'DESC',
        '_sort_by'    => 'createdAt',
    ];

    protected $accessMapping = [
        'refund' => 'REFUND',
    ];

    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);

        $rootAlias = $query->getRootAliases()[0];
        $query->leftJoin($rootAlias.'.paymentTransaction', 'pt');
        $query->leftJoin($rootAlias.'.language', 'll');
        $query->addSelect('pt, ll');


        return $query;
    }

    public function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
        $collection->remove('delete');
        $collection->remove('create');
        $collection->remove('edit');
        $collection->add('refund', $this->getRouterIdParameter().'/refund');
        $collection->add('studentInfo', $this->getRouterIdParameter().'/student-info');
    }

    /**
     * @param DatagridMapper $datagridMapper
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add(
                'teacher', null, [
                             'show_filter' => true
                         ]
            )
            ->add(
                'student', null, [
                             'show_filter' => true
                         ]
            )
            ->add(
                'language', null, [
                              'show_filter' => true
                          ]
            )
            ->add('lessonLength')
            ->add('lessons')
            ->add(
                'status', null, [], 'choice', [
                            'choices' => [
                                0 => 'created',
                                1 => 'paid',
                                2 => 'used',
                                3 => 'refunded',
                            ],
                        ]
            );
    }

    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier(
                'id',
                null,
                [
                    'route' => [
                        'name' => 'show'
                    ]
                ]
            )
            ->add('statusText')
            ->add('teacher')
            ->add('student')
            ->add(
                'studentInfo',
                'string',
                [
                    'template' => ':CRUD:list__action_student_info.html.twig'
                ]
            )
            ->add('language')
            ->add('lessonLength')
            ->add('lessons')
            ->add('lessonsLeft')
            ->add('price')
            ->add('trial')
            ->add('teacher.currency', null, ['label' => 'Currency'])
            ->add('paymentTransaction')
            ->add(
                'createdAt', 'datetime', [
                               'dateType' => IntlDateFormatter::SHORT,
                               'timeType' => IntlDateFormatter::SHORT,
                           ]
            )
            ->add(
                'updatedAt', 'datetime', [
                               'dateType' => IntlDateFormatter::SHORT,
                               'timeType' => IntlDateFormatter::SHORT,
                           ]
            )
            ->add(
                '_action',
                null,
                [
                    'actions' => [
                        'show'   => [],
                        'refund' => [
                            'template' => ':CRUD:list__action_refund.html.twig'
                        ],
                    ]
                ]
            );
    }

    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('id')
            ->add('price')
            ->add('lessonLength')
            ->add('lessons')
            ->add('lessonsLeft')
            ->add('commission')
            ->add('status')
            ->add('createdAt')
            ->add('updatedAt');
    }

    /**
     * @param ShowMapper $showMapper
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('teacher')
            ->add('student')
            ->add('language')
            ->add('price')
            ->add('teacher.currency', null, ['label' => 'Currency'])
            ->add('lessonLength')
            ->add('lessons')
            ->add('lessonsLeft')
            ->add('commission')
            ->add('statusText')
            ->add('paymentTransaction')
            ->add('createdAt')
            ->add('updatedAt');
    }

    public function getTemplate($name)
    {
        if ($name === 'refund') {
            return ':Purchase:base_refund.html.twig';
        }

        return parent::getTemplate($name);
    }

}
symfony2
  • 1 个回答
  • 10 Views
Martin Hope
Алексей
Asked: 2020-02-04 18:04:35 +0000 UTC

使用密码加密加载夹具时出错

  • 1

Symfony 版本 - 2.8

使用密码加密加载用户实体的夹具时,会发生错误:

[Symfony\Component\Debug\Exception\ContextErrorException]
可捕获的致命错误:传递给 LoadUsersRolesData::__construct() 的参数 1 必须是 Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface 的
实例,没有给出,在 /home 中调用/user/yandex/shop/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php 在第 358 行并定义

我根据文档做所有事情:https ://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html

编码:

<?php

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use App\ShopBundle\Entity\Role;
use App\ShopBundle\Entity\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class LoadUsersRolesData extends AbstractFixture implements OrderedFixtureInterface
{
    private $encoder;

    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    public function load(ObjectManager $manager)
    {
        $role = new Role();
        $role->setName('ROLE_ADMIN');
        $role->setDescription('Учётная запись администратора');

        $manager->persist($role);

        $role2 = new Role();
        $role2->setName('ROLE_BUYER');
        $role2->setDescription('Учётная запись покупателя');

        $manager->persist($role2);

        //Добавление пользователя админа
        $user = new User();
        $user->setUserName('admin');
        $user->setEmail('admin@shop.my');

        $password = $this->encoder->encodePassword($user, 'admin');
        $user->setPassword($password);

        $user->getUserRoles()->add($role);

        $manager->persist($user);

        //Добавление пользователя покупателя
        $user2 = new User();
        $user2->setUserName('user');
        $user2->setEmail('user@shop.my');

        //Шифрование пароля
        $password = $this->encoder->encodePassword($user2, 'test');

        $user2->setPassword($password);
        $user2->getUserRoles()->add($role2);

        $manager->persist($user2);

        $manager->flush();
    }

    public function getOrder()
    {
        return 1;
    }
}

文档还说可以通过 $this->container 属性加密密码,但我想知道为什么它不能这样工作。Symfony 版本可能不合适。而不是这个应该使用另一个类..?

symfony2
  • 1 个回答
  • 10 Views
Martin Hope
vellmur
Asked: 2020-09-15 21:14:47 +0000 UTC

集合的 Symfony 表单事件监听器

  • 2

第三天我一直在受苦,我不知道如何将表单事件侦听器应用于集合。数据会根据用户输入动态变化。该表单包含一个包含用户填写字段的数据的集合。为了动态地改变数据,我使用了事件POST_SET,PRE_SUBMIT同时它POST_SET使用了一个 bang,但是在提交表单之后使用事件,这是一个问题。

在事件中PRE_SUBMIT,我遍历表单的所有集合并获取我可以更改的集合字段。只是这里的问题是,在使用集合时,它们的初始化发生在事件期间SUBMIT,也就是说,PRE_SUBMIT它们根本还没有在表单中!SUBMIT如果你使用or来改变 event POST_SUBMIT,那么集合已经在表单中了,但是它不能再被改变,因为在这个阶段它已经被初始化了。

因此,结果变成了一个恶性循环,PRE_SUBMIT您可以更改这些集合,但它们还不存在,但SUBMIT它们存在,但您无法更改它们。为清晰起见的示例代码:

public function preSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    // We don`t need to change locations in add event only for update existed
    $client = $form->getConfig()->getOptions()['client'];

    $shareForms = $form->get('shares');

    foreach ($data['shares'] as $key => $share) {
        if ($share['pickUpDay'] !== null && $share['pickUpDay'] !== '') {
            $locations = $this->em->getRepository('AppBundle:Member\Location')->getLocationsByDay($client, $share['pickUpDay']);
            $this->addLocationField($shareForms->get($key), $locations);
        }
    }
}  

我无法在preSubmit方法中获取集合的实例,但我可以,但无法再更改那里的字段。也许有人遇到过?$shareForms->get($key)postSubmit

symfony2
  • 2 个回答
  • 10 Views
Martin Hope
Alexandr Konradi
Asked: 2020-06-21 21:36:21 +0000 UTC

在 symfony2 中更新路由

  • 0

我需要在项目中添加一个新的路由,为此我在 routing.yml 文件中添加了一个新规则。(复制并略微更改了当前规则)。

但是当我点击链接时,出现 404 错误。

我在 app/cache/prod 文件夹中找到了 appProdUrlMatcher.php 文件,其中所有路由都是重复的,但形式略有不同。如果您在此文件中编写路由,则一切正常。但据我所知,这个文件是自动生成的。如果删除此文件或整个缓存文件夹,则整个站点将停止工作。

告诉我如何更新这些路线或如何正确描述它们?

symfony2
  • 1 个回答
  • 10 Views
Martin Hope
Вадим Бондаренко
Asked: 2020-04-18 20:25:30 +0000 UTC

symfony 处理文件

  • 0

文件处理是如何在 siphon 中实现的?例如,我有一个静态文件,我想让它可供下载。我需要如何在 Twig 模板引擎中为其生成路径。我知道有一个资产,但据我了解,它定位于 CSS、JS 和图像文件。

symfony2
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5