Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:25:56+00:00 2026-06-18T04:25:56+00:00

Quite a long time since I try to handle many-to many relationships with attribute.

  • 0

Quite a long time since I try to handle many-to many relationships with attribute.

I re-checked the entities, the forms… Everything looks ok. Still get an error :

The relationship is built like this: Book > OneToMany > AuthorBook < ManyToOne < Author

Nested forms allow to create books, from which authors are created with a year attribute..

So far, I’m only trying to record new authors, in order to make them exist before handling a relationship with a book.

Here is my code:

foreach ($book->getAuthorBooks() as $authorBook) {
   $AuthorUrlname = $this->mu->generateUrlname( $authorBook->getAuthor()->getFirstname().'-'.$authorBook->getAuthor()->getLastname() );

   // If the author does not exist yet, it is added to the database:

   $checkAuthor = $this->em->getRepository('cmdMyBundle:Author')->findOneByUrlname($AuthorUrlname);

   if ($checkAuthor == null) {                  
        // Author does not exist
        $newAuthor = new Author();
        $newAuthor->setLastname($authorBook->getAuthor()->getLastname());
        $newAuthor->setFirstname($authorBook->getAuthor()->getFirstname());
        $newAuthor->setUrlname($AuthorUrlname);
        $newAuthor->setCollection($this->collection);
        $this->em->persist($newAuthor);
        $this->em->flush();

    }else{
        // Author already exists
    }

}// foreach $authorBook

And the error:
A new entity was found through the relationship ‘cmd\MyBundle\Entity\Book#authorBooks’ that was not configured to cascade persist operations for entity: cmd\MyBundle\Entity\authorBook@0000000002571fc4000000002c4ddfaa.

Why is the error related to authorBooks relationship ? My code only tries to record a new author…

Thanks

EDIT_____________

Indeed, I made further tests, and a drastic one with this code :

public function onSuccess(Page $page){   
    $this->em->flush();

This also generates the same error, as if the error was not due to the form processing code…
I validated the orm schema using this method, and so far everything looks good.

I tried to cascade persist the entities :

class AuthorBook
{

/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="cmd\MyBundle\Entity\Author", inversedBy="authorBooks", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
private $author;

/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="cmd\MyBundle\Entity\Book", inversedBy="authorBooks", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
private $book;

—

class Book
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

 /**
 * @ORM\OneToMany(targetEntity="cmd\MyBundle\Entity\AuthorBook", mappedBy="book", cascade={"persist"})
 */
private $authorBooks;

—

class Author
{
/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\OneToMany(targetEntity="cmd\MyBundle\Entity\AuthorBook", mappedBy="author", cascade={"persist"})
 */
private $authorBooks;

This updates the error message:

Entity of type AuthorBook has identity through a foreign entity
Author, however this entity has no ientity itself. You have to call
EntityManager#persist() on the related entity and make sure it an
identifier was generated before trying to persist ‘AuthorBook’. In
case of Post Insert ID Generation (such as MySQL Auto-Increment or
PostgreSQL SERIAL) this means you have to call EntityManager#flush()
between both persist operations.

I understand I need to persist and flush books and authors before AuthorBook. However, I can’t flush anything. I even have this error when I don’t persist anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:25:57+00:00Added an answer on June 18, 2026 at 4:25 am

    There is something wrong with your class AuthorBook. Remove @ORM\Id for both $author and $book and add a correct id field. You should have had an error message when updating your schema, but in case you didn’t:

    php app/console doctrine:schema:update --force;
    

    Update AuthorBook to this:

    class AuthorBook
    {
    
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         *
         * @var integer $id
         */
        protected $id;
    
    
        /**
         * @ORM\ManyToOne(targetEntity="cmd\MyBundle\Entity\Author", inversedBy="authorBooks", cascade={"persist"})
         * @ORM\JoinColumn(nullable=false)
         */
        private $author;
    
        /**
         * 
         * @ORM\ManyToOne(targetEntity="cmd\MyBundle\Entity\Book", inversedBy="authorBooks", cascade={"persist"})
         * @ORM\JoinColumn(nullable=false)
         */
        private $book;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying for quite a long time and still it posts only as
Since expanding a large gzip file takes quite long time (sometimes over half a
It's been a long time since I've touched C++ and I've never been quite
I'm looking since quite a long time something like a CLLocationManager simulator that would
I have settings page in my wp7 application. I loads for quite long time
I've spent quite a long time Googling this and read various articles but I
I've been using Vim for quite a long time, but I'm at a level
I've been sitting on this idea for quite a long time and would like
Ladies and Gentlemen, For quite a long time now, I've been using the following
(Long time reader of SO, first time asking a q. I'm quite new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.