我正在处理Yii2在添加TimestampBehavior以从主配置运行时遇到的问题.原因是我必须在大多数模型的前端和后端使用它.
要在模型中使用它很简单:
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => function(){ return date('Y-m-d H:i:s'); } ,
],
];
}
Run Code Online (Sandbox Code Playgroud)
但如果我试图在main.php中添加行为,则没有任何反应.我在考虑:
'as timestamp'=>[
'class'=> \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => function(){ return date('Y-m-d H:i:s'); } ,
],
Run Code Online (Sandbox Code Playgroud)
但它不起作用.有任何想法吗?
所以,我们有代码:
class Foo
def bar
puts "Before existent: #{(defined? some_variable)}"
puts "Before not_existent: #{(defined? nonexistent_variable)}"
raise "error"
some_variable = 42
rescue
puts "exception"
ensure
puts "Ensure existent: #{(defined? some_variable)}"
puts "Ensure not_existent: #{(defined? nonexistent_variable)}"
end
end
Run Code Online (Sandbox Code Playgroud)
并从irb调用它:
> Foo.new.bar
Run Code Online (Sandbox Code Playgroud)
而且,这将返回:
Before existent:
Before not_existent:
exception
Ensure existent: local-variable
Ensure not_existent:
=> nil
Run Code Online (Sandbox Code Playgroud)
现在问题 - 为什么?我们之前提出异常而不是some_variable定义.为什么这样工作?为什么some_variable在ensure块中定义?(顺便说一下,它定义为零)
更新: 感谢@Max的回答,但如果我们更改代码以使用实例变量:
class Foo
def bar
puts "Before existent: #{(defined? @some_variable)}"
puts "Before not_existent: #{(defined? @nonexistent_variable)}"
raise …Run Code Online (Sandbox Code Playgroud) 有时对象由纯数据组成.这些对象具有字段,访问器,实际上没有其他方法.
有时对象由纯粹的行为组成.它们具有表示其状态的其他对象,或者数据作为方法参数传递.通常这些对象代表算法或某种策略.
您更喜欢哪种州/行为比例?
什么更可维护?
什么更容易出错?
我正在使用erlang作为服务之间的桥梁,我想知道人们有什么建议来处理被击落的连接?
我从本地文件中获取输入并将它们输出到AMQP,可以想象AMQP经纪人可能会崩溃.对于这种情况,我想继续重试连接到AMQP服务器,但我不想将CPU与这些连接尝试挂钩.我倾向于在重新启动AMQP代码时进行睡眠.难道"黑客"本能绕过快速失败并让erlang处理它的目的吗?更一般地说,是否应该使用erlang管理员行为来处理故障连接?
我刚刚从eclipse切换到Notepad ++,我修改了记事本的一些行为,但仍然有一个未解决:当我双击变量时,我希望我的编辑器能够捕获整个变量.记事本自动排除php前缀"$",这是无效的.有没有办法可以改变行为?
谢谢
我正在使用附加的行为向我的代码添加拖放功能.到目前为止,一切工作正常,但我的问题是当我想测试我的行为类.
例如,其中一个行为类将类似于以下内容:
public class DroppableContainerBehavior: Behavior<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.AllowDrop = true;
AssociatedObject.Drop += new DragEventHandler(AssociatedObject_Drop);
AssociatedObject.DragOver += new DragEventHandler(AssociatedObject_DragOver);
AssociatedObject.DragLeave += new DragEventHandler(AssociatedObject_DragLeave);
}
private void AssociatedObject_Drop(object sender, DragEventArgs e)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是当我想为AssociatedObject_Drop方法创建一个单元测试时,我需要创建一个DragEventArgs对象,但这个类是密封的.
我的印象是我做错了什么..我的问题是,我应该测试我的行为类吗?行为与UI有关,通常不值得测试UI.我对吗?也许我必须改变我的行为代码,使其更可测试?有任何想法吗?
谢谢你的帮助!
我正在开发的项目使用了Plone令人敬畏的Dexterity插件.我的一些自定义内容类型具有必须计算的非常具体的名称.我之前完成此操作的方法是将plone.app.content.interfaces.INameFromTitle添加为对象的通用设置条目中的行为,按照手册的说明:
<?xml version="1.0"?>
<object name="avrc.aeh.cycle" meta_type="Dexterity FTI">
...
<property name="schema">myproject.mytype.IMyType</property>
<property name="klass">plone.dexterity.content.Item</property>
...
<property name="behaviors">
<element value="plone.app.content.interfaces.INameFromTitle" />
</property>
...
</object>
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个提供INameFromTitle的适配器:
from five import grok
from zope.interface import Interface
import zope.schema
from plone.app.content.interfaces import INameFromTitle
class IMyType(Interface):
foo = zope.schema.TextLine(
title=u'Foo'
)
class NameForMyType(grok.Adapter):
grok.context(IMyType)
grok.provides(INameFromTitle)
@property
def title(self):
return u'Custom Title %s' % self.context.foo
Run Code Online (Sandbox Code Playgroud)
此方法与此博客文章中建议的方法非常相似:
http://davidjb.com/blog/2010/04/plone-and-dexterity-working-with-computed-fields
不幸的是,这个方法在plone.app.dexterity beta之后停止工作,现在我的内容项没有正确分配它们的名字.
有人会碰巧知道如何为非常具体的命名用例扩展Dexterity的INameFromTitle行为吗?
非常感谢您的帮助,谢谢!
如果我print在这段代码中使用:
<?php
for($i = 1; $i <= 3; print $i . "\n") {
$i++;
}
?>
Run Code Online (Sandbox Code Playgroud)
我认为这是输出:
2
3
4
但是当我使用echo代码时不起作用:
<?php
for($i = 1; $i <= 3; echo $i . "\n") {
$i++;
}
?>
Run Code Online (Sandbox Code Playgroud)
我看到这个错误:
PHP解析错误:第3行的/media/datos/xampp/htdocs/temp/1.php中的语法错误,意外的'echo'(T_ECHO),期待')'
我的问题是:
print在for循环中使用第三个表达式,但在使用时不能使用echo,为什么它们的行为彼此不同?参考文献:
我正在使用afterFind函数来修改find函数中的数据.它工作正常.如果我将afterFind函数移动到一个行为(在插件中)它仍然有效,但仅当感兴趣的模型是主要模型时,即当模型属于另一个模型时不调用它.这有什么办法吗?我正在使用蛋糕1.3.4.这是行为的简化版本:
class ChemicalStructureBehavior extends ModelBehavior {
function afterFind(&$model, $results, $primary) {
foreach ($results as &$unit) {
// format chemical formula (with subscripts)
$unit[$model->alias]['chemical_formula_formatted'] = preg_replace('/([0-9]+)/i', '<sub>$1</sub>', $unit[$model->alias]['chemical_formula']);
}
return $results;
}
}
Run Code Online (Sandbox Code Playgroud) 您知道当您将鼠标悬停在链接上时,在大多数浏览器中,它会显示左下角(也称为chrome)或状态栏中的链接?我怎么能禁用它?