Closed kojidev closed 6 years ago
Your phoneNumber
property must not be a string
, but an instance of libphonenumber\PhoneNumber
.
[Doctrine\Common\Persistence\Mapping\MappingException]
The class 'libphonenumber\PhoneNumber' was not found in the chain configure
d namespaces AppBundle\Entity, Gedmo\Translatable\Entity
...
/**
* @ORM\Column(type="phone_number", nullable=true)
* @AssertPhoneNumber
* @Serializer\Type("libphonenumber\PhoneNumber")
*
* @var PhoneNumber
*/
private $phone;
public function getPhone(): PhoneNumber
{
return $this->phone;
}
public function setPhone(PhoneNumber $phone = null)
{
$this->phone = $phone;
}
Just to clarify, does removing the @Serializer\Type
annotation fix the problem? Because the error seems to point to the part of code that belongs to that line...
Hello! I am hitting the same error, yet i have done everything in the docs. I am using Symfony 3.4 and Flex.
Error
Unable to transform value for property path "phone_number": Expected a \libphonenumber\PhoneNumber.
Relevant part of my entity
<?php
namespace App\Entity;
use libphonenumber\PhoneNumber;
use JMS\Serializer\Annotation\Type;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Entity(repositoryClass="App\Repository\FirmRepository")
* @ORM\Table(name="firm")
*/
class Firm
{
/**
* @ORM\Column(type="phone_number", nullable=true)
*
* @var PhoneNumber
*
* @Assert\NotBlank(groups={"add_firm"})
* @AssertPhoneNumber(groups={"add_firm"}, defaultRegion="GB")
*
* @Type("libphonenumber\PhoneNumber")
*/
protected $phone_number;
}
My form type
<?php
namespace App\Form\Type;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FirmAddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phone_number', PhoneNumberType::class,
array(
'mapped' => true,
'label' => 'Uzņēmuma tālruņa numurs:',
'data' => '123456789',
//'default_region' => 'GB',
//'format' => PhoneNumberFormat::NATIONAL
'default_region' => PhoneNumberUtil::UNKNOWN_REGION,
'format' => PhoneNumberFormat::INTERNATIONAL
)
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
//'data_class' => 'App\Entity\Location',
'validation_groups' => array('add_firm')
));
}
public function getBlockPrefix()
{
return null;
}
}
Doctrine.yaml
doctrine:
dbal:
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
types:
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType`
How can i overcome this error? Thank you for ideas.
Used an answer on StackOverflow
from author with the same problem. #103
I'm getting same error after upgrading to symfony 3.4, it was working fine on v2.8
@xabbuh is right I just mistypehinted setter. But it wouldn't work anyway because nelmio/alice
bundle will inject string, if you want an object you have to create DataProvider. I found using nelmio/alice
inconvenient anyway.
It's basically the same issue that this guy faced, but he doesn't seem to interesting it anymore.
I did everything by readme, but Doctrine still complains about type incompatibility:
User class
config.yml
fixture file:
Where should magic of transformation go? Should I create EventListener that will be transform string -> PhoneNumber after fetching data from DB and vice versa?