I've got following problem and I can't see any solution (even less probable after analysing doctrine source code). The problem is following:
I've got two entity classes, let's say Author and Book. My assumption is that one book can be written by many authors, so it will be many-to-many relation with Author as an owner. Now I want to have one action where I can create new book and immediately assign it to existing author and I know id of an author so it shouldn't be required to actually select the author from database. And so I can try to:
$book = new Book();
//assign data to book
$author = $em->getPartialReference('Author', $idOfAuthor);
$author->addBook($book);
$em->persist($author);
$em->flush();
What's the problem with the above code? Let's say that relation has cascade="persist". If anyone of you would try to do that, you'll get really odd result: book will be added to database, but relation won't be saved because partial reference is marked as readOnly in doctrine UnitOfWork and because of that doctrine won't save any of modifications made on it but for some reason it WILL follow cascade persistance.
So we can try different approach:
$book = new Book();
//assign data to book
$author = $em->getReference('Author', $idOfAuthor);
$author->addBook($book); //here's problematic line
$em->persist($author);
$em->flush();
That one works but it will select all data of an author at line that i've commented as 'problematic' because proxy class provided by doctrine as reference will lazy-load all attributes on any method call performed on author with just one exception ant it's getId() which is obviously useless in that situation.
So my question is: Am I wrong? Did I miss something?