Reputation: 40
I have a many-to-one relationship with the 'login_log' and 'user_info' and 'user_account'. this is three entity's summary.
class UserInfo extends Entity {
/**
* @ORM\Column(name="user_id", type="integer")
* @ORM\Id
*/
protected $userId;
/**
* @ORM\Column(name="true_name", type="string", length=20)
*/
protected $trueName;
...
}
class UserAccount extends Entity {
/**
* @ORM\Column(name="user_id", type="integer")
* @ORM\Id
*/
protected $userId;
/**
* @ORM\Column(name="user_name", type="string", length=20, nullable=true)
*/
protected $userName;
...
}
class LoginLog extends Entity {
/**
* @ORM\Column(name="log_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $logId;
/**
* @ORM\ManyToOne(targetEntity="UserInfo")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
protected $userInfo;
/**
* @ORM\ManyToOne(targetEntity="UserAccount")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
protected $userAccount;
/**
* @ORM\Column(name="user_id", type="integer")
*/
protected $userId;
...
}
when i want add login log:
$log = new LoginLog();
$log->setUserId(11);
$em->persist($log);
$em->flush();
but in symfony profiler, i got those query:
INSERT INTO login_log (user_id) VALUES(?)
Parameters: { 1: null}
why first parameters is null!
Upvotes: 0
Views: 299
Reputation: 1491
this makes no sence :
/**
* @ORM\ManyToOne(targetEntity="UserAccount")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
*/
protected $userAccount;
/**
* @ORM\Column(name="user_id", type="integer")
*/
protected $userId;
they are both using the same columnname, get rid of the userId property, and use a setter for userAccount.
Right now, you are setting one thing, and when makeing the query, doctrine freaks out, and uses the other one.
Understand what i mean ?
$log = new LoginLog();
$log->setUserAccount($user); // $user beeing an actual user, not an ID
$em->persist($log);
$em->flush();
Upvotes: 1