Skip to content

Commit a676454

Browse files
authored
Fix #232: Fix master branch tests (#255)
1 parent 732ce16 commit a676454

11 files changed

+1405
-257
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ phpunit.phar
2828
/phpunit.xml
2929

3030
# local tests files
31-
/tests/ActiveRecordTestTrait.php
3231
/tests/data/config.local.php

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ env:
44

55
language: php
66

7+
dist: trusty
8+
79
php:
810
- 5.4
911
- 5.5

ActiveRecord.php

+64-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
use yii\base\InvalidConfigException;
1414
use yii\base\InvalidParamException;
1515
use yii\base\NotSupportedException;
16+
use yii\db\ActiveQueryInterface;
17+
use yii\db\ActiveRecordInterface;
1618
use yii\db\BaseActiveRecord;
1719
use yii\db\StaleObjectException;
1820
use yii\helpers\ArrayHelper;
1921
use yii\helpers\Inflector;
20-
use yii\helpers\Json;
2122
use yii\helpers\StringHelper;
2223

2324
/**
@@ -118,10 +119,10 @@ public static function findAll($condition)
118119
*/
119120
private static function filterCondition($condition)
120121
{
121-
foreach($condition as $k => $v) {
122+
foreach ($condition as $k => $v) {
122123
if (is_array($v)) {
123124
$condition[$k] = array_values($v);
124-
foreach($v as $vv) {
125+
foreach ($v as $vv) {
125126
if (is_array($vv)) {
126127
throw new InvalidArgumentException('Nested arrays are not allowed in condition for findAll() and findOne().');
127128
}
@@ -358,7 +359,7 @@ public static function populateRecord($record, $row)
358359
if (isset($row['fields'])) {
359360
// reset fields in case it is scalar value
360361
$arrayAttributes = $record->arrayAttributes();
361-
foreach($row['fields'] as $key => $value) {
362+
foreach ($row['fields'] as $key => $value) {
362363
if (!isset($arrayAttributes[$key]) && count($value) === 1) {
363364
$row['fields'][$key] = reset($value);
364365
}
@@ -528,14 +529,14 @@ public function update($runValidation = true, $attributeNames = null, $options =
528529
}
529530

530531
/**
531-
* @see update()
532532
* @param array $attributes attributes to update
533533
* @param array $options options given in this parameter are passed to elasticsearch
534534
* as request URI parameters. See [[update()]] for details.
535535
* @return int|false the number of rows affected, or false if [[beforeSave()]] stops the updating process.
536536
* @throws StaleObjectException if optimistic locking is enabled and the data being updated is outdated.
537537
* @throws InvalidParamException if no [[version]] is available and optimistic locking is enabled.
538538
* @throws Exception in case update failed.
539+
* @see update()
539540
*/
540541
protected function updateInternal($attributes = null, $options = [])
541542
{
@@ -564,7 +565,7 @@ protected function updateInternal($attributes = null, $options = [])
564565
$values,
565566
$options
566567
);
567-
} catch(Exception $e) {
568+
} catch (Exception $e) {
568569
// HTTP 409 is the response in case of failed optimistic locking
569570
// http://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html
570571
if (isset($e->errorInfo['responseCode']) && $e->errorInfo['responseCode'] == 409) {
@@ -629,9 +630,9 @@ protected static function primaryKeysByCondition($condition)
629630
* @param array $attributes attribute values (name-value pairs) to be saved into the table
630631
* @param array $condition the conditions that will be passed to the `where()` method when building the query.
631632
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
632-
* @see [[ActiveRecord::primaryKeysByCondition()]]
633633
* @return int the number of rows updated
634634
* @throws Exception on error.
635+
* @see [[ActiveRecord::primaryKeysByCondition()]]
635636
*/
636637
public static function updateAll($attributes, $condition = [])
637638
{
@@ -677,9 +678,9 @@ public static function updateAll($attributes, $condition = [])
677678
* Use negative values if you want to decrement the counters.
678679
* @param array $condition the conditions that will be passed to the `where()` method when building the query.
679680
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
680-
* @see [[ActiveRecord::primaryKeysByCondition()]]
681681
* @return int the number of rows updated
682682
* @throws Exception on error.
683+
* @see [[ActiveRecord::primaryKeysByCondition()]]
683684
*/
684685
public static function updateAllCounters($counters, $condition = [])
685686
{
@@ -767,7 +768,7 @@ public function delete($options = [])
767768
$this->getOldPrimaryKey(false),
768769
$options
769770
);
770-
} catch(Exception $e) {
771+
} catch (Exception $e) {
771772
// HTTP 409 is the response in case of failed optimistic locking
772773
// http://www.elastic.co/guide/en/elasticsearch/guide/current/optimistic-concurrency-control.html
773774
if (isset($e->errorInfo['responseCode']) && $e->errorInfo['responseCode'] == 409) {
@@ -799,9 +800,9 @@ public function delete($options = [])
799800
*
800801
* @param array $condition the conditions that will be passed to the `where()` method when building the query.
801802
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
802-
* @see [[ActiveRecord::primaryKeysByCondition()]]
803803
* @return int the number of rows deleted
804804
* @throws Exception on error.
805+
* @see [[ActiveRecord::primaryKeysByCondition()]]
805806
*/
806807
public static function deleteAll($condition = [])
807808
{
@@ -857,4 +858,57 @@ public function unlinkAll($name, $delete = false)
857858
{
858859
throw new NotSupportedException('unlinkAll() is not supported by elasticsearch, use unlink() instead.');
859860
}
861+
862+
public function link($name, $model, $extraColumns = [])
863+
{
864+
$relation = $this->getRelation($name);
865+
866+
if ($relation->via === null) {
867+
$this->validateViaRelationLink($model, $relation);
868+
}
869+
870+
parent::link($name, $model, $extraColumns);
871+
}
872+
873+
/**
874+
* Validates model so that it does not contain array as it's keys while linking.
875+
*
876+
* @param ActiveRecordInterface $model the model to be linked with the current one.
877+
* @param ActiveQueryInterface|ActiveQuery the relational query object.
878+
*/
879+
protected function validateViaRelationLink($model, $relation)
880+
{
881+
$p1 = $model->isPrimaryKey(array_keys($relation->link));
882+
$p2 = static::isPrimaryKey(array_values($relation->link));
883+
884+
$atLeastOneExists = !$this->getIsNewRecord() || !$model->getIsNewRecord();
885+
886+
$foreign = null;
887+
$link = null;
888+
889+
if ($p1 && $p2 && $atLeastOneExists) {
890+
891+
if ($this->getIsNewRecord()) {
892+
$foreign = $this;
893+
$link = array_flip($relation->link);
894+
} else {
895+
$foreign = $model;
896+
$link = $relation->link;
897+
}
898+
} elseif ($p1) {
899+
$foreign = $this;
900+
$link = array_flip($relation->link);
901+
} elseif ($p2) {
902+
$foreign = $model;
903+
$link = $relation->link;
904+
}
905+
906+
if ($foreign && $link) {
907+
foreach ($link as $fk => $pk) {
908+
if (is_array($foreign->{$fk})) {
909+
throw new InvalidCallException('Unable to link models: foreign model cannot be linked if it\'s property is an array.');
910+
}
911+
}
912+
}
913+
}
860914
}

docs/guide-ru/usage-ar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ $aggData = Customer::find()->addAggregation('customers_by_date', 'terms', [
159159
$customersByDate = ArrayHelper::map($aggData['aggregations']['customers_by_date']['buckets'], 'key', 'doc_count');
160160
```
161161

162-
Теперь `$customersByDate` содержит 10 дат, которые соответствуют наибольшему числу зарегистрированных пользователей.
162+
Теперь `$customersByDate` содержит 10 дат, которые соответствуют наибольшему числу зарегистрированных пользователей.

0 commit comments

Comments
 (0)