小编gre*_*emo的帖子

从jQuery选择中删除没有id的元素?

如何id从调用插件的集合(而不是从DOM本身)中删除没有属性的元素?

<span id="id737293" class="attach"></span>
<div class="attach"></span>
Run Code Online (Sandbox Code Playgroud)

jQuery调用和插件:

$('.attach').attach();

(function($) {

    $.fn.attach = function(options) {
       // Remove elements (without id) on which the plugin was invoked
       var valid = ???;

       // Loop each valid element (with id attribute) and process
       valid.each(function() {
       });

       return this; // Maintain chainability
    };

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

jquery jquery-plugins jquery-selectors

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

避免UNION在MySQL中使用两个几乎相同的表

我不是很擅长MySQL,我打算写一个查询来计算用户发送的消息,根据它的类型和is_auto字段.

消息可以是"小文本消息"或"简报"类型.我创建了两个实体,它们之间有一些不同的字段.重要的是messages_count表中没有newsletter,它在查询中使用:

CREATE TABLE IF NOT EXISTS `small_text_message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `messages_count` int(11) NOT NULL,
  `username` varchar(255) NOT NULL,
  `method` varchar(255) NOT NULL,
  `content` longtext,
  `sent_at` datetime DEFAULT NULL,
  `status` varchar(255) NOT NULL,
  `recipients_count` int(11) NOT NULL,
  `customers_count` int(11) NOT NULL,
  `sheduled_at` datetime DEFAULT NULL,
  `sheduled_for` datetime DEFAULT NULL,
  `is_auto` tinyint(1) NOT NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
Run Code Online (Sandbox Code Playgroud)

和:

CREATE TABLE `newsletter` (
  `id` …
Run Code Online (Sandbox Code Playgroud)

mysql sql optimization

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

是否可以在Symfony 2中保护整个控制器?

我正在使用JMSSecurityExtra包来保护我的控制器中的方法.但有什么方法可以保护整个控制器@Secure

php jms symfony

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

EXPLAIN SELECT显示MySQL没有使用我的索引

表定义,注意UNIQUE索引:

CREATE TABLE meta
(
  id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  type SET('tag', 'keyword') NOT NULL,
  name VARCHAR(255) NOT NULL,
  user_id INT UNSIGNED NOT NULL,
  UNIQUE (name, type, user_id),
  FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
Run Code Online (Sandbox Code Playgroud)

所以MySQL应该WHERE name = 'tag' and type = 'cat'只在搜索中使用索引(仅限最左边的前缀)WHERE type = 'tag'.

我做了:

EXPLAIN SELECT * FROM meta WHERE type = 'tag'
Run Code Online (Sandbox Code Playgroud)

结果是(第五栏possible_keys):

'1', 'SIMPLE', 'meta', 'ALL', NULL, NULL, NULL, NULL, '1', 'Using where' …
Run Code Online (Sandbox Code Playgroud)

mysql indexing database-indexes

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

表单事件监听器(如TrimListener)如何"绑定"到Symfony中的表单选项?

使用Symfony中的文本字段类型,有一个修剪选项.我很确定该trim()操作是由Form\Extension\Core\EventListener\TrimListener班级进行的.它是该PRE_BIND事件的监听者并致电:

$event->setData(trim($event->getData()));
Run Code Online (Sandbox Code Playgroud)

我想提供自己的"normalize_spaces"选择:

$builder->add('last_name', 'text', array(
    'label'           => 'Last name',
    'normlize_spaces' => true
));
Run Code Online (Sandbox Code Playgroud)

我怎样才能提供这个选项NormalizeSpacesListener

class NormalizeSpacesListener implements EventSubscriberInterface
{
    public function preBind(FormEvent $event)
    {
        $data = $event->getData();

        if (is_string($data)) {
            $event->setData(preg_replace('/[ ]{2,}/', ' ', $data));
        }
    }

    public static function getSubscribedEvents()
    {
        return array(FormEvents::PRE_BIND => 'preBind');
    }
}
Run Code Online (Sandbox Code Playgroud)

symfony-forms symfony

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

如何在运行时生成或修改PHP类?

schmittjoh/CG-库看来我需要什么,但没有文件可言.

该库提供了生成PHP代码通常需要的一些工具.其中一个优势在于通过行为增强现有类.

鉴于A课程:

class A {}
Run Code Online (Sandbox Code Playgroud)

我想在运行时和一些缓存机制上修改类A,使其实现给定的接口:

interface I
{
    public function mustImplement();
}
Run Code Online (Sandbox Code Playgroud)

......与方法"默认"的实现mustImplement()A类.

php runtime php-5.3

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

定义一个函数并立即在Coffee中调用它,无法获得所需的结果

无法让我的Coffee脚本完全编译:

( function (root) { return 'Hello Coffee'; }(this) );
Run Code Online (Sandbox Code Playgroud)

第一次尝试:

do (root) ->
    'Hello Coffee'
Run Code Online (Sandbox Code Playgroud)

...没有生成与上面相同的代码,输出(with --bare):

(function(root) {
  return 'Hello Coffee';
})(root);
Run Code Online (Sandbox Code Playgroud)

coffeescript

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

为什么原型函数的执行上下文("this")在这个例子中是错误的?

原型函数bar在Node.js环境中bind应该在别处执行(应该可用).我希望this内部bar()函数成为我的对象的实例:

var Foo = function (arg) {
    this.arg = arg;

    Foo.prototype.bar.bind(this);
};

Foo.prototype.bar = function () {
    console.log(this); // Not my object!
    console.log(this.arg); // ... thus this is undefined
}

var foo = new Foo();
module.execute('action', foo.bar); // foo.bar is the callback 
Run Code Online (Sandbox Code Playgroud)

...为什么bar()日志undefinedthis不是我的实例?为什么bind调用不会改变执行上下文?

javascript this anonymous-function node.js

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

为什么async.map函数与本机fs.stat函数一起使用?

async.map(['file1','file2','file3'], fs.stat, function(err, results){
    // results is now an array of stats for each file
});
Run Code Online (Sandbox Code Playgroud)

根据文档,第二个参数是:

iterator(item,callback) - 一个应用于数组中每个项的函数.

精细.

迭代器传递一个回调(错误,转换),一旦完成错误(可以为null)和转换项,就必须调用它.

我认为这fs.stat不符合这一点,我会说这不应该奏效.

它应该是这样的:

async.map(['file1','file2','file3'],
    function (file, complete) {
        fs.stat(file, function (err, stat) {
            complete(err, stat)
        });
    }, function(err, results){
        // results is now an array of stats for each file
    }
);
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js node-async

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

Symfony 2与验证组一起形成,错误映射到错误的属性?

从来没有遇到过这个问题.

  • 用手机填写表格,将姓氏留空
  • 提交表单(验证组成为DefaultCreate)
  • 错误"姓氏是必需的".映射在错误的$phone字段上,而应该是mappend到$lastName自己的属性

错误

你能重现同样的问题吗?

$phone属性在Create验证组中,而Default隐藏组中的$ phone :

class User
{
    /**
     * @Assert\NotBlank(groups={"Create"}, message="Last name is required.")
     *
     * @var string
     */
    protected $lastName;

    /**
     * @Assert\NotBlank(message="Phone is required.")
     *
     * @var string
     */
    protected $phone;
}
Run Code Online (Sandbox Code Playgroud)

我根据提交的数据确定验证组:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('lastName', 'text');
        $builder->add('phone', 'text');
        $builder->add('submit', 'submit');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'required' => false, …
Run Code Online (Sandbox Code Playgroud)

symfony-forms symfony symfony-2.4

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