Implement DbMessageSource #13

Closed
qiangxue opened this Issue Mar 25, 2013 · 0 comments

Projects

None yet

2 participants

@qiangxue
Member

Similar as 1.1.
Please also refer to the implementation of DbCache and DbSession for API design of the class.

@resurtm resurtm was assigned May 6, 2013
@qiangxue qiangxue closed this in #512 Jun 7, 2013
@tonydspaniard tonydspaniard added a commit that referenced this issue Dec 28, 2013
@tonydspaniard tonydspaniard Merge remote-tracking branch 'upstream/master' into 364-toAscii
* upstream/master: (91 commits)
  fixed init.bat paths
  renamed backstage → backend
  Fixed test break.
  Response WIP
  coding style fix.
  [1] Redone missing code. [2] Added empty line at the end of file [3] Removed exception.
  Updated code style. braces on the same line for control statements.
  Removed unused columsn from find constraint sql. Fixed typo. Added extra schema check for when a foreign table is not in the same schema. Updated indentation to conform to other classes.
  Multilevel Items
  Fixes issue #514.
  Removed the config setting that should not have been commited.
  Removed false exception catching.
  Removed custom pgsql PDO and added defaultSchema as public property.
  CS fixes.
  Minor refactoring of DbMessageSource.
  Response WIP
  Minor refactoring of t().
  Added Jsonable support.
  Fixes #13. Implement DB message source.
  Yii::t() minor fix.
  ...
0aab6de
@im-kulikov im-kulikov added a commit to im-kulikov/yii2 that referenced this issue Feb 2, 2015
@im-kulikov im-kulikov Work around #6506: About yii2-mongodb find()->one
Dotdeb now has php5-mongo_1%3a1.6.0-1~dotdeb.2_amd64.deb for Debian 7

Stack trace:
```php
PHP Warning 'yii\base\ErrorException' with message 'Invalid argument supplied for foreach()'

in /project/vendor/composer/yiisoft/yii2/db/BaseActiveRecord.php:1047

Stack trace:
#0 /project/vendor/composer/yiisoft/yii2/db/BaseActiveRecord.php(1047): yii\base\ErrorHandler->handleError(2, 'Invalid argumen...', '/project...', 1047, Array)
#1 /project/vendor/composer/yiisoft/yii2/db/ActiveQueryTrait.php(132): yii\db\BaseActiveRecord::populateRecord(Object(app\models\Setting), NULL)
#2 /project/vendor/composer/yiisoft/yii2-mongodb/ActiveQuery.php(215): yii\mongodb\ActiveQuery->createModels(Array)
#3 /project/vendor/composer/yiisoft/yii2-mongodb/ActiveQuery.php(154): yii\mongodb\ActiveQuery->populate(Array)
#4 /project/app/components/Settings.php(31): yii\mongodb\ActiveQuery->one()
#5 /project/vendor/composer/yiisoft/yii2/base/Object.php(107): app\components\Settings->init()
#6 [internal function]: yii\base\Object->__construct(Array)
#7 /project/vendor/composer/yiisoft/yii2/di/Container.php(362): ReflectionClass->newInstanceArgs(Array)
#8 /project/vendor/composer/yiisoft/yii2/di/Container.php(147): yii\di\Container->build('app\\components\\...', Array, Array)
#9 /project/vendor/composer/yiisoft/yii2/BaseYii.php(344): yii\di\Container->get('app\\components\\...', Array, Array)
#10 /project/vendor/composer/yiisoft/yii2/di/ServiceLocator.php(133): yii\BaseYii::createObject(Array)
#11 /project/vendor/composer/yiisoft/yii2/base/Application.php(302): yii\di\ServiceLocator->get('settings')
#12 /project/vendor/composer/yiisoft/yii2/web/Application.php(63): yii\base\Application->bootstrap()
#13 /project/vendor/composer/yiisoft/yii2/base/Application.php(267): yii\web\Application->bootstrap()
#14 /project/vendor/composer/yiisoft/yii2/base/Object.php(107): yii\base\Application->init()
#15 /project/vendor/composer/yiisoft/yii2/base/Application.php(206): yii\base\Object->__construct(Array)
#16 /project/web/index.php(40): yii\base\Application->__construct(Array)
#17 {main}
```

I found the following code in `mongo-php-driver` (maybe it will help):

`v1.5.8` - https://github.com/mongodb/mongo-php-driver/blob/1.5.8/cursor.c#L234:

```c
	if (!cursor->started_iterating) {
		MONGO_METHOD(MongoCursor, doQuery, return_value, getThis());
		cursor->started_iterating = 1;
	}
```

`v1.6.0` - https://github.com/mongodb/mongo-php-driver/blob/1.6.0/cursor.c#L240:

```c
	if (!cursor->started_iterating) {
		php_mongo_runquery(cursor TSRMLS_CC);
		if (EG(exception)) {
			RETURN_NULL();
		}
		cursor->started_iterating = 1;
	}
```
Now, in Yii (don't pretend to be correct, but in that case - it works ;) ):

https://github.com/yiisoft/yii2/blob/master/extensions/mongodb/Query.php#L147-L172
```php
    protected function fetchRowsInternal($cursor, $all, $indexBy)
    {
        $result = [];
        if ($all) {
            foreach ($cursor as $row) {
                if ($indexBy !== null) {
                    if (is_string($indexBy)) {
                        $key = $row[$indexBy];
                    } else {
                        $key = call_user_func($indexBy, $row);
                    }
                    $result[$key] = $row;
                } else {
                    $result[] = $row;
                }
            }
        } else {
            if ($cursor->hasNext()) {
                $result = $cursor->getNext();
            } else {
                $result = false;
            }
        }
        return $result;
    }
```

I replace with:

```php
    protected function fetchRowsInternal($cursor, $all, $indexBy)
    {
        $result = [];

        foreach ($cursor as $row) {
            // if we need all - no changes:
            if ($all) {
                if ($indexBy !== null) {
                    if (is_string($indexBy)) {
                        $key = $row[$indexBy];
                    } else {
                        $key = call_user_func($indexBy, $row);
                    }
                    $result[$key] = $row;
                } else {
                    $result[] = $row;
                }
            // otherwise get one and break:
            } else {
                $result = $row;

                break;
            }
        }

        return $result;
    }
```
ba3c7b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment