If you are creating a child entity and never persisting it explicitly, then
a cascade should be defined.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On Thu, Feb 8, 2018 at 11:52 AM, Sander <sandervanmoo...@gmail.com> wrote:

> Hi,
>
> I'm having some trouble with the following:
>
> I have 3 entities: (I know the naming of the properties is not great)
>
> User
>
> /**
>  * @ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerToken", 
> mappedBy="user")
>  */
> private $customersToken;
>
>
>
> Customer
>
> /**
>  * @ORM\OneToMany(targetEntity="CustomerToken", mappedBy="customer")
>  */
> private $tokens;
>
>
>
> CustomerToken
>
> /**
>  * @ORM\ManyToOne(targetEntity="Customer", inversedBy="tokens")
>  * @ORM\JoinColumn(name="customerId", referencedColumnName="id")
>  * @Assert\NotBlank()
>  */
> private $customer;
>
>
>
> /**
>  * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", 
> inversedBy="customersToken")
>  * @ORM\JoinColumn(name="userId", referencedColumnName="id")
>  */
> private $user;
>
>
> The CustomerToken table looks like this:
>
>
> <https://lh3.googleusercontent.com/-DzzrtZqMaUQ/Wnwo8CqN-uI/AAAAAAAACVE/CAPZSSpjMWMfM3re7zOJgAQdPZ-As4Z8wCLcBGAs/s1600/Selection_017.png>
>
>
> I'm using Symfony 3.4. In a Form I show all customers, when a user selects
> a company I get the users that belong to this customer through a Ajax call.
> There is no direct relation between user and customer,there is however a
> third entity relating the user and customer.
>
> When the user submits the form I want to create a new CustomerToken (the
> token itself is a generated string) that has both the user as the company
> linked. The way it is now, i'm getting all sorts of problems.
>
> if($form->isSubmitted() && $form->isValid()) {
>     // Create and set token
>     $token->generateAndSetToken();
>
>     // Save to DB
>     $em->persist($token);
>     $em->flush();
>
>     return $this->redirectToRoute('tokens_home', ['_env' => $_env]);
> }
>
>
>
> The strange thing is, it works in the dev envoirment (app_dev.php) and not
> in prod (app.php).In dev i'm not getting any errors and the row is created
> like I would expect. When I try to submit in prod (create a new
> CustomerToken) I get errors like this:
>
> [2018-02-08 09:11:11] request.CRITICAL: Uncaught PHP Exception 
> Doctrine\ORM\ORMInvalidArgumentException: "A new entity was found through the 
> relationship 'AppBundle\Entity\Customer#entities' that was not configured to 
> cascade persist operations for entity: 7. To solve this issue: Either 
> explicitly call EntityManager#persist() on this unknown entity or configure 
> cascade persist this association in the mapping for example 
> @ManyToOne(..,cascade={"persist"})." at 
> /home/sander/Documents/dev/am-admin/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php
>  line 102 {"exception":"[object] 
> (Doctrine\\ORM\\ORMInvalidArgumentException(code: 0): A new entity was found 
> through the relationship 'AppBundle\\Entity\\Customer#entities' that was not 
> configured to cascade persist operations for entity: 7. To solve this issue: 
> Either explicitly call EntityManager#persist() on this unknown entity or 
> configure cascade persist this association in the mapping for example 
> @ManyToOne(..,cascade={\"persist\"}). at 
> /home/sander/Documents/dev/am-admin/vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php:102)"}
>  []
>
>
> I can work around this by setting cascade:{"persist"} but it will just go
> on like that until I hit the point where I get a error inserting the new
> row in the table. The strange thing is, the table that is throwing the
> error is the user and later customer table.
> So it looks like it wants to create a new user and customer, instead of a
> customertoken.
>
> I think I may need to make use of a join table ?
>
> Any help would be appreciated!
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "doctrine-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to doctrine-user+unsubscr...@googlegroups.com.
> To post to this group, send email to doctrine-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/doctrine-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to doctrine-user+unsubscr...@googlegroups.com.
To post to this group, send email to doctrine-user@googlegroups.com.
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to