具有多个实体的Symfony2表单在提交时变慢

时间:2018-01-12 22:18:40

标签: forms performance symfony submit symfony-2.8

我有一个名为Materials的实体,有8个关联实体(带连接表)。 当我提交表单来更新数据库中的现有实体/记录时,最多需要24秒才能完成该过程。我读到了一些我不应该使用的地方:

$em = $this->getDoctrine()->getManager();
$form->handleRequest($request);
$data = $form->getData();
$em->persist($data);
$em->flush();

因为多个实体需要花费太长时间才能坚持,但为了提高性能,我应该使用更新查询:

Exp.
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('ourcodeworldBundle:Posts');
$newCreatedAt = new \DateTime();

$qb = $repo->createQueryBuilder('p');
    $qb->update()
        ->set('p.createdAt', ':newCreatedAt')
        ->setParameter('newCreatedAt', $newCreatedAt); 
$qb->getQuery()->execute();

https://ourcodeworld.com/articles/read/2/5-simple-tips-for-boost-the-database-handling-with-symfony2-and-doctrine

有人知道这是否正确,如果是,我是否必须手动更新每个实体?我需要很长时间才能将其写为查询,因为它有很多。

1 个答案:

答案 0 :(得分:0)

我发现了问题。

用于在我的控制器中$form->handleRequest($request);处失败的表单:

if ($request->getMethod() == 'POST') {
  $req = $request->request->get("material");
  $form->handleRequest($request);
  ...
}

但真正的问题是我的Material类中的Assert语句没有很好地定义。一旦我更正了handleRequest,就不再减慢表单提交过程。

这是我更新的Material类,带有固定的Assert语句:

/**
 * @ORM\Table(name="materials")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\MaterialRepository")
 */
class Material
{
    /**
     * @var integer
     *
     * @ORM\Column(name="materialID", type="integer", nullable=true)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $materialID;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="lrcID", type="integer", nullable=true)
     */
    private $lrcID;

    /**
     * @var string
     *
     * @Assert\Type("\string")
     * @Assert\Length(max=255)
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     */
    private $title;

    /**
     * @var string
     *
     * @Assert\Type("\string")
     * @ORM\Column(name="description", type="text", nullable=false)
     */
    private $description;

    /**
     * @var string
     *
     * @Assert\Type("\string")
     * @Assert\Length(max=255)
     * @ORM\Column(name="author", type="string", length=255, nullable=true)
     */
    private $author;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="year", type="integer", nullable=true)
     */
    private $year;

    /**
     * @var File
     *
     * @Assert\File(
     *     maxSize = "10M",
     *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"},
     *     maxSizeMessage = "The maxmimum allowed file size is 10MB.",
     *     mimeTypesMessage = "Please, upload the imag as a jpge, gif, png, or tiff file."
     * )
     * @ORM\Column(name="image", type="string", length=100, nullable=true)
     */
    private $image;

    /**
     * @var \DateTime
     *
     * @Assert\NotBlank()
     * @Assert\Type("\DateTime")
     * @ORM\Column(name="dateModified", type="datetime", nullable=true)
     */
    private $dateModified;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="isActive", type="integer", nullable=true)
     */
    private $isActive;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="isFree", type="integer", nullable=true)
     */
    private $isFree;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="sizevalue", type="integer", nullable=true)
     */
    private $sizevalue;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="sizeunit", type="integer", nullable=true)
     */
    private $sizeunit;

    /**
     * @var integer
     *
     * @Assert\Type("\integer")
     * @ORM\Column(name="isComplete", type="integer", nullable=true)
     */
    private $isComplete;

    /**
     *
     * @Assert\Url(
     *    checkDNS = true,
     *    message = "The url '{{ value }}' is not a valid url",
     *    dnsMessage = "The host '{{ value }}' could not be resolved.",
     * )
     * @Assert\Length(max=255)
     * @ORM\Column(name="url", type="string", length=255, nullable=false)
     */
    private $url;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialLanguage")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialLanguage", inversedBy="material")
     * @ORM\JoinTable(name="materials_language_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="languageID", referencedColumnName="languageID", nullable=false)})
     */
    public $materiallanguage;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialType")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialType", inversedBy="material")
     * @ORM\JoinTable(name="materials_type_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="typeID", referencedColumnName="typeID", nullable=false)})
     */
    public $materialtype;


    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialAudience")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialAudience", inversedBy="material")
     * @ORM\JoinTable(name="materials_audience_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="audienceID", referencedColumnName="audienceID", nullable=false)})
     */
    public $materialaudience;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialLevel")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialLevel", inversedBy="material")
     * @ORM\JoinTable(name="materials_level_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="levelID", referencedColumnName="levelID", nullable=false)})
     */
    public $materiallevel;


    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialFormat")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialFormat")
     * @ORM\JoinTable(name="materials_format_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="formatid", referencedColumnName="formatid", nullable=false)})
     */
    public $materialformat;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialSpecificMedium")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialSpecificMedium")
     * @ORM\JoinTable(name="materials_specificmedium_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="specificmediumID", referencedColumnName="specificmediumid", nullable=false)})
     */
    public $materialspecificmedium;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialSizeUnits")
     * @Assert\Valid()
     * @ORM\ManyToOne(targetEntity="MaterialSizeUnits", inversedBy="material")
     * @ORM\JoinColumn(name="sizeunit", referencedColumnName="id", nullable=true)
     */
    public $materialsizeunits;

    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialCategory")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialCategory")
     * @ORM\JoinTable(name="materials_category_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="categoryID", referencedColumnName="categoryID", nullable=false)})
     */
    public $materialcategory;


    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialKeyword")
     * @Assert\Valid()
     * @ORM\ManyToMany(targetEntity="MaterialKeyword", inversedBy="material")
     * @ORM\JoinTable(name="materials_keyword_map",
     *      joinColumns={@ORM\JoinColumn(name="materialID", referencedColumnName="materialID", nullable=false)},
     *      inverseJoinColumns={@ORM\JoinColumn(name="keywordID", referencedColumnName="id", nullable=false)})
     */
    public $materialkeyword;


    /**
     * @Assert\Type(type="AppBundle\Entity\MaterialYear")
     * @Assert\Valid()
     * @ORM\ManyToOne(targetEntity="MaterialYear")
     * @ORM\JoinColumn(name="year", referencedColumnName="yearID")
     */
    public $materialyear;


    /**
     * @Assert\Type(type="AppBundle\Entity\Lrc")
     * @Assert\Valid()
     * @ORM\ManyToOne(targetEntity="Lrc", inversedBy="material")
     * @ORM\JoinColumn(name="lrcID", referencedColumnName="id")
     */
    public $lrc;


    /**
    * Constructor
    */
    public function __construct()
    {
        $this->MaterialLanguage = new ArrayCollection();
        $this->MaterialType = new ArrayCollection();
        $this->MaterialLevel = new ArrayCollection();
        $this->MaterialAudience = new ArrayCollection();
        $this->MaterialFormat = new ArrayCollection();
        $this->MaterialSpecificMedium = new ArrayCollection();
        $this->MaterialSizeUnits = new ArrayCollection();
        $this->MaterialCategory = new ArrayCollection();
        $this->MaterialKeyword = new ArrayCollection();
        $this->MaterialYear = new ArrayCollection();
        $this->Lrc = new ArrayCollection();
    }




    /**
     * Set materiallanguage
     *
     * @param array $materiallanguage
     *
     */
    public function setMateriallanguage(ArrayCollection $materiallanguage)
    {
        $this->materiallanguage = $materiallanguage;
    }

    /**
     * Get materiallanguage
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMateriallanguage()
    {
        return $this->materiallanguage;
    }

    /**
     * Set materialtype
     *
     * @param array $materialtype
     *
     */
    public function setMaterialtype(ArrayCollection $materialtype)
    {
        $this->materialtype = $materialtype;
    }

    /**
     * Get materialtype
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialtype()
    {
        return $this->materialtype;
    }


    /**
     * Set materialaudience
     *
     * @param array $materialaudience
     *
     */
    public function setMaterialaudience(ArrayCollection $materialaudience)
    {
        $this->materialaudience = $materialaudience;
    }

    /**
     * Get materialaudience
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialaudience()
    {
        return $this->materialaudience;
    }

    /**
     * Set materialformat
     *
     * @param array $materialformat
     *
     */
    public function setMaterialformat(ArrayCollection $materialformat)
    {
        $this->materialformat = $materialformat;
    }

    /**
     * Get materialformat
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialformat()
    {
        return $this->materialformat;
    }



    /**
     * Set materialspecificmedium
     *
     * @param array $materialspecificmedium
     *
     */
    public function setMaterialspecificmedium(ArrayCollection $materialspecificmedium)
    {
        $this->materialspecificmedium = $materialspecificmedium;
    }

    /**
     * Get materialspecificmedium
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialspecificmedium()
    {
        return $this->materialspecificmedium;
    }


    /**
     * Set materiallevel
     *
     * @param array $materiallevel
     *
     */
    public function setMateriallevel(ArrayCollection $materiallevel)
    {
        $this->materiallevel = $materiallevel;
    }

    /**
     * Get materiallevel
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMateriallevel()
    {
        return $this->materiallevel;
    }


    /**
     * Set materialsizeunits
     *
     * @param array $materialsizeunits
     *
     */
    public function setMaterialsizeunits(MaterialSizeUnits $materialsizeunits)
    {
        $this->materialsizeunits = $materialsizeunits;
    }

    /**
     * Get materialsizeunits
     *
     * @return array
     */
    public function getMaterialsizeunits()
    {
        return $this->materialsizeunits;
    }

    /**
     * Set materialcategory
     *
     * @param array $materialcategory
     *
     */
    public function setMaterialcategory(ArrayCollection $materialcategory)
    {
        $this->materialcategory = $materialcategory;
    }

    /**
     * Get materialcategory
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialcategory()
    {
        return $this->materialcategory;
    }


    /**
     * Set materialkeyword
     *
     * @param array $materialkeyword
     *
     */
    public function setMaterialkeyword(MaterialKeyword $materialkeyword)
    {
        $this->materialkeyword = $materialkeyword;
    }

    /**
     * Get materialkeyword
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialkeyword()
    {
        return $this->materialkeyword;
    }


    /**
     * Set materialyear
     *
     * @param array $materialyear
     *
     */
    public function setMaterialyear(MaterialYear $materialyear)
    {
        $this->materialyear = $materialyear;
    }

    /**
     * Get materiallamaterialyear
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getMaterialyear()
    {
        return $this->materialyear;
    }




    /**
     * Set lrc
     *
     * @param array $lrc
     *
     */
    public function setLrc(Lrc $lrc=null)
    {
        $this->lrc = $lrc;
    }

    /**
     * Get lrc
     *
     * @Assert\Type("\array")
     * @return array
     */
    public function getLrc()
    {
        return $this->lrc;
    }


    /**
     * Set materialID
     *
     * @param integer $materialID
     *
     * @return Material
     */
    public function setMaterialID($materialID)
    {
        $this->materialID = $materialID;

        return $this;
    }

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

    /**
     * Set lrcID
     *
     * @param integer $lrcID
     *
     * @return Material
     */
    public function setLrcID($lrcID)
    {
        $this->lrcID = $lrcID;

        return $this;
    }

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

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Material
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Material
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

    /**
     * Set author
     *
     * @param string $author
     *
     * @return Material
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

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

    /**
     * Set year
     *
     * @param integer $year
     *
     * @return Material
     */
    public function setYear($year)
    {
        $this->year = $year;

        return $this;
    }

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

    /**
     * Set image
     *
     * @param string $image
     *
     * @return Material
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

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


    /**
     * Set dateModified
     *
     * @param \DateTime $dateModified
     *
     * @return Material
     */
    public function setDateModified(\DateTime $dateModified)
    {
        $this->dateModified = $dateModified;

        return $this;
    }

    /**
     * Get dateModified
     *
     * @return \DateTime
     */
    public function getDateModified()
    {
        return $this->dateModified;
    }

    /**
     * Set isActive
     *
     * @param integer $isActive
     *
     * @return Material
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

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

    /**
     * Set isFree
     *
     * @param integer $isFree
     *
     * @return Material
     */
    public function setIsFree($isFree)
    {
        $this->isFree = $isFree;

        return $this;
    }

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

    /**
     * Set sizevalue
     *
     * @param integer $sizevalue
     *
     */
    public function setSizevalue($sizevalue)
    {
        $this->sizevalue = $sizevalue;

    }

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

    /**
     * Set sizeunit
     *
     * @param integer $sizeunit
     *
     * @return Material
     */
    public function setSizeunit($sizeunit)
    {
        $this->sizeunit = $sizeunit;

        return $this;
    }

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

    /**
     * Set isComplete
     *
     * @param integer $isComplete
     *
     * @return Material
     */
    public function setIsComplete($isComplete)
    {
        $this->isComplete = $isComplete;

        return $this;
    }

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

    /**
     * Set url
     *
     * @param string $url
     *
     * @return Material
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

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


}