Doctrine 2, error inserting in table with foreign key

josecash

I'm very new using Doctrine, is the first project I work with it and I'm having an error while I try to insert a new user.

The thing is I've got a class User with a foreign key Country and when I try to insert a user Doctrine also try to insert the country, the country already exists so PDO launch an integrity constraint violation and Doctrine a Doctrine\DBAL\DBALException.

I know the annotation cascade={"persist"} makes the country entity to be written in the db, without it, doctrine launch another error:

A new entity was found through the relationship 'User#country' that was not configured to cascade persist operations for entity: Country@0000000078b1861f00007f935266d9fe. 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"}). If you cannot find out which entity causes the problem implement 'Country#__toString()' to get a clue.

I've tried with all cascade options and only with persist and all the error above doesn't come up...

Is there something like cascade={"no-persist"} or something that tells doctrine the value of this attribute must be already inserted in table country???

Some code:

/**
 * User
 *
 * @Table(name="user")
 * @Entity
 */
class User {
       ...
        /**
         * @var Country
         *
         * @OneToOne(targetEntity="Country", cascade={"persist"})
         * @JoinColumn(name="country", referencedColumnName="id")
         */
       private $country
       ...
    }
    /**
     * Country
     *
     * @Table(name="country")
     * @Entity
     */
        class Country {
           ...
            /**
             * @var integer
             *
             * @Column(name="id", type="integer")
             * @Id
             */
            private $id;

        }

Any clue will be highly appreciated. Thanks.

Cerad

Put the cascade=persist back in.

You need to check the database to see if the country exists. Your insert with an existing country fails because the country object needs to be managed by the entity manager.

$country = $countryRepository->find($countryId);
if (!$country) 
{
    $country = new Country();
    $entityManager->persist($country);
}
$user->setCountry($country);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting a foreign key into a table

From Dev

Inserting a Foreign Key into a table using C#

From Dev

Doctrine ORM Error: A Foreign Key Constraint Fails

From Dev

Doctrine ORM Error: A Foreign Key Constraint Fails

From Dev

doctrine2 with codeigniter foreign key insert

From Dev

Inserting multiple rows into a table with the same foreign key in column1 & different values for column2

From Dev

MySQL - Inserting Primary Key from one table to another (Foreign Key)

From Dev

error with alter table and foreign key

From Dev

New records inserted in foreign key table when inserting in parent table

From Dev

creating table with primary key and 2 foreign keys with error

From Dev

Doctrine INDEX BY foreign key

From Dev

C# to mysql - inserting new row to table with foreign key

From Dev

Errors while inserting pandas dataframe to a table due to foreign key

From Dev

Symfony 3.1 Doctrine Entity Cannot alter table add foreign key

From Dev

Doctrine 2 Symfony 2 Getting foreign key entities without mapping

From Dev

Symfony2 Inserting two entities related using foreign key

From Dev

Error during the creation of table due to foreign key

From Dev

Loading a table into PostgreSQL with a foreign key produces an error

From Dev

Error creating mysql table with Foreign Key

From Dev

ERROR when referencing foreign key in table

From Dev

MySQL - foreign key constrained by primary key in same table, error #1452

From Dev

inserting data from primary key to foreign key

From Dev

Entity Framework foreign key inserting duplicate key

From Dev

SQLSTATE[HY000]: General error: 1025 Error on rename error when dropping foreign key in Doctrine Migration

From Dev

Duplicate key constraint error when inserting to empty table with no sequenced columns

From Dev

Duplicate key constraint error when inserting to empty table with no sequenced columns

From Dev

Foreign key failure when inserting into child table ( referencing composite unique index)

From Dev

Error Inserting: no such table error

From Dev

Symfony Doctrine - retrieve records from a related table using the foreign key field

Related Related

  1. 1

    Inserting a foreign key into a table

  2. 2

    Inserting a Foreign Key into a table using C#

  3. 3

    Doctrine ORM Error: A Foreign Key Constraint Fails

  4. 4

    Doctrine ORM Error: A Foreign Key Constraint Fails

  5. 5

    doctrine2 with codeigniter foreign key insert

  6. 6

    Inserting multiple rows into a table with the same foreign key in column1 & different values for column2

  7. 7

    MySQL - Inserting Primary Key from one table to another (Foreign Key)

  8. 8

    error with alter table and foreign key

  9. 9

    New records inserted in foreign key table when inserting in parent table

  10. 10

    creating table with primary key and 2 foreign keys with error

  11. 11

    Doctrine INDEX BY foreign key

  12. 12

    C# to mysql - inserting new row to table with foreign key

  13. 13

    Errors while inserting pandas dataframe to a table due to foreign key

  14. 14

    Symfony 3.1 Doctrine Entity Cannot alter table add foreign key

  15. 15

    Doctrine 2 Symfony 2 Getting foreign key entities without mapping

  16. 16

    Symfony2 Inserting two entities related using foreign key

  17. 17

    Error during the creation of table due to foreign key

  18. 18

    Loading a table into PostgreSQL with a foreign key produces an error

  19. 19

    Error creating mysql table with Foreign Key

  20. 20

    ERROR when referencing foreign key in table

  21. 21

    MySQL - foreign key constrained by primary key in same table, error #1452

  22. 22

    inserting data from primary key to foreign key

  23. 23

    Entity Framework foreign key inserting duplicate key

  24. 24

    SQLSTATE[HY000]: General error: 1025 Error on rename error when dropping foreign key in Doctrine Migration

  25. 25

    Duplicate key constraint error when inserting to empty table with no sequenced columns

  26. 26

    Duplicate key constraint error when inserting to empty table with no sequenced columns

  27. 27

    Foreign key failure when inserting into child table ( referencing composite unique index)

  28. 28

    Error Inserting: no such table error

  29. 29

    Symfony Doctrine - retrieve records from a related table using the foreign key field

HotTag

Archive