-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DDC-3745: OneToOne identity through foreign entity exception on flush #4584
Comments
Hello, I would like to ask you if is there any progress? I have the same problem. Thank you. |
Hello, is there any progress with this bug? Is it fixed |
Same issue for me. It there any fix or workaround? |
+1 |
Same here. |
any fix or workaround? |
This was already fixed on #1570 when I've ran the test bellow on <?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\OrmFunctionalTestCase;
class xxxxTest extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(xxxxUser::class),
$this->_em->getClassMetadata(xxxxUserProfile::class),
]
);
}
public function testThingsWorks()
{
$user = new xxxxUser();
$this->_em->persist($user);
$this->_em->flush();
}
}
/**
* @Entity
*/
class xxxxUser
{
/**
* @Id
* @Column(name="uid", type="integer")
* @GeneratedValue(strategy="AUTO")
*
* @var int
*/
private $id;
/**
* @var xxxxUserProfile|null
*
* @OneToOne(targetEntity=xxxxUserProfile::class, mappedBy="user", cascade="persist")
*/
private $profile = null;
public function __construct()
{
$this->profile = new xxxxUserProfile($this);
}
}
/**
* @Entity
*/
class xxxxUserProfile
{
/**
* – UserProfile's "uid" column points to User's "uid" column
* – it is PK (primary key)
* - it is FK (foreign key) as well
* – "owning side"
*
* @var xxxxUser
*
* @Id
* @OneToOne(targetEntity=xxxxUser::class, inversedBy="profile")
* @JoinColumn(name="uid", referencedColumnName="uid", nullable=false)
*/
private $user;
public function __construct(xxxxUser $user)
{
$this->user = $user;
}
} On the PR we have the explanation why this wasn't backported on |
I have the same issue
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\OrmFunctionalTestCase;
class xxxxTest extends OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(xxxxUser::class),
$this->_em->getClassMetadata(xxxxUserProfile::class),
]
);
}
public function testThingsWorks()
{
$user = new xxxxUser();
$this->_em->persist($user);
$this->_em->persist($user->getProfile());
$this->_em->flush();
}
}
/**
* @Entity
*/
class xxxxUser
{
/**
* @Id
* @Column(name="uid", type="integer")
* @GeneratedValue(strategy="AUTO")
*
* @var int
*/
private $id;
/****
* @var xxxxUserProfile|null
*
* @OneToOne(targetEntity=xxxxUserProfile::class, mappedBy="user", cascade={"persist"})
*/
private $profile = null;
public function __construct()
{
$this->profile = new xxxxUserProfile($this);
}
public function getProfile()
{
return $this->profile;
}
}
/**
* @Entity
*/
class xxxxUserProfile
{
/**
* – UserProfile's "uid" column points to User's "uid" column
* – it is PK (primary key)
* - it is FK (foreign key) as well
* – "owning side"
*
* @var xxxxUser
*
* @Id
* @OneToOne(targetEntity=xxxxUser::class, inversedBy="profile")
* @JoinColumn(name="uid", referencedColumnName="uid", nullable=false)
*/
private $user;
public function __construct(xxxxUser $user)
{
$this->user = $user;
}
} Upd. the problem is in four asterisks in phpdoc for /**** <------ wrong phpdoc
* @var xxxxUserProfile|null
*
* @OneToOne(targetEntity=xxxxUserProfile::class, mappedBy="user", cascade={"persist"})
*/
private $profile = null; |
@igaponov you're right about the docblock, now my test fails as well... and it actually fails on IMO this is an edge-case since it only happens if @guilhermeblanco @Ocramius I'd say this is a |
After updating to Doctrine 2.6 I get the same error while before it was working fine. I now have to use two |
Im also hitting the same issue. I've identified it for now with |
Just followed the doctrine docs on Identity through foreign Entities and stumbled upon this bug. When I create a new Article using the entities declared in "Use-Case 1: Dynamic Attributes", I get the mentioned exception: $article = new Article();
$article->addAttribute("foo", "bar");
$this->em->persist($article); // throws ORMInvalidArgumentException This was really unexpected. @ro0NL thanks for the tip. Initializing the auto-generated id works: class Article
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue */
private $id = -1; Maybe this bug should be reopened? Or mentioned in the docs? |
I have the same error too after updating to Doctrine 2.6. I can't apply the @ro0NL tip because I can't change the DB structure. |
For people having issues with 2.6, see #7003. |
Jira issue originally created by user Daveet:
Also asked at SO: https://stackoverflow.com/questions/30402203
I'm adding it here as well as an issue here, because I believe it just might be Doctrine's bug.
I have
User
andUserProfile
OneToOne–related Doctrine ORM entities. They should always exist as a pair, there should be noUser
withoutUserProfile
.User should get its id from autoincrement, while UserProfile should have User's id. So they both should have the same id and there is no other column to set up the relationship (Doctrine docs: Identity through foreign Entities).
User's id is both a primary key (PK) and foreign key (FK) at the same time.
I managed to set it up, but it requires that User is saved first and only later UserProfile is created and saved in a separate step.
What I want is that UserProfile is always created with User, in the constructor, but if I do that, I get this exception:
Doctrine\ORM\ORMInvalidArgumentException: The given entity of type 'AppBundle\Entity\UserProfile' (AppBundle\Entity\UserProfile@0000000052e1b1eb00000000409c6f2c) has no identity/no id values set. It cannot be added to the identity map.
Please see code below – it works, but not the way I want. The php comments show what I want to achieve.
The text was updated successfully, but these errors were encountered: