简体   繁体   English

具有多个实体的Symfony2表单提交速度慢

[英]Symfony2 form with multiple entities slow on submit

I have an entity called Materials with 8 associated entities (with join tables). 我有一个名为Materials的实体,具有8个关联实体(带有联接表)。 When I submit the form to update existing entities/records in the database, it takes up to 24 seconds to finish the process. 当我提交表单以更新数据库中的现有实体/记录时,最多需要24秒才能完成该过程。 I read somewhere that I shouldn't use: 我读了一些我不应该使用的地方:

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

Because multiple entities would take too long to persist, but to boost performance I should use the update query: 因为多个实体需要很长时间才能持久,但是要提高性能,我应该使用更新查询:

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 https://ourcodeworld.com/articles/read/2/5-simple-tips-for-boost-the-database-handling-with-symfony2-and-doctrine

Does anybody know if that is correct and if so, will I have to really update every entity manually? 有人知道这是否正确吗?如果正确,我是否真的必须手动更新每个实体? It will take me a long time to write this as queries, since there are so many. 因为有很多查询,我将花很长时间将其写为查询。

I found the problem. 我发现了问题。

The form used to fail at $form->handleRequest($request); 表单过去在$form->handleRequest($request);处失败$form->handleRequest($request); in my controller: 在我的控制器中:

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

But the real problem was that my Assert statements in my Material class were not well defined. 但是真正的问题是我的Material类中的Assert语句定义不正确。 As soon as I corrected them handleRequest no longer slowed down the form submission process. 我更正了它们后, handleRequest不再减慢表单提交过程的速度。

Here is my updated Material class with the fixed Assert statements: 这是我使用固定的Assert语句更新的Material类:

/**
 * @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;
    }


}

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

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