Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voskobovich/3072697f9cf6c7c4a2242140a8a0ff5e to your computer and use it in GitHub Desktop.
Save voskobovich/3072697f9cf6c7c4a2242140a8a0ff5e to your computer and use it in GitHub Desktop.
DI in Yii2 AR model
class User extends ActiveRecord
{
/**
* @var StatusEnum
*/
private $_statusEnum;
/**
* BaseUser constructor.
* @param StatusEnum $statusEnum
* @param array $config
*/
public function __construct(StatusEnum $statusEnum, array $config = [])
{
parent::__construct($config);
$this->_statusEnum = $statusEnum;
}
/**
* Get list of statuses.
* @return array
*/
public function getStatusItems()
{
return $this->_statusEnum->getList();
}
/**
* @return string
*/
public function getStatus()
{
return $this->_statusEnum->getLabelByKey($this->status_key);
}
}
/**
ArgumentCountError
Too few arguments to function base\models\BaseUser::__construct(), 0 passed in /var/www/html/yii2
advanced/vendor/yiisoft/yii2/db/BaseActiveRecord.php on line 1155 and at least 1 expected
*/
// BaseActiveRecord.php on line 1155
/**
* Creates an active record instance.
*
* This method is called together with [[populateRecord()]] by [[ActiveQuery]].
* It is not meant to be used for creating new records directly.
*
* You may override this method if the instance being created
* depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
* @param array $row row data to be populated into the record.
* @return static the newly created active record
*/
public static function instantiate($row)
{
return new static;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment