PHP-内存管理Symfony表单和原则
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-内存管理Symfony表单和原则,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3717字,纯文字阅读大概需要6分钟。
内容图文
![PHP-内存管理Symfony表单和原则](/upload/InfoBanner/zyjiaocheng/948/783a3e990fbf482ebf1ca863ff9e80c1.jpg)
使用Symfony构建表单时,表单的构建速度非常慢,并且内存高峰.
该表单是使用一些子表单构建的,并使用了一对多关系.当表单中的数据变大(多面有更多实体)时,表单变慢并且内存使用量也变大,尽管时间和内存使用量似乎没有变化,但这似乎还不错.
例如,在多端具有约71个实体时,内存使用量约为116 MB,加载需要14秒.
我已经推断出已完成的查询数量(从75到4),尽管在创建表单的那一刻仍然会出现内存高峰
$form = $this->createForm(new TapsAndAppliancesType(), $taps);
有任何提示和技巧可以加快速度吗?
解决方法:
我假设您在表单中使用类型实体.它们非常繁重,因为首先将所有实体都提取为对象,然后还原为某些id =>标签样式.
因此,您可以编写自己的entityChoice类型,该类型可用于id => label -array(因此在第一个位置没有任何东西作为对象被获取),并向此类型添加DataTransformer:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use MyNamespace\EntityToIdTransformer;
class EntityChoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new EntityToIdTransformer($options['repository']));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'empty_value' => false,
'empty_data' => null,
));
$resolver->setRequired(array(
'repository'
));
}
public function getParent()
{
return 'choice';
}
public function getName()
{
return 'entityChoice';
}
}
作为DataTransformer:
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class EntityToIdTransformer implements DataTransformerInterface
{
private $entityRepository;
public function __construct(EntityRepository $entityRepository)
{
$this->entityRepository = $entityRepository;
}
/**
* @param object|array $entity
* @return int|int[]
*
* @throws TransformationFailedException
*/
public function transform($entity)
{
if ($entity === null) {
return null;
}
elseif (is_array($entity) || $entity instanceof \Doctrine\ORM\PersistentCollection) {
$ids = array();
foreach ($entity as $subEntity) {
$ids[] = $subEntity->getId();
}
return $ids;
}
elseif (is_object($entity)) {
return $entity->getId();
}
throw new TransformationFailedException((is_object($entity)? get_class($entity) : '').'('.gettype($entity).') is not a valid class for EntityToIdTransformer');
}
/**
* @param int|array $id
* @return object|object[]
*
* @throws TransformationFailedException
*/
public function reverseTransform($id)
{
if ($id === null) {
return null;
}
elseif (is_numeric($id)) {
$entity = $this->entityRepository->findOneBy(array('id' => $id));
if ($entity === null) {
throw new TransformationFailedException('A '.$this->entityRepository->getClassName().' with id #'.$id.' does not exist!');
}
return $entity;
}
elseif (is_array($id)) {
if (empty($id)) {
return array();
}
$entities = $this->entityRepository->findBy(array('id' => $id)); // its array('id' => array(...)), resulting in many entities!!
if (count($id) != count($entities)) {
throw new TransformationFailedException('Some '.$this->entityRepository->getClassName().' with ids #'.implode(', ', $id).' do not exist!');
}
return $entities;
}
throw new TransformationFailedException(gettype($id).' is not a valid type for EntityToIdTransformer');
}
}
最后在Service.yml中将FormType注册为新的Type
services:
myNamespace.form.type.entityChoice:
class: MyNamespace\EntityChoiceType
tags:
- { name: form.type, alias: entityChoice }
然后,您可以将其作为表单使用
$formBuilder->add('appliance', 'entityChoice', array(
'label' => 'My Label',
'repository' => $repository,
'choices' => $repository->getLabelsById(),
'multiple' => false,
'required' => false,
'empty_value' => '(none)',
))
使用$repository作为所需存储库的实例,将“ choices”作为id =>的数组;标签
内容总结
以上是互联网集市为您收集整理的PHP-内存管理Symfony表单和原则全部内容,希望文章能够帮你解决PHP-内存管理Symfony表单和原则所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。