Empty json_array attribute when persisting in BD

Asked

Viewed 35 times

0

Boas Galeri.

I have a curious problem using Doctrinefixturesbundle. I have a class Application of the kind Entity Class that maps to the table system. In this class there is an attribute called options who’s kind json_array mapped to the column of sistema opções and I initialize it as a ArrayCollection. Options is logically coded so that each element of this collection is an Object (Optionattribute Entity). I did this because I needed to have a Form that had the ability to add/remove/edit each element of this collection, and then I created the Optionattribute Entity that is not mapped in the Database. I created the Formtype for both Application and Optionattribute and everything worked out nicely.

Now I went to create Fixtures for the Application Entity and the options system column is only '{}', that is, empty. No execution error, just not persisting the collection of Optionattribute entities.

Follows the classes

The fixtures class for Application

class ApplicationFixtures extends Fixture
{
    public function load(ObjectManager $oManager)
    {
        $types = ['TextType', 'TextareaType', 'EmailType', 'IntegerType', 'MoneyType',
            'NumberType', 'UrlType', 'TelType', 'DateType', 'DateTimeType', 'TimeType', 'PercentType',
            'SearchType', 'BirthdayType', 'CheckboxType', 'FileType'];
        $appsAlias = [1 => 'Facebook', 'Twitter', 'Instagram', 'Tumbler', 'Tinder'];
        foreach ($appsAlias as $k => $app) {
            /* @var $application Application */
            $application = new Application();
            $application->setName('Application Number ' . $k)
                ->setIsActive(true)
                ->setAlias($app);

            $rand_keys = array_rand($types, 5);

            foreach ($rand_keys as $typesKey) {
                $optionAttribute = new OptionAttribute();
                $optionAttribute->setName('Option ' . str_replace('Type', '', $types[$typesKey]))
                    ->setDefaultValue('Default Value for Option ' . $typesKey)
                    ->setRequired(($typesKey % 2 == 1))
                    ->setType($types[$typesKey]);

                $application->addOption($optionAttribute);
            }

            $oManager->persist($application);
        }
        $oManager->flush();
    }

}

Application Entity

/**
 * @ORM\Table(name="sistema")
 * @ORM\Entity
 */
class Application
{

    // Several attributes...

    /**
     * @var ArrayCollection|OptionAttribute[]
     * @ORM\Column(name="sistema_opcoes", type="json_array", length=255, nullable=true)
     */
    private $options;

    public function __construct() {
        // ...
        $this->options = new ArrayCollection();
    }

    // Several getters and setters for ordinary fields
    /**
     * @return ArrayCollection
     */
    public function getOptions()
    {
        $options = new ArrayCollection();
        foreach($this->options as $k => $value) {
            /* @var $option OptionAttribute */
            $option = new OptionAttribute();
            $option->unserialize($value);
            $options->add($option);
        }
        return $options;
    }

    /**
     * @param ArrayCollection $options
     * @return Application
     */
    public function setOptions(ArrayCollection $options)
    {
        foreach ($options as $option) {
            $this->addOption($option);
        }
        return $this;
    }

    /**
     * @param OptionAttribute $option
     * @return $this
     */
    public function addOption(OptionAttribute $option)
    {
        $this->options->add($option->serialize());
        return $this;
    }

}

And the Optionattribute Entity

class OptionAttribute implements \Serializable
{
    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $defaultvalue;

    /**
     * @var string
     */
    private $type;

    /**
     * @var boolean
     */
    private $required = false;

    // Getters and Setters

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->name,
            $this->defaultvalue,
            $this->type,
            $this->required,
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
            $this->name,
            $this->defaultvalue,
            $this->type,
            $this->required,
            ) = unserialize($serialized);
    }
}

I wonder where I’m going wrong???

1 answer

0

For those who have the same problem.

I realized that the conversion of Arraycollection for json_array did not work as I expected and then I changed the code so that the attribute was filled only with the type array in the attribute in question and everything worked as expected.

I didn’t have to change the method getOptions.

Follows the amendments

/**
 * @ORM\Table(name="sistema")
 * @ORM\Entity
 */
class Application
{

    // Several attributes...

    /**
     * @var array
     * @ORM\Column(name="sistema_opcoes", type="json_array", length=255, nullable=true)
     */
    private $options;

    public function __construct() {
        // ...
        $this->options = [];
    }

    // Several getters and setters for ordinary fields

    /**
     * @param ArrayCollection $options
     * @return Application
     */
    public function setOptions(ArrayCollection $options)
    {
        $this->options = [];
        foreach ($options as $option) {
            $this->addOption($option);
        }
        return $this;
    }

    /**
     * @param OptionAttribute $option
     * @return $this
     */
    public function addOption(OptionAttribute $option)
    {
        $options = $this->getOptions();
        if ($options->contains($option)) {
            return;
        }
        $this->options[]= ($option->serialize());
        return $this;
    }

}

From that point I was able to use the Fixture class normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.