Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add collectionName #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

reallovelei
Copy link

因为使用了注解@task
所以会在启动的时候 在runtime里生成runtime/container/proxy/Phper666_MongoDb_MongoDb.proxy.php

public function findAll(array $filter = [], array $options = [], array $collectionOptions = [])
    {
        $__function__ = __FUNCTION__;
        $__method__ = __METHOD__;
        var_dump(1, $this->collectionName);
        return self::__proxyCall(__CLASS__, __FUNCTION__, self::__getParamsMap(__CLASS__, __FUNCTION__, func_get_args()), function (array $filter = [], array $options = [], array $collectionOptions = []) use($__function__, $__method__) {
            try {
                var_dump(2, $this->collectionName);
                /**
                 * @var $collection MongoDBConnection
                 */
                $collection = $this->getConnection();
                return $collection->findAll($this->collectionName, $filter, $options, $collectionOptions);
            } catch (\Exception $e) {
                throw new MongoDBException($this->handleErrorMsg($e));
            }
        });
    }

__proxyCall 上面的部分 第一次执行是有值的。但是在__proxyCall再次调用本身的时候 $this->collectionName就没有值了。
这种情况只有在config/autoload/crontab.php 里调用的时候 才会出现。
如果直接在命令行执行,是直接调用的vendor里的MongoDb.php 这时候就不会发生。我也比较纳闷。

线上问题 希望可以合并,这样我就不用自己打包发布了。

@phper666
Copy link
Owner

@reallovelei 这个问题一直存在的,你可以看我的文档我是怎么解决的。为什么会出现这个问题,是因为@task注解使用的是代理方式,代理方式无法获取到继承类的属性,也就是无法获取到子类的collectionName,所以会默认拿父类(MongoDb.php)的collectionName,父类的conllectionName是null,所以会出现这个问题。你这个方法也可以,不过要给每个方法上都添加这个参数.你fork一份出来,自己发布,目前php的项目我基本不动了,等我缓过一段时间,再重新梳理一下

@reallovelei
Copy link
Author

reallovelei commented Feb 22, 2021 via email

@phper666
Copy link
Owner

phper666 commented Feb 22, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants