Reputation: 71
On Sylius, I want to add 1 table for customer so that for specific customer type (e.g. VIP), extra fields has to be filled in. (One to One relationship)
Entity CustomerExtra
created. Form type CustomerExtraType
created.
Originally, I created the form type extends AbstractType. The customer form shows those extra fields but would not save the record.
Then I've found out Sylius should extends AbstractResourceType instead so I made the changes but error throw:
Cannot autowire service "AppBundle\Form\Type\CustomerExtraType": argument "$dataClass" of method "Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType::__construct()" is type-hinted "string", you should configure its value explicitly.
Here are the code:
service.yml
services:
app.form.type.customerextra:
class: AppBundle\Form\Type\CustomerExtraType
tags:
- { name: form.type}
config.yml
sylius_resource:
resources:
app.customerextra:
driver: doctrine/orm
classes:
model: AppBundle\Entity\CustomerExtra
form: AppBundle\Form\Type\CustomerExtraType
Customer Entity
namespace AppBundle\Entity;
use AppBundle\Entity\CustomerExtra as BaseCustomerExtra;
use Sylius\Component\Core\Model\Customer as BaseCustomer;
class Customer extends BaseCustomer
{
/**
* @var CustomerExtra|null
*/
protected $customerextra;
public function __construct(BaseCustomerExtra $customerextra)
{
$this->createdAt = new \DateTime();
$this->customerextra = $customerextra;
}
/**
* {@inheritdoc}
*/
public function getCustomerExtra(): ?BaseCustomerExtra
{
return $this->customerextra;
}
/**
* {@inheritdoc}
*/
public function setCustomerExtra(BaseCustomerExtra $customerextra): void
{
$this->customerextra = $customerextra;
$this->customerextra->setCustomer($this);
$customerextra->setCustomer($this);
}
CustomerExtra Entity
private $customer;
/**
* {@inheritdoc}
*/
public function getCustomer(): ?Customer
{
return $this->customer;
}
/**
* {@inheritdoc}
*/
public function setCustomer(?Customer $customer): void
{
$this->customer = $customer;
}
CustomerProfileType
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$customer = $event->getData();
$event->getForm()->add('customerextra', CustomerExtraType::class, [
]);
});
CustomerExtraType
namespace AppBundle\Form\Type;
use AppBundle\Entity\CustomerExtra;
use Symfony\Component\Form\FormBuilderInterface;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
class CustomerExtraType extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('customer_extra1', TextType::class, [
'required' => false,
'label' => 'sylius.form.customer.customer_extra1',
])
->add('customer_extra2', NumberType::class, [
'required' => false,
'label' => 'sylius.form.customer.customer_extra2',
])
;
}
public function getBlockPrefix()
{
return 'app_customerextra';
}
public function getName()
{
return 'admin_customerextra';
}
}
Please help.
Upvotes: 1
Views: 1375
Reputation: 20035
I had this issue while migrating from Sylius 1.0-beta to 1.3. I had the same error message about the same parent type:
Cannot autowire service "App\SomeBundle\MyType": argument "$dataClass" of method "Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType::__construct()" is type-hinted "string", you should configure its value explicitly.
What fixed it for me was disabling autowiring by default:
services:
# Default configuration for services in *this* file
_defaults:
# Automatically injects dependencies in your services
autowire: false
Upvotes: 0
Reputation: 953
Try to change the service definition:
services:
app.form.type.customerextra:
class: AppBundle\Form\Type\CustomerExtraType
arguments:
$dataClass: AppBundle\Entity\CustomerExtra
tags:
- { name: form.type}
Update: ok, try instead:
app.form.type.customerextra:
class: AppBundle\Form\Type\CustomerExtraType
arguments:
- AppBundle\Entity\CustomerExtra
- "Default"
tags:
- { name: form.type}
Upvotes: 2