This topic contains 0 replies, has 1 voice, and was last updated by fudu 1 year ago.
-
Topic
-
i’m trying to override Form/Type in Symfony-Orocommerce, and at first, i got this error:
Cannot read index “namePrefix” from object of type
“Oro\CustomerBundle\Entity\CustomerUser” because it doesn’t implement
\ArrayAccess.And after follow this post,i got another error which i can’t solved :(
The form’s view data is expected to be an instance of class
MyCode\Bundle\CustomerBundle\Entity\CustomerUser, but is an instance
of class Oro\Bundle\CustomerBundle\Entity\CustomerUser. You can avoid
this error by setting the “data_class” option to null or …Here is my code:
MyCode\Bundle\CustomerBundle\Controller\CustomerUserController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384<?phpnamespace 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;}}What i’ve done here is i change CustomerUserType to MyCodeCustomerUserType, that’s all.
CustomerBundle\Resources\config\services.yml
1234567891011121314parameters:mycode_customer.entity.customer_user.class: MyCode\Bundle\CustomerBundle\Entity\CustomerUserservices: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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166<?phpnamespace 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']]);Thanks for reading, have a good day :)
Here is my stackoverflow post (if you interesting)
You must be logged in to reply to this topic.