我在2个相关表中插入记录,当一个记录插入父表时,我得到它最后插入的id(工作正常),但是当我将这个id提供给它的子表时,它接受它作为空值
这是我的代码:
$obj_users = new Users();
$obj_users->setFirst_name('ABC');
$entityManager->persist($obj_users);
$entityManager->flush();
echo $USERID = $obj_users->getId_user(); //this is working fine
$obj_serviceprovider = new ServiceProvider();
$obj_serviceprovider->setUser($USERID); // here it goes empty
$entityManager->persist($obj_serviceprovider);
$entityManager->flush();
我的代码有什么问题?
答案 0 :(得分:0)
试试这样:
$obj_users = new Users();
$obj_users->setFirst_name('ABC');
$entityManager->persist($obj_users);
$entityManager->flush();
echo $USERID = $obj_users->getId_user();
$obj_serviceprovider = new ServiceProvider();
$obj_serviceprovider->setUser($obj_users);
$entityManager->persist($obj_serviceprovider);
$entityManager->flush();
as doctrine存储id但在定义关系的子项时需要一个对象