Php Symfony原则实体插入错误

Php Symfony原则实体插入错误,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我真的很奇怪插入实体的问题 在foreach中,请使用以下代码: public function setGate($keyId, $elementId, $keywordType){ echo "<BR>setGate $keyId , $elementId"; if(!intval($keyId) or !intval($elementId)) throw new MeaException("$keyId,$elementId Keywords s

我真的很奇怪插入实体的问题

在foreach中,请使用以下代码:

public function setGate($keyId, $elementId, $keywordType){

    echo "<BR>setGate $keyId , $elementId";
    if(!intval($keyId) or !intval($elementId))
        throw new MeaException("$keyId,$elementId Keywords setGate empty");

    $gate = $this->repositoryGate
        ->createQueryBuilder('e')
            ->select('e.id')
            ->where('e.mkeywords_id=:keyid and e.elid=:elid')
            ->setParameters(array('keyid'=>$keyId,'elid'=>$elementId))
            ->getQuery()
        ->getOneOrNullResult();

    if($gate) return true;

    $gateObj = new KeywordsGate();

    $gateObj->setElid($elementId);
    $gateObj->setMkeywordsId($keyId);
    $gateObj->setMkeywordstypeId($keywordType);

    $this->em->persist($gateObj);

    try {
        $this->em->flush();
    }catch (\Exception $e){
        var_dump($gateObj);
        var_dump($e);
        die('e');
    }

    return $this;
}
并且在一个元素上有错误:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'mkeywords_id' cannot be null'
并在屏幕上显示:

object(Miejsce\KeywordsBundle\Entity\KeywordsGate)[1206]
  private 'id' => null
  private 'mkeywords_id' => int 1476
  private 'elid' => string '5922' (length=4)
  private 'mkeywordstype_id' => int 4
  private 'keywordElement' => null

object(Doctrine\DBAL\DBALException)[1208]
  protected 'message' => string 'An exception occurred while executing 'INSERT INTO mKeywordsGate (mkeywords_id, elid, mkeywordstype_id) VALUES (?, ?, ?)' with params [null, "5922", 4]:

那么,哪里会出现错误呢

请修复您的代码:$this->em->persist$gateObj而不是$this->em->persist$gateObji fixed-off-corse我在粘贴到StackOverflow时出错您能发布完整的foreach代码吗?mkeywords_id是外键列,对吗?如果是外键,则必须在$gateObj->setMkeywordsId$keyIdEntity上插入其实体;是的,你有权利-当我把对象和$gateObj->setKeywordElement$keywordObject;非$gateObj->setmkeywordsidinger$keyId;剧本很好。
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'mkeywords_id' cannot be null'
object(Miejsce\KeywordsBundle\Entity\KeywordsGate)[1206]
  private 'id' => null
  private 'mkeywords_id' => int 1476
  private 'elid' => string '5922' (length=4)
  private 'mkeywordstype_id' => int 4
  private 'keywordElement' => null

object(Doctrine\DBAL\DBALException)[1208]
  protected 'message' => string 'An exception occurred while executing 'INSERT INTO mKeywordsGate (mkeywords_id, elid, mkeywordstype_id) VALUES (?, ?, ?)' with params [null, "5922", 4]: