在flush

时间:2015-09-11 06:56:12

标签: php symfony doctrine-orm

我的代码出了问题。我只是想在我的数据库中插入一组简单的数据,但是doctrine将我的外键插入为NULL。

我已经转储了我的数据并发现,我的外键(adr_id)中有一些值,但是在我刷新后它被设置为NULL。

这是我的控制者:

       $emblem = new Emblems();

    $em = $this->getDoctrine()->getManager();

    $form = $this->createForm(new EmblemType(), $emblem);

        $form->handleRequest($request);

        if ( $form->isValid()) {

            $attachmentArray = $form['attachment']->getData();
                foreach ( $attachmentArray as $attachment ) {
                    if ( $attachment != NULL ) {
                    $random = rand(0,99999999);
                    $dir = $this->container->getParameter('kernel.root_dir').'/../web/uploads/embleme';
                    $extension = $attachment->getClientOriginalextension();
                    $attachment->move($dir, $random . '.' . $extension);

                    $image = new Images();

                    $image->setFotoid($random);
                    $image->setArticleId($emblem->getArticleId());
                    $image->setMimetype($extension);

                    $em->persist($image);
                    $em->flush();
                    }   

                }

        $em->persist($emblem);
        $em->flush();

        $this->get('session')->getFlashBag()->add('notice', 'Änderungen wurden erfolgreich gespeichert!');

        return $this->render('NobleBundle:Default:new.html.twig', array('form' => $form->createView()));

我的ORM映射:(注意:如果我删除"多对一",它运作完美,但我没有相关实体那么......

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="NobleBundle\Entity\Emblems" table="emblems">
<id name="id" type="integer" column="id">
  <generator strategy="IDENTITY"/>
</id>
<field name="articleId" type="integer" column="article_id" nullable="false"/>
<field name="articleDesc" type="text" column="article_desc" nullable="false"/>
<field name="articleDescShort" type="text" column="article_desc_short" nullable="false"/>
<field name="adrId" type="integer" column="adr_id" nullable="false"/>
<field name="amount" type="integer" column="amount" nullable="false"/>
        <many-to-one
        field="adress"
        target-entity="Adresses"
        inversed-by="emblem"
        join-column="adress">

        <join-column name="adr_id" referenced-column-name="id" />
        </many-to-one>
 </entity>
 </doctrine-mapping>

之前我已经完成了这项工作,并且真的只是复制了我以前的代码,所以我不知道问题出在哪里。

修改

Emblems Entity

<?php

namespace NobleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Emblems
 */
 class Emblems
 {
/**
 * @var integer
 */
private $articleId;

/**
 * @var string
 */
private $articleDesc;

/**
 * @var string
 */
private $articleDescShort;

/**
 * @var integer
 */
private $adrId;

/**
 * @var integer
 */
private $amount;

/**
 * @var integer
 */
private $id;


/**
 * Set articleId
 *
 * @param integer $articleId
 * @return Emblems
 */
public function setArticleId($articleId)
{
    $this->articleId = $articleId;

    return $this;
}

/**
 * Get articleId
 *
 * @return integer 
 */
public function getArticleId()
{
    return $this->articleId;
}

/**
 * Set articleDesc
 *
 * @param string $articleDesc
 * @return Emblems
 */
public function setArticleDesc($articleDesc)
{
    $this->articleDesc = $articleDesc;

    return $this;
}

/**
 * Get articleDesc
 *
 * @return string 
 */
public function getArticleDesc()
{
    return $this->articleDesc;
}

/**
 * Set articleDescShort
 *
 * @param string $articleDescShort
 * @return Emblems
 */
public function setArticleDescShort($articleDescShort)
{
    $this->articleDescShort = $articleDescShort;

    return $this;
}

/**
 * Get articleDescShort
 *
 * @return string 
 */
public function getArticleDescShort()
{
    return $this->articleDescShort;
}

/**
 * Set adrId
 *
 * @param integer $adrId
 * @return Emblems
 */
public function setAdrId($adrId)
{
    $this->adrId = $adrId;

    return $this;
}

/**
 * Get adrId
 *
 * @return integer 
 */
public function getAdrId()
{
    return $this->adrId;
}

/**
 * Set amount
 *
 * @param integer $amount
 * @return Emblems
 */
public function setAmount($amount)
{
    $this->amount = $amount;

    return $this;
}

/**
 * Get amount
 *
 * @return integer 
 */
public function getAmount()
{
    return $this->amount;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

private $attachment;

/**
 * Set attachment
 */
public function setAttachment($attachment = null) 
{
    $this->attachment = $attachment;

    return $this;
}

/**
 * Get attachment
 */

public function getAttachment()
{
    return $this->attachment;
}

/**
 * @var \NobleBundle\Entity\Adresses
 */
private $adress;

/**
 * Set adress
 *
 * @param \NobleBundle\Entity\Adresses $adress
 * @return Emblems
 */
public function setAdress(\NobleBundle\Entity\Adresses $adress = null)
{
    $this->adress = $adress;

    return $this;
}

/**
 * Get adress
 *
 * @return \NobleBundle\Entity\Adresses 
 */
public function getAdress()
{
    return $this->adress;
}
}

申请实体

<?php

namespace NobleBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Adresses
 */
 class Adresses
{
/**
 * @var integer
 */
private $adrNo;

/**
 * @var string
 */
private $adrName;

/**
 * @var string
 */
private $adrOrt;

/**
 * @var integer
 */
private $id;


/**
 * Set adrNo
 *
 * @param integer $adrNo
 * @return Adresses
 */
public function setAdrNo($adrNo)
{
    $this->adrNo = $adrNo;

    return $this;
}

/**
 * Get adrNo
 *
 * @return integer 
 */
public function getAdrNo()
{
    return $this->adrNo;
}

/**
 * Set adrName
 *
 * @param string $adrName
 * @return Adresses
 */
public function setAdrName($adrName)
{
    $this->adrName = $adrName;

    return $this;
}

/**
 * Get adrName
 *
 * @return string 
 */
public function getAdrName()
{
    return $this->adrName;
}

/**
 * Set adrOrt
 *
 * @param string $adrOrt
 * @return Adresses
 */
public function setAdrOrt($adrOrt)
{
    $this->adrOrt = $adrOrt;

    return $this;
}

/**
 * Get adrOrt
 *
 * @return string 
 */
public function getAdrOrt()
{
    return $this->adrOrt;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $emblem;

/**
 * Constructor
 */
public function __construct()
{
    $this->emblems = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add emblem
 *
 * @param \NobleBundle\Entity\Emblems $emblem
 * @return Adresses
 */
public function addEmblem(\NobleBundle\Entity\Emblems $emblem)
{
    $this->emblem[] = $emblem;

    return $this;
}

/**
 * Remove emblem
 *
 * @param \NobleBundle\Entity\Emblems $emblems
 */
public function removeEmblem(\NobleBundle\Entity\Emblems $emblem)
{
    $this->emblem->removeElement($emblem);
}

/**
 * Get emblem
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getEmblem()
{
    return $this->emblem;
}
}

0 个答案:

没有答案