0

I've got this strange problem, here is example usage of my custom ThingType class.

->add('photos', 'namespace\Form\Type\ThingType', [
    'required' => false,
])

if the field name is photos everything works as expected, but if I change my entity field to let's say photosi, run generate entities, and change the form field name, this error is thrown:

Neither the property "photosi" nor one of the methods "addPhotosus()"/"removePhotosus()", "setPhotosi()", "photosi()", "__set()" or "__call()" exist and have public access in class "AppBundle\Entity\Product".

I guess the problem comes from Symfony trying to generate getter method name for my entity. Why is this addPhotosus method name generated? How can I solve this?

EDIT:

I'm using model transformer when showing the data to the user.

    $builder->addModelTransformer(new CallbackTransformer(
        function ($imagesAsText) {
            if (!$imagesAsText) {
                return null;
            }

            $newImages = [];
            foreach($imagesAsText as $img) {
                $newImages[] = $img->getID();
            }

            return implode(',', $newImages);
        },
        function ($textAsImages) use ($repo) {
            $images = [];
            foreach(explode(',', $textAsImages) as $imgID) {
                $img = $repo->findOneById($imgID);

                if ($img) {
                    $images[] = $img;
                }
            }

            return $images;
        }
    ));

The actual field is TextType::class with entity ids in it for example 1,10,32,51. The model transformer transforms this data to entities. Setting 'data_class' to my form type seems irrelevant, because the actual form type is a part of entity. I mean I have Product entity and Photo entity, photos is array of photo entity. So in my ThingType, what data_class should I use, photo or product?

Thanks

2 Answers 2

1

The fist parameter of the add method for a form, should be one of the mapped attributes of the data_class of the form, usually selected inside the form as

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Product'
        ));
}

That isn't related to the form name. So , you are trying to access to a "photosi" attribute inside your Product class.

Hope this help you.

Sign up to request clarification or add additional context in comments.

3 Comments

And you can add a 'mapped'=>false if you want this child to not be mappe.
I'm using TextType::class to keep array of entities, transformed by modelTransformer and this error shows when I try to set 'data_class' The form's view data is expected to be an instance of class AppBundle\Entity\Product, but is a(n) string
@nacholibre Maybe you should update your question with some code, or make a new one , since it had nothing to do with this thread question.
0

Ok so for the first point you need to remember that Symfony is looking for setXX() and getXX()method in your entity for each entry of your form. If you change your variable name you need to update the form :

->add('newName', XXType::class, [
    'required' => false,
])

and you're entity by changing the variable

class Entity 
{

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $newName;

    public function getOldName(){
        return $this->$oldName;
    }

   public function setOldName(oldName){
       $this->oldName = $oldName;
       return $this
   }

}

then run the command

php bin/console make:entity --regenerate

and symfony will upload your entity by itself

class Entity 
{

    /**
     * @ORM\Column(type="string", length=255)
     * @SerializedName("title")
     * @Groups({"calendar"})
     */
    private $newName;

    public function getOldName(){
        return $this->$oldName;
    }

   public function setOldName($oldName){
       $this->oldName = $oldName;
       return $this
   }

   public function getNewName(){
       return $this->newName;
   }

   public function setNewName($newName){
       $this->newName = $newName;
       return $this
   }

note that the old get and set method are not deleted by the script

note as well that in your specific case of photosi, symfonyguess that the "i" is a plural mark and look for addPhotosus() methods

For the edit it looks very unclear and has nothing to do with the first question. Consider reading : doc on collectionType

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.