是否有任何工具可以为给定的路由和参数生成路径,自动附加查询字符串?作为临时解决方法,我正在使用自制宏:
{% 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中是否有一些本机功能可以做到这一点?
方法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的最后一天
有没有想任何功能$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)
编辑:谢谢,投票结束,这是重复,抱歉...
我正在尝试使用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) 基本上我想在我的配置中允许任意(但不是空)数量的键值对,在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) 我将使用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)以将其包含在新电子邮件中?
我是单元测试和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) 我知道Doctrine 2不支持FULLTEXT索引.我实际上使用结果集映射和本地查询到FULLTEXT搜索innodb表(MySQL 5.6).但我仍然需要将一个或多个实体字段标记为索引的一部分.
有没有办法使用注释添加索引?似乎@Index注释没有指定...的类型
模型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"参数.
问题是关于Doctirne,但我认为可以扩展到许多ORM.
分离:
实体与EntityManager分离,因此不再通过调用其
EntityManager#detach($entity)上的方法或通过将分离操作级联到它来进行管理.对分离的实体所做的更改(如果有的话)(包括删除实体)将在分离实体后不会同步到数据库.
合并:
合并实体是指将(通常是分离的)实体合并到EntityManager的上下文中,以便它们再次被管理.要将实体的状态合并到EntityManager中,请使用该
EntityManager#merge($entity)方法.传递的实体的状态将合并到此实体的托管副本中,随后将返回此副本.
我(几乎)理解这是如何工作的,但问题是:为什么需要分离/合并来源?当这两个操作可以使用/需要时,你能给我一个例子/场景吗?
symfony ×6
php ×4
doctrine-orm ×2
phpunit ×2
unit-testing ×2
actionlink ×1
asp.net-mvc ×1
bundle ×1
c# ×1
datetime ×1
doctrine ×1
mocking ×1
mysql ×1
orm ×1
performance ×1
permalinks ×1
phar ×1
routevalues ×1
swiftmailer ×1
testing ×1
twig ×1
url ×1