Skip to content

modified DoctrineWriter's setValue to check for manyToMany add methods#124

Open
intrepion wants to merge 2 commits into
ddeboer:masterfrom
intrepion:doctrine-writer-many-to-many
Open

modified DoctrineWriter's setValue to check for manyToMany add methods#124
intrepion wants to merge 2 commits into
ddeboer:masterfrom
intrepion:doctrine-writer-many-to-many

Conversation

@intrepion

Copy link
Copy Markdown

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.

@ddeboer

ddeboer commented Dec 6, 2014

Copy link
Copy Markdown
Owner

Interesting! Could you update DoctrineWriter docs too?

@intrepion

Copy link
Copy Markdown
Author

Sure thing!

@ddeboer

ddeboer commented Dec 7, 2014

Copy link
Copy Markdown
Owner

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

@Baachi

Baachi commented Jan 13, 2015

Copy link
Copy Markdown
Collaborator

Really cool 👍

@WouterSioen

Copy link
Copy Markdown
Contributor

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

@WouterSioen

Copy link
Copy Markdown
Contributor

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

@ip512

ip512 commented Apr 19, 2015

Copy link
Copy Markdown
Contributor

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

@ddeboer

ddeboer commented May 28, 2015

Copy link
Copy Markdown
Owner

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

Conflicts:
	src/Writer/DoctrineWriter.php
	tests/Writer/DoctrineWriterTest.php
@Nexotap

Nexotap commented Oct 2, 2015

Copy link
Copy Markdown

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);
         }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants