简体   繁体   English

Symfony2多对一获取空参数

[英]Symfony2 many to one get null parameter

I have a many-to-one relationship with the 'login_log' and 'user_info' and 'user_account'. 我与“ login_log”,“ user_info”和“ 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: 但是在symfony profiler中,我得到了这些查询:

INSERT INTO login_log (user_id) VALUES(?) 
Parameters: { 1: null}  

why first parameters is null! 为什么第一个参数为空!

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. 它们都使用相同的列名,摆脱了userId属性,并对userAccount使用了setter。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM