misd-service-development / phone-number-bundle

Integrates libphonenumber into your Symfony2-Symfony4 application
459 stars 143 forks source link

Unable to transform value for property path phone #121

Closed kojidev closed 6 years ago

kojidev commented 7 years ago

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:

php bin/console doctrine:fixtures:load purging database loading AppBundle\DataFixtures\ORM\LoadFixtures
[Doctrine\DBAL\Types\ConversionException] Expected \libphonenumber\PhoneNumber, got string

User class

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

@ORM\Entity
class User
{
    /**
     * @ORM\Column(type="phone_number", nullable=true)
     * @AssertPhoneNumber
     * @Serializer\Type("libphonenumber\PhoneNumber")
     *
     * @var string
     */
    private $phone;

    public function setPhone(?string $phone = null)
    {
        $this->phone = $phone;
    }

    public function getPhone(): string
    {
        return $this->phone;
    }
}

config.yml

doctrine:
    dbal:
        ...
        types:
            phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType

fixture file:

AppBundle\Entity\User:
    user_john:
        firstname: John
        lastname: Johnson
        email: some@email.com
        phone: +79990000001

Where should magic of transformation go? Should I create EventListener that will be transform string -> PhoneNumber after fetching data from DB and vice versa?

xabbuh commented 7 years ago

Your phoneNumber property must not be a string, but an instance of libphonenumber\PhoneNumber.

kojidev commented 7 years ago

[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;
    }
ste93cry commented 7 years ago

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...

Rikijs commented 6 years ago

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.

Rikijs commented 6 years ago

Used an answer on StackOverflow from author with the same problem. #103

iBasit commented 6 years ago

I'm getting same error after upgrading to symfony 3.4, it was working fine on v2.8

kojidev commented 6 years ago

@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.