Skip to content

Commit

Permalink
Merge branch 'master' into marin-3
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored May 18, 2024
2 parents a3f051b + a010cd8 commit 0c1c7af
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^10.5",
"rector/rector": "^0.19",
"rector/rector": "^1.0",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.20",
Expand Down
20 changes: 15 additions & 5 deletions src/ActiveQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function asArray(bool|null $value = true): self;
* The parameters to this method can be either one or multiple strings, or a single array of relation names and the
* optional callbacks to customize the relations.
*
* A relation name can refer to a relation defined in {@see ActiveQueryTrait::modelClass|modelClass} or a
* sub-relation that stands for a relation of a related record.
* A relation name can refer to a relation defined in {@see modelClass} or a sub-relation that stands for a relation
* of a related record.
*
* For example, `orders.address` means the `address` relation defined in the model class corresponding to the
* `orders` relation.
Expand All @@ -45,7 +45,7 @@ public function asArray(bool|null $value = true): self;
*
* ```php
* // Create active query
* CustomerQuery = new ActiveQuery(Customer::class, $this->db);
* CustomerQuery = new ActiveQuery(Customer::class, $db);
* // find customers together with their orders and country
* CustomerQuery->with('orders', 'country')->all();
* // find customers together with their orders and the orders' shipping address
Expand All @@ -59,8 +59,18 @@ public function asArray(bool|null $value = true): self;
* ])->all();
* ```
*
* @param array|string $with The relations to be eagerly loaded. This can be either an array of relations, or a
* variable number of strings representing the relations.
* You can call `with()` multiple times. Each call will add relations to the existing ones.
*
* For example, the following two statements are equivalent:
*
* ```php
* CustomerQuery->with('orders', 'country')->all();
* CustomerQuery->with('orders')->with('country')->all();
* ```
*
* @param array|string ...$with a list of relation names or relation definitions.
*
* @return static the query object itself.
*/
public function with(array|string ...$with): self;

Expand Down
2 changes: 1 addition & 1 deletion src/ActiveQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function asArray(bool|null $value = true): static
* CustomerQuery->with('orders')->with('country')->all();
* ```
*
* @param array|string $with
* @param array|string ...$with a list of relation names or relation definitions.
*
* @return static the query object itself.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Yiisoft\ActiveRecord;

use IteratorAggregate;
use Throwable;
use Yiisoft\ActiveRecord\Trait\ArrayIteratorTrait;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\InvalidConfigException;
Expand Down Expand Up @@ -80,9 +82,13 @@
*
* @method ActiveQuery hasMany($class, array $link) {@see BaseActiveRecord::hasMany()} for more info.
* @method ActiveQuery hasOne($class, array $link) {@see BaseActiveRecord::hasOne()} for more info.
*
* @template-implements IteratorAggregate<string, mixed>
*/
class ActiveRecord extends BaseActiveRecord
class ActiveRecord extends BaseActiveRecord implements IteratorAggregate
{
use ArrayIteratorTrait;

/**
* The insert operation. This is mainly used when overriding {@see transactions()} to specify which operations are
* transactional.
Expand Down
4 changes: 1 addition & 3 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ArrayAccess;
use Closure;
use IteratorAggregate;
use ReflectionException;
use Throwable;
use Yiisoft\Arrays\ArrayableInterface;
Expand Down Expand Up @@ -46,9 +45,8 @@
* See {@see ActiveRecord} for a concrete implementation.
*
* @template-implements ArrayAccess<int, mixed>
* @template-implements IteratorAggregate<int>
*/
abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface
abstract class BaseActiveRecord implements ActiveRecordInterface, ArrayAccess, ArrayableInterface
{
use ArrayableTrait;
use BaseActiveRecordTrait;
Expand Down
16 changes: 0 additions & 16 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Yiisoft\ActiveRecord;

use ArrayAccess;
use ArrayIterator;
use Error;
use Exception;
use IteratorAggregate;
use ReflectionException;
use ReflectionMethod;
use Throwable;
Expand Down Expand Up @@ -210,20 +208,6 @@ public function __set(string $name, mixed $value): void
throw new UnknownPropertyException('Setting unknown property: ' . static::class . '::' . $name);
}

/**
* Returns an iterator for traversing the attributes in the ActiveRecord.
*
* This method is required by the interface {@see IteratorAggregate}.
*
* @return ArrayIterator an iterator for traversing the items in the list.
*/
public function getIterator(): ArrayIterator
{
$attributes = $this->getAttributes();

return new ArrayIterator($attributes);
}

/**
* Returns whether there is an element at the specified offset.
*
Expand Down
31 changes: 31 additions & 0 deletions src/Trait/ArrayIteratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Trait;

use ArrayIterator;
use IteratorAggregate;

/**
* Trait to implement {@see IteratorAggregate} interface for ActiveRecord.
*
* @method array getAttributes(array|null $names = null, array $except = [])
* @see ActiveRecordInterface::getAttributes() for more info.
*/
trait ArrayIteratorTrait
{
/**
* Returns an iterator for traversing the attributes in the ActiveRecord.
*
* This method is required by the interface {@see IteratorAggregate}.
*
* @return ArrayIterator an iterator for traversing the items in the list.
*/
public function getIterator(): ArrayIterator
{
$attributes = $this->getAttributes();

return new ArrayIterator($attributes);
}
}

0 comments on commit 0c1c7af

Please sign in to comment.