小编gre*_*emo的帖子

在Symfony2中生成追加查询字符串的路径

是否有任何工具可以为给定的路由和参数生成路径,自动附加查询字符串?作为临时解决方法,我正在使用自制宏:

{% macro path(route, args, with_query) %}
{% spaceless %}
    {% set with_query = with_query|default(false) and app.request.queryString %}
    {{ path(route, args) ~ (with_query ? '?' ~ app.request.queryString : '' ) }}
{% endspaceless %}
{% endmacro %}
Run Code Online (Sandbox Code Playgroud)

在Symfony2/Twig中是否有一些本机功能可以做到这一点?

symfony twig

28
推荐指数
2
解决办法
3万
查看次数

关于如何在PHPUnit中使用setUp()和tearDown()的真实单词示例?

方法setUp()tearDown()在每次测试之前和之后调用.但是,真的,有什么真正的单词示例关于我为什么需要这个?

检查其他人的测试,我总是看到类似的东西:

public function setUp()
{
    $this->testsub = new TestSubject();
}

public function tearDown()
{
    unset($this->testsub);
}

public function testSomething()
{
    $this->assertSame('foo', $this->testsub->getFoo());
}
Run Code Online (Sandbox Code Playgroud)

当然,这种方式与"旧"局部变量方式之间几乎没有区别.

php testing phpunit unit-testing

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

获得本月的最后一天?

可能重复:
PHP的最后一天

有没有想任何功能$date->getMonthDays()$date->getLastDayOfMonth()在PHP中获得的天在给定月份数(或最后一天数)?

$start = new DateTime('2012-02-01');
$end   = clone $start;

// Interval = last day of the month minus current day in $start
$interval = $start->getLastDayOfMonth() - intval($start->format('j'));
$end->add(new DateInterval('P' . $interval . 'D'));
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢,投票结束,这是重复,抱歉...

php datetime

24
推荐指数
3
解决办法
7万
查看次数

PHP Phar在功能强大的PC上创建速度慢,如何加速(加载/读取~3000个文件)?

我正在尝试使用Phar打包我的Web应用程序(Symfony 2项目).我成功地在一个合理的时间内(1-2分钟)打包了一个带有数百个文件的微框架Silex.

问题在于我的开发机器(i7 4770k,16GB,SSD Raid 0,RAM磁盘上的项目)创建存档非常慢,每个文件需要大约1秒.我真的需要找到一种方法来加快速度.

单次迭代读取/加载文件很慢.我正在使用以下方法添加文件:

function addFile(Phar $phar, SplFileInfo $file)
{
    $root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
    $path = strtr(str_replace($root, '', $file->getRealPath()), '\\', '/');
    $phar->addFromString($path, file_get_contents($file));
}

$phar = new Phar(/* ... */);
$phar->startBuffering();

// ...    
foreach ($files as $file) {
    addFile($phar, $file);
}

// ...
$phar->setStub(/* ... */);
$phar->stopBuffering();
Run Code Online (Sandbox Code Playgroud)

如何加快阅读/添加文件的速度?可能是我的操作系统(Windows)的问题?

编辑:禁用缓冲并没有解决问题.从字符串添加相同的速度:

// This is VERY fast (~ 1 sec to read all 3000+ files)
$strings = array();
foreach ($files as $file) {
    $root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR);
    $path = …
Run Code Online (Sandbox Code Playgroud)

php performance phar symfony

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

允许Symfony 2 Bundle语义配置中的键值对

基本上我想在我的配置中允许任意(但不是空)数量的键值对,在billings节点下,定义一个关联数组.

我在我的Configurator.php(部分)中有这个:

->arrayNode('billings')
    ->isRequired()
    ->requiresAtLeastOneElement()
    ->prototype('scalar')
    ->end()
->end()
Run Code Online (Sandbox Code Playgroud)

然后,在我的config.yml:

my_bundle:
    billings:
        monthly : Monthly
        bimonthly : Bimonthly
Run Code Online (Sandbox Code Playgroud)

但是,输出$config:

class MyBundleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container,
            new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        $processor     = new Processor();
        $configuration = new Configuration();

        $config = $processor->processConfiguration($configuration, $configs);

        $container->setParameter('my_bundle.billings', $config['billings']);

        var_dump($config);
        die();
    }

}
Run Code Online (Sandbox Code Playgroud)

...我得到的是数字索引,而不是关联索引:

 'billings' => …
Run Code Online (Sandbox Code Playgroud)

bundle dependency-injection symfony

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

如何将完整的URL添加到Symfony2发送的简报中?

我将使用Symfony2定期向许多用户发送简报.我要为那些在使用电子邮件客户端阅读时遇到问题的人提供HTML电子邮件的固定链接.

无论如何,假设我以这种方式发送简报:

// Assume success and create a new SentMessage to store and get permalink
$sent = new SentMessage();

$sent->setRecipients(/* ... */);
$sent->setSubject(/* ... */);
$sent->setContent(/* ... */);

// Get the slug for the new sent message
$slug = $sent->getSlug(); // Like latest-product-offers-546343

// Construct the full URL
// e.g. http://mydomain.com/newsletter/view/latest-product-offers-546343

// Actually send a new email
$mailer->send(/* .. */);
Run Code Online (Sandbox Code Playgroud)

如何构建完整的URL(域+控制器+动作+ slug)以将其包含在新电子邮件中?

url permalinks swiftmailer symfony

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

在PHPUnit中实现给定接口的模拟对象上的未定义方法?

我是单元测试和PHPUnit的新手.

我需要一个模拟,我有一个完全控制,实现ConfigurationInterface接口.测试主题是ReportEventParamConverter对象,测试必须检查我的对象和界面之间的交互.

ReportEventParamConverter 对象(这里简化):

class ReportEventParamConverter implements ParamConverterInterface
{
    /**
     * @param Request $request
     * @param ConfigurationInterface $configuration
     */
    function apply(Request $request, ConfigurationInterface $configuration)
    {
        $request->attributes->set($configuration->getName(), $reportEvent);
    }

    /**
     * @param ConfigurationInterface $configuration
     * @return bool
     */
    function supports(ConfigurationInterface $configuration)
    {
        return 'My\Namespaced\Class' === $configuration->getClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我试图模仿界面的方式:

$cls = 'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface';
$mock = $this->getMock($mockCls);
Run Code Online (Sandbox Code Playgroud)

我需要模拟两种方法的返回值:getClass()getName().例如:

$mock->expects($this->any())
    ->method('getClass')
    ->will($this->returnValue('Some\Other\Class'))
;
Run Code Online (Sandbox Code Playgroud)

当我创建一个新的ReportEventParamConverter测试supports()方法时,我得到以下PHPUnit错误:

致命错误:调用未定义的方法Mock_ConfigurationInterface_21e9dccf :: getClass().

$converter = new ReportEventParamConverter();
$this->assertFalse($converter->supports($mock));
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mocking symfony

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

使用注释在Doctrine 2中添加FULLTEXT索引?

我知道Doctrine 2不支持FULLTEXT索引.我实际上使用结果集映射和本地查询到FULLTEXT搜索innodb表(MySQL 5.6).但我仍然需要将一个或多个实体字段标记为索引的一部分.

有没有办法使用注释添加索引?似乎@Index注释没有指定...的类型

mysql full-text-search symfony doctrine-orm

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

如何为此ActionLink创建正确的路径值?

模型SearchResults.aspx是一个实例PersonSearch; 当新页面的请求到达时(GET请求),操作方法应该接受它并计算新结果.

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult SearchResults(PersonSearch search, int? page)
{
    ViewData["Results"] = new PaginatedList<Person>(_searchService.FindPersons(search), page ?? 0, 1);
    return View("SearchResults", search);
}
Run Code Online (Sandbox Code Playgroud)

然后我必须生成上一个/下一个链接:

<%= Html.ActionLink("Next Page >", "SearchResults", routeValues) %>
Run Code Online (Sandbox Code Playgroud)

如果我使用routeValues = ViewData.Model我可以看到传递地址的对象属性,但我无法添加"page"参数.

c# asp.net-mvc actionlink routevalues

18
推荐指数
2
解决办法
8万
查看次数

为什么需要在ORM中分离和合并实体?

问题是关于Doctirne,但我认为可以扩展到许多ORM.

分离:

实体与EntityManager分离,因此不再通过调用其EntityManager#detach($entity)上的方法或通过将分离操作级联到它来进行管理.对分离的实体所做的更改(如果有的话)(包括删除实体)将在分离实体后不会同步到数据库.

合并:

合并实体是指将(通常是分离的)实体合并到EntityManager的上下文中,以便它们再次被管理.要将实体的状态合并到EntityManager中,请使用该 EntityManager#merge($entity)方法.传递的实体的状态将合并到此实体的托管副本中,随后将返回此副本.

我(几乎)理解这是如何工作的,但问题是:为什么需要分离/合并来源?当这两个操作可以使用/需要时,你能给我一个例子/场景吗?

orm doctrine doctrine-orm

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