data-import icon indicating copy to clipboard operation
data-import copied to clipboard

modified DoctrineWriter's setValue to check for manyToMany add methods

Open intrepion opened this issue 11 years ago • 9 comments

This PR is for allowing DoctrineWriter to handle updating many-to-many and one-to-many associations that use add and remove methods. The unit tests have also been updated.

intrepion avatar Oct 09 '14 05:10 intrepion

Interesting! Could you update DoctrineWriter docs too?

ddeboer avatar Dec 06 '14 13:12 ddeboer

Sure thing!

intrepion avatar Dec 07 '14 07:12 intrepion

Great! If you could do so in this PR, I’ll merge everything in one go.

ddeboer avatar Dec 07 '14 20:12 ddeboer

Really cool :+1:

Baachi avatar Jan 13 '15 15:01 Baachi

@intrepion @ddeboer Any news on this? I kindof need this.

WouterSioen avatar Feb 11 '15 08:02 WouterSioen

I've fixed this by adding a setter on my many to many relation, so no need to hurry anymore ;)

WouterSioen avatar Feb 11 '15 08:02 WouterSioen

I'm interested in this PR. There is just missing documentation for it to be merged ?

ip512 avatar Apr 19 '15 17:04 ip512

Could you please rebase this on master so we can merge this?

ddeboer avatar May 28 '15 19:05 ddeboer

I almost wrote something similar by myself. But now that I've found this I'm wondering when it will be merged?

Update: Tried to copy/paste setValue(...). Doesn't seem to work out of box right now. Also, when defining a $setter variable inside the function, wouldnt it make obsolte to provide such a variable to the function as an argument?

Update2: Just in case you are interested. Here's the code of that function which works for me. Maybe it's usefull for you.

    /**
     * Call a setter of the entity
     *
     * @param object $entity
     * @param mixed  $value
     * @param string $fieldName
     */
    protected function setValue($entity, $value, $fieldName)
    {
        $getter  = 'get' . ucfirst($fieldName);
        $setter  = 'set' . ucfirst($fieldName);
        $adder   = 'add' . ucfirst($fieldName);
        $remover = 'remove' . ucfirst($fieldName);

        if (method_exists($entity, $setter))
        {
            $entity->$setter($value);
        }
        elseif (method_exists($entity, $adder))
        {
            $oldValue = $entity->$getter();
            if ($oldValue)
            {
                foreach ($oldValue as $oldItem)
                {
                    $entity->$remover($oldItem);
                }
            }
            if (is_array($value))
            {
                foreach ($value as $newItem)
                {
                    $entity->$adder($newItem);
                }
            }
            else
                $entity->$adder($value);
         }
    }

Nexotap avatar Oct 02 '15 07:10 Nexotap