小编Cha*_*ase的帖子

即使session.gc_probability设置为0,Symfony也会在Ubuntu 14.04上调用PHP垃圾收集器

由于某些原因标题状态我的Symfony 2.5应用程序调用php垃圾收集器,即使我的所有php.ini文件都有:

session.gc_probability = 0
Run Code Online (Sandbox Code Playgroud)

有谁知道如何防止这种情况发生?

我得到的错误信息:

Notice: SessionHandler::gc(): ps_files_cleanup_dir: opendir(/var/lib/php5)
failed: Permission denied (13) in /<path-to-my-site>/var/cache/dev/classes.php line 432 
Run Code Online (Sandbox Code Playgroud)

来自PHPINFO():

Directive               Local Value   Master Value
session.gc_divisor      1000          1000
session.gc_maxlifetime  86400         86400
session.gc_probability  0             0
Run Code Online (Sandbox Code Playgroud)

我知道我可以将www-data用户权限授予该/var/lib/php5文件夹,或者将其更改为用户已经访问过的session.save_path某个地方,www-data但我想知道为什么在禁用该进程时甚至会调用此进程.

php garbage-collection symfony symfony-2.5

44
推荐指数
2
解决办法
1万
查看次数

使用twig变量动态调用导入的宏子函数

我正在尝试使用变量来调用特定的宏名称.

我有一个正在导入的宏文件

{% import 'form-elements.html.twig' as forms %}
Run Code Online (Sandbox Code Playgroud)

现在在该文件中有所有表单元素宏:text,textarea,select,radio等.

我有一个传入的数组变量,其中包含一个元素:

$elements = array(
    array(
        'type'=>'text,
        'value'=>'some value',
        'atts'=>null,
    ),
    array(
        'type'=>'text,
        'value'=>'some other value',
        'atts'=>null,
    ),
);

{{ elements }}
Run Code Online (Sandbox Code Playgroud)

我想要做的是从宏中生成这些元素.当按名称调用时,它们工作得很好:

{{ forms.text(element.0.name,element.0.value,element.0.atts) }}
Run Code Online (Sandbox Code Playgroud)

但是我想做的是这样的:

{% for element in elements %}
{{ forms[element.type](element.name,element.value,element.atts) }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下所有导致相同的错误:

{{ forms["'"..element.type.."'"](element.name,element.value,element.atts) }}
{{ forms.(element.type)(element.name,element.value,element.atts) }}
{{ forms.{element.type}(element.name,element.value,element.atts) }}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会引发以下错误:

 Fatal error: Uncaught exception 'LogicException' with message 'Attribute "value" does not exist for Node "Twig_Node_Expression_GetAttr".' in Twig\Environment.php on line 541
Run Code Online (Sandbox Code Playgroud)

有关解决方案或更好的架构的任何帮助或建议都将非常有用.

twig

10
推荐指数
1
解决办法
3259
查看次数

如何在symfony 2表单构建器中向选项元素添加自定义属性

我试图使用symfony2表单生成器向选项元素添加自定义属性即时通讯我不确定本机是否可行.如果不是我需要知道如何添加功能.

以下面的形式为例:

class FooForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user','choice',array(
            'choices' => array(
                 'designers'=>'designers',
                 '1'=>'mike',
                 '2'=>'carroll',
                 'developers'=>'developers',
                 '3'=>'chase',
                 '4'=>'brett',
                 '5'=>'jordan',
             )
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在渲染时我需要它看起来像:

<select>
    <option value="" disabled="disabled">designers</option>
    <option value="1">mike</option>
    <option value="2">carroll</option>
    <option value="" disabled="disabled">developers</option>
    <option value="3">chase</option>
    <option value="4">brett</option>
    <option value="5">jordan</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我所期待的将是这样的:

class FooForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user','choice',array(
            'choices' => array(
                 'designers'=>array(
                      'label'=>'designers',
                      'attr'=>arrry('disabled'=>'disabled')
                 ),
                 '1'=>'mike',
                 '2'=>'carroll',
                 'developers'=>array(
                      'label'=>'developers',
                      'attr'=>arrry('disabled'=>'disabled')
                 ),
                 '3'=>'chase',
                 '4'=>'brett',
                 '5'=>'jordan',
             )
        )); …
Run Code Online (Sandbox Code Playgroud)

php forms formbuilder symfony

7
推荐指数
1
解决办法
9235
查看次数

最有效的方法是使用doctrine 2批量INSERT IGNORE

我有一个脚本,需要获取数据库中的条目列表,然后迭代那些在另一个表中创建新条目,如果它们不存在.

目前正在做:

foreach($entries as $entry){
    $newItem = new Item();
    $newItem->setAttribute($entry->getAttribute());
    $entityManager->persist($newItem);
    try{
        $entityManager->flush();
    } catch(\Exception $e){
        if(!strpos($e->getMessage(),'Duplicate')){
            throw $e;
        }
        $entityManager = $this->getDoctrine()->getManager(); 
        //refreshes the entity manager
    }

}
Run Code Online (Sandbox Code Playgroud)

但是这样做是非常耗时的,有1000个条目,脚本有时需要10分钟才能完成.我已经看到其他帖子建议在进行这样的批量处理时每隔20个左右记录一次,如果这20个记录中有一个是重复的,那么整个事务就会死掉,我不知道怎么回去试试看找到有问题的条目以在再次重新提交之前将其排除.

任何有关这方面的帮助将不胜感激.

php mysql symfony doctrine-orm

6
推荐指数
1
解决办法
3552
查看次数

将Composer与私有VCS结合使用时,它会无限期地请求授权

在我的composer.json中,我有一个私有的vcs存储库:

{
    "type": "vcs",
    "url": "https://github.com/username/repo.git"
}
Run Code Online (Sandbox Code Playgroud)

当我打电话时,php composer.phar update我得到以下信息:

Your GitHub credentials are required to fetch private repository metadata (https://github.com/username/repo.git)
The credentials will be swapped for an OAuth token stored in /root/.composer/config.json, your password will not be stored
To revoke access to this token you can visit https://github.com/settings/applications

Username: myusername
Password:
    Authentication required (api.github.com):
      Username: myusername
      Password:
    Authentication required (api.github.com):
      Username: myusername
      Password:
    Authentication required (api.github.com):
      Username:
Run Code Online (Sandbox Code Playgroud)

不管我输入我的github凭据多少次,它都会不断询问。如果我输入了不正确的内容,它将执行相同的操作,没有错误或尝试次数最多。

有什么想法会导致这种情况吗?我正在通过运行以下方式使用最新版本:

php composer.phar self-update
Run Code Online (Sandbox Code Playgroud)

周围的工作

作为一项变通方案,我现在去了:https : //github.com/settings/applications …

php composer-php

5
推荐指数
1
解决办法
2978
查看次数

在自定义字段类型上使用表单事件侦听器无法添加其他字段

我创建了一个自定义字段类型,默认情况下为1字段.选择该字段值后,事件侦听器已订阅该字段并被触发.如果该字段的值与我声明的任意值匹配,则假设将另一个字段添加到表单中.

问题是事件被触发,我可以在事件监听器中添加字段之前和之后调试,但是在返回表单时不会呈现新字段.

这是我试图做的简化版本.是的,我确实已将自定义字段类型正确注册为服务.

主表格类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('first_name','text')
       ->add('custom_field','my_custom_fields');
}
Run Code Online (Sandbox Code Playgroud)

然后是自定义类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('field_1','entity',array(/*My entity settings*/));

    $formModifier = function(FormInterface $form, $campaign) use($options) {
        $form->add('field_2', 'entity',array(/*my entity options*/))
        ->add('Save','submit',array('attr'=>array('class'=>'btn btn-primary')));
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use ($formModifier, $options) {
            // this would be your entity
            $entity = $event->getData();
            //var_dump($entity); exit; // This gets Hit
            if(!$entity || !($entity instanceof My\Entity\Class) ){return;}
                $formModifier($event->getForm(), $entity);
            }
        }
    );

    $builder->get('field_1')->addEventListener(
        FormEvents::POST_SUBMIT,
        function(FormEvent $event) use ($formModifier, $options) …
Run Code Online (Sandbox Code Playgroud)

symfony-forms symfony symfony-2.3

5
推荐指数
1
解决办法
1174
查看次数

Twig 检查值是否在数组中

我无法使用 Twig 检查数组中是否存在值。如果购物车中有某种产品,我想在结账时隐藏运输方式。 我只能使用Twig代码,所以我必须在其中找到逻辑。

假设当产品 ID 1234 在购物车中时我想隐藏#certain_div

所以我所拥有的是这个->

  {% if checkout %}

      {% set array = theme.sku_shipping_rule | split(',') %}
     // theme.sku_shipping_rule = a text string like 1234, 4321, 5478         

        {% if checkout.products %}
         {% for product in checkout.products %}
          {% if product.sku in array %}

           <style>
             #certain_div {
                display: none;
              }
           </style>

          {% endif %}
         {% endfor %}
       {% endif %}

    {% endif %}
Run Code Online (Sandbox Code Playgroud)

我面临的问题是我的代码似乎总是返回 true。因此,即使 与product.sku数组中的值不匹配,它仍然会隐藏#certain_div。我已经通过放置{{ product.sku }}在之前进行了测试 …

arrays saas symfony twig

5
推荐指数
1
解决办法
1万
查看次数

Symfony 2.3如何在一个表单组中将按钮组合在一起?

现在我有一个包含以下内容的FormType:

$builder->add('name','text')
    ->add('save','submit',array('label'=>'Save', 'attr'=>array('class'=>'btn btn-primary')))
    ->add('reset','reset',array('label'=>'Reset Form', 'attr'=>array('class'=>'btn btn-warning')));
Run Code Online (Sandbox Code Playgroud)

现在我有一些形式主题,将上述内容呈现为:

<form method="post" action="">
    <input type="hidden" name="_csrf_token" value="*********" />
    <div class="form-group">
        <label class="col-4">Name</label>
        <input type="text" name="form[name]" value="" placeholder="Name" />
    </div>
    <div class="form-group">
        <input type="submit" name="form[save]" value="Save" class="btn btn-primary" />
    </div>
    <div class="form-group">
        <input type="reset" name="form[reset]" value="Reset" class="btn btn-warning" />
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

但是我想要的输出是:

<form method="post" action="">
    <input type="hidden" name="_csrf_token" value="*********" />
    <div class="form-group">
        <label class="col-4">Name</label>
        <input type="text" name="form[name]" value="" placeholder="Name" />
    </div>
    <div class="form-group">
        <input type="submit" name="form[save]" value="Save" class="btn btn-primary" />
        <input …
Run Code Online (Sandbox Code Playgroud)

php forms symfony twig symfony-2.3

4
推荐指数
1
解决办法
3223
查看次数

如何更改树枝模板在路由生成中使用的主机?

好的,所以我有一个带有动作的控制器和与之相关的2条路线:

/**
 * @Route("/index/preview/", name="mybundle.preview_index")
 * @Route("/", name="mybundle.index")
 * @Template
 */
public function indexAction(Request $request)
{
    $preview = ($request->get('_route') === 'mybundle.preview_index');
    $host = $request->getHttpHost(); //domain.com
    if(!$preivew){
        $host = 'domain2.com';
    }
    return array(
        'preivew' => $preview,
        'host' => $host,
        'basePath' => $preview?'mybundle.preview_':'mybundle.',
    );
}
Run Code Online (Sandbox Code Playgroud)

然后我想根据主机在树枝模板内生成一条路由:

{{ path(basePath~'index') }}
//Then somehow pass the host to this so that i get the intended domain
Run Code Online (Sandbox Code Playgroud)

如果我使用预览路由访问此路由,则将得到:

domain.com/index/preview/
Run Code Online (Sandbox Code Playgroud)

但是,如果我不这样做,它将给我:

domain2.com/
Run Code Online (Sandbox Code Playgroud)

我尝试过的

  • 从控制器内部设置路由器上下文,但这不会更改树枝中生成的路由

symfony twig symfony-routing

3
推荐指数
1
解决办法
1509
查看次数

Twig调试(我没有config.yml)

很久以后我又回到了PHP游戏中.我正在看Twig,需要了解更多正在发生的事情.我发现了一些需要进入config.yml文件的文本.警告:它不在我的系统上.它是否来自Twig版本或我是否也必须安装Symfony?有点迷失在这里.

干杯.

编辑:我只需要{{dump(var)}}来工作.httpd错误日志告诉我:PHP致命错误:未捕获异常'Twig_Error_Syntax',消息'the function"dump"不存在于

我正在设置我的Twig环境:

$twig = new Twig_Environment( $loader, array(
    'cache' => '/tmp',
    'debug' => true
));
Run Code Online (Sandbox Code Playgroud)

php debugging twig

3
推荐指数
1
解决办法
1778
查看次数

Symfony2 - 如何设置控制器createForm函数使用的实体管理器?

我有一个表单类型,它使用带有实体字段的查询构建器来获取相关选项.但是因为我正在为实体使用自定义实体管理器,所以它似乎无法识别这些选项.我得到错误:

Entities passed to the choice field must be managed. Maybe persist them in the entity manager? 
Run Code Online (Sandbox Code Playgroud)

控制器动作:

/**
 * @Route("/edit/{keyword_rank_id}/", name="lg.keywordrank.campaign.edit")
 * @Template
 */
public function editAction(Request $request, Company $company, $client_slug, $keyword_rank_id)
{
    $em = $this->getDoctrine()->getManager($company->getEntityManagerName());
    $client = $this->getEntityOrNotFound($em, 'LGClientBundle:Client', 'client_slug', $client_slug);
    $kr = $this->getEntityOrNotFound($em, 'LGKeywordRankBundle:KeywordRank', 'keyword_rank_id', $keyword_rank_id);
    $form = $this->createForm(new KeywordRankForm(), $kr, array('client'=>$client,'em'=>$em));
    ...
}
Run Code Online (Sandbox Code Playgroud)

表格类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name','text',array(
        'label'=>'Campaign Name'
    ))
    ->add('client_domain', 'entity', array(
        'class' => 'LGClientBundle:ClientDomain',
        'choices'=> $this->getClientDomains($options['em'], $options['client']),
        'property' => …
Run Code Online (Sandbox Code Playgroud)

php mysql symfony

2
推荐指数
1
解决办法
4698
查看次数

如何使用Symfony Forms将事件侦听器添加到动态添加的字段

我正在使用事件侦听器来动态修改表单.我想为动态添加的字段添加另一个事件监听器.我不知道如何实现这一目标.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('first_field','choice',array(
        'choices'=>array('1'=>'First Choice','2'=>'Second Choice')
    ));

    $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'preSetData'));
    $builder->get('first_field')->addEventListener(FormEvents::POST_SUBMIT, array($this, 'postSubmit'));
}

public function preSetData(FormEvent $event)
{
    $form = $event->getForm();
    $form->add('second_field','choice',array(
        'choices'=>array('1'=>'First Choice','2'=>'Second Choice')
    ));
    //Some how add an event listener to this field

}

public function postSubmit(FormEvent $event)
{
    $form = $event->getForm()->getParent();
    $form->add('second_field','choice',array(
        'choices'=>array('1'=>'First Choice','2'=>'Second Choice')
    ));
    //Some how add an event listener to this field
}
Run Code Online (Sandbox Code Playgroud)

我只是使用函数$builder中的buildForm函数来添加事件监听器,second_field但因为在最初生成表单时该字段不存在会抛出错误.

如果我尝试通过执行以下操作在第一个事件侦听器中添加新事件侦听器:

$form->get('second_field')->addEventListener(...)
Run Code Online (Sandbox Code Playgroud)

然后我得到错误:

Call to undefined method …
Run Code Online (Sandbox Code Playgroud)

php symfony-forms symfony symfony-2.3

2
推荐指数
2
解决办法
7230
查看次数

如何使用 Doctrine 2 提高 LARGE 导入的性能

我有大约 100 万条记录需要导入。我一直在网上寻找改进和加速这一过程的方法。目前,我的应用程序连接到 1 个数据库,对大约 220 万行的表进行选择,此选择持续大约需要 10-13 秒。我使用此查询选择 10,000 行。

$results = $em->getRepository('...')->createQueryBuilder('x')
              ->where('...')
              ->setFirstResult($index)
              ->setMaxResults($maxResults)
              ->getQuery()
              ->getResult();
Run Code Online (Sandbox Code Playgroud)

然后,我继续迭代每一行,并在另一个数据库中进行 2 次查找,使用这些实体创建一个新实体,并使用事务一次插入所有 10,000 个新实体。

$secondEm->transactional(function($em){
    foreach($results as $result){
        $value1 = $em->getRepository('A')->findOneBy(array('value'=>$result->getValue()));
        $value2 = $em->getRepository('B')->findOneBy(array('value'->$result->getValue()));
        $newEntity = new Entity();
        $newEntity->setValue1($value1)->setValue2($value2);
        $em->persist($newEntity);
    }
    $em->flush();
});
$secondEm->clear();
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是每次后续插入的导入时间都会逐渐变长。前 10,000 个大约需要 60 秒,第二个需要 100 秒,然后从那里开始,每次新插入似乎都会增加大约 5-10 秒。

我读过,对于进行大型插入的 innodb 表,您应该禁用foreign_key_checks 和 unique_checks,但我不知道如何为原则事务插入做到这一点。

任何有关如何禁用这些检查或什至更好的方法来执行此导入的建议将不胜感激。

地位

选择查询现在似乎也在时间上增加。最后一个查询:

$maxResults = 10000;
$index = 470000;
Run Code Online (Sandbox Code Playgroud)

选择花了 97 秒,导入花了 173 秒。

重要 这个过程每个请求发生一次,我有一个 javascript 操作,它将自动提交空白表单,这一切都发生在 POST 上,我意识到这可能会更好地从命令运行,因为它都在同一服务器上,但在那里还有其他方法可以优化这个吗?

有趣的

现在已经插入了大约 …

php mysql innodb symfony doctrine-orm

1
推荐指数
1
解决办法
3477
查看次数