Doctrine One-To-Many Relationship Won't Save - Integrity Constraint Violation

前端 未结 1 1583
生来不讨喜
生来不讨喜 2021-01-17 22:53

I\'m attempting to use Doctrine ORM Associations. I\'ve read several tutorials and the online docs, but it\'s not working, and I\'m honestly not sure what I\'m doing wrong h

1条回答
  •  清酒与你
    2021-01-17 23:52

    The problem is that you are not setting the user entity in your history class. The association stuff does not do this automatically.

    class UserHistory
    {
        public function setUser($user) { $this->user = $user; }
    
    class User
    {
        public function addHistory($history)
        {
            $this->history->add($history);
            $history->addUser($this);  // *** This is what you are missing
        }
    
    // In your controller class
    $user->addHistory($history);
    

    0 讨论(0)
提交回复
热议问题