我正在尝试在Symfony-Orocommerce中覆盖Form / Type,首先,我遇到了以下错误:
无法从类型的对象读取索引“ namePrefix” “ Oro \ CustomerBundle \ Entity \ CustomerUser”,因为它没有实现 \ ArrayAccess。
在遵循this post之后,我遇到了另一个我无法解决的错误:(
窗体的视图数据应该是class的一个实例 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']
]
);
感谢阅读,祝您有美好的一天:)
答案 0 :(得分:0)
我认为我已经找到了解决方案,我没有注意到控制器中的 updateAction ()使用( CustomerUser $ customerUser )而不是( MyCodeCustomerUser $ customerUser ),说明为什么会导致此问题,并在解决此问题后又导致另一个问题。
请检查this link以获得更多信息。