1

我正在尝试覆盖 Symfony-Orocommerce 中的表单/类型,起初,我收到了这个错误:

无法从“Oro\CustomerBundle\Entity\CustomerUser”类型的对象中读取索引“namePrefix”,因为它没有实现 \ArrayAccess。

在关注这篇文章之后,我遇到了另一个我无法解决的错误:(

表单的视图数据应该是 MyCode\Bundle\CustomerBundle\Entity\CustomerUser 类的一个实例,但它是 Oro\Bundle\CustomerBundle\Entity\CustomerUser 类的一个实例。您可以通过将“data_class”选项设置为 null 或...来避免此错误

这是我的代码:

MyCode\Bundle\CustomerBundle\Controller\CustomerUserController.php

<?php

namespace MyCode\Bundle\CustomerBundle\Controller;

use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use MyCode\Bundle\CustomerBundle\Entity\CustomerUser as MyCodeCustomerUser;
use Oro\Bundle\CustomerBundle\Form\Handler\CustomerUserHandler;
use Oro\Bundle\CustomerBundle\Form\Type\CustomerUserType;
use MyCode\Bundle\CustomerBundle\Form\Type\CustomerUserType as MyCodeCustomerUserType;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Oro\Bundle\SecurityBundle\Annotation\Acl;
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Oro\Bundle\CustomerBundle\Controller\CustomerUserController as OroCustomerUserController;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;

class CustomerUserController extends Controller
{
   //some code
   /**
     * Edit customer user form
     *
     * @Route("/update/{id}", name="oro_customer_customer_user_update", requirements={"id"="\d+"})
     * @Template
     * @Acl(
     *      id="oro_customer_customer_user_update",
     *      type="entity",
     *      class="OroCustomerBundle:CustomerUser",
     *      permission="EDIT"
     * )
     * @param CustomerUser $customerUser
     * @param Request     $request
     * @return array|RedirectResponse
     */
    public function updateAction(CustomerUser $customerUser, Request $request)
    {
        return $this->update($customerUser, $request);
    }

    /**
     * @param CustomerUser $customerUser
     * @param Request     $request
     * @return array|RedirectResponse
     */
    protected function update(CustomerUser $customerUser, Request $request)
    {
        $form = $this->createForm(MyCodeCustomerUserType::class, $customerUser);
        $handler = new CustomerUserHandler(
            $form,
            $request,
            $this->get('oro_customer_user.manager'),
            $this->get('oro_security.token_accessor'),
            $this->get('translator'),
            $this->get('logger')
        );

        $result = $this->get('oro_form.model.update_handler')->handleUpdate(
            $customerUser,
            $form,
            function (CustomerUser $customerUser) {
                return [
                    'route'      => 'oro_customer_customer_user_update',
                    'parameters' => ['id' => $customerUser->getId()]
                ];
            },
            function (CustomerUser $customerUser) {
                return [
                    'route'      => 'oro_customer_customer_user_view',
                    'parameters' => ['id' => $customerUser->getId()]
                ];
            },
            $this->get('translator')->trans('oro.customer.controller.customeruser.saved.message'),
            $handler
        );

        return $result;
    }
}

我在这里所做的就是将CustomerUserType更改为MyCodeCustomerUserType,仅此而已。

CustomerBundle\Resources\config\services.yml

parameters:
  mycode_customer.entity.customer_user.class: MyCode\Bundle\CustomerBundle\Entity\CustomerUser

services:
  mycode.form.type.customer_user:
      class: 'MyCode\Bundle\CustomerBundle\Form\Type\CustomerUserType'
      arguments:
            - '@security.authorization_checker'
            - '@oro_security.token_accessor'
      calls:
            - [setDataClass, ['%mycode_customer.entity.customer_user.class%']]
            - [setAddressClass, ['%oro_customer.entity.customer_user_address.class%']]
      tags:
            - { name: form.type, alias: mycode_customer_customer_user }

MyCode\Bundle\CustomerBundle\Form\Type\CustomerUserType.php

<?php

namespace MyCode\Bundle\CustomerBundle\Form\Type;

use Oro\Bundle\AddressBundle\Form\Type\AddressCollectionType;
use MyCode\Bundle\CustomerBundle\Entity\CustomerUser as MyCodeCustomerUser;
use Oro\Bundle\CustomerBundle\Entity\Repository\CustomerUserRoleRepository;
use Oro\Bundle\FormBundle\Form\Type\OroDateType;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessorInterface;
use Oro\Bundle\CustomerBundle\Form\Type\CustomerSelectType;
use Oro\Bundle\UserBundle\Form\Type\UserMultiSelectType;
use Oro\Bundle\CustomerBundle\Form\Type\CustomerUserTypedAddressType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Oro\Bundle\CustomerBundle\Form\Type\CustomerUserType as OroCustomerUserType;

class CustomerUserType extends OroCustomerUserType
{
    /**
     * @param FormBuilderInterface $builder
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function addEntityFields(FormBuilderInterface $builder)
    {
        $builder
            ->add(
                'namePrefix',
                TextType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.name_prefix.label'
                ]
            )
            ->add(
                'firstName',
                TextType::class,
                [
                    'required' => true,
                    'label' => 'oro.customer.customeruser.first_name.label'
                ]
            )
            ->add(
                'middleName',
                TextType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.middle_name.label'
                ]
            )
            ->add(
                'lastName',
                TextType::class,
                [
                    'required' => true,
                    'label' => 'oro.customer.customeruser.last_name.label'
                ]
            )
            ->add(
                'nameSuffix',
                TextType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.name_suffix.label'
                ]
            )
            ->add(
                'email',
                EmailType::class,
                [
                    'required' => true,
                    'label' => 'oro.customer.customeruser.email.label'
                ]
            )
            ->add(
                'customer',
                CustomerSelectType::class,
                [
                    'required' => true,
                    'label' => 'oro.customer.customeruser.customer.label'
                ]
            )
            ->add(
                'enabled',
                CheckboxType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.enabled.label',
                ]
            )
            ->add(
                'birthday',
                OroDateType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.birthday.label',
                ]
            )
            ->add(
                'salesRepresentatives',
                UserMultiSelectType::class,
                [
                    'label' => 'oro.customer.customer.sales_representatives.label',
                ]
            )
            ->add(
                'isGuest',
                CheckboxType::class,
                [
                    'required' => false,
                    'label' => 'oro.customer.customeruser.is_guest.label',
                ]
            );


        if ($this->authorizationChecker->isGranted('oro_customer_customer_user_role_view')) {
            $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'preSetData']);
            $builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'preSubmit']);
        }

        if ($this->authorizationChecker->isGranted('oro_customer_customer_user_address_update')) {
            $options = [
                'label' => 'oro.customer.customeruser.addresses.label',
                'entry_type' => CustomerUserTypedAddressType::class,
                'required' => false,
                'entry_options' => [
                    'data_class' => $this->addressClass,
                    'single_form' => false,
                ],
            ];

            if (!$this->authorizationChecker->isGranted('oro_customer_customer_user_address_create')) {
                $options['allow_add'] = false;
            }

            if (!$this->authorizationChecker->isGranted('oro_customer_customer_user_address_remove')) {
                $options['allow_delete'] = false;
            }

            $builder
                ->add(
                    'addresses',
                    AddressCollectionType::class,
                    $options
                );
        }

        $builder->add(
                    'test',
                    TextType::class, 
                    [
                        'mapped' => false,
                        'required'  => false,
                        'label'     => 'Test',
                        'attr' => ['placeholder' => 'Test']
                    ]
                );

感谢阅读,祝你有美好的一天:)

4

1 回答 1

0

我想我找到了解决方案,我没有注意到控制器中的updateAction () 使用(CustomerUser $customerUser)而不是(MyCodeCustomerUser $customerUser),这就是它导致问题的原因,在修复之后,它会导致另一个问题。

请检查此链接以获取更多信息。

于 2018-11-19T10:48:40.007 回答