0 follower

Trait Yiisoft\ActiveRecord\Trait\FactoryTrait

Trait to add factory support to ActiveRecord.

See also \Yiisoft\ActiveRecord\Trait\AbstractActiveRecord::createQuery().

Public Methods

Hide inherited methods

Method Description Defined By
createQuery() Yiisoft\ActiveRecord\Trait\FactoryTrait
withFactory() Set the factory to use for creating new instances. Yiisoft\ActiveRecord\Trait\FactoryTrait

Method Details

Hide inherited methods

createQuery() public method

public createQuery( Yiisoft\ActiveRecord\ActiveRecordInterface|string|null $modelClass null ): Yiisoft\ActiveRecord\ActiveQueryInterface
$modelClass Yiisoft\ActiveRecord\ActiveRecordInterface|string|null

                public function createQuery(ActiveRecordInterface|string|null $modelClass = null): ActiveQueryInterface
{
    if (!isset($this->factory)) {
        return parent::createQuery($modelClass);
    }
    $model = is_string($modelClass)
        ? $this->factory->create($modelClass)
        : $modelClass ?? $this;
    if (method_exists($model, 'withFactory')) {
        return parent::createQuery($model->withFactory($this->factory));
    }
    return parent::createQuery($model);
}

            
withFactory() public method

Set the factory to use for creating new instances.

public withFactory( \Yiisoft\Factory\Factory $factory ): Yiisoft\ActiveRecord\Trait\FactoryTrait
$factory \Yiisoft\Factory\Factory

                public function withFactory(Factory $factory): static
{
    $new = clone $this;
    $new->factory = $factory;
    return $new;
}