/** * Create an instance of the model. * * This model will be automatically saved to the database if the model we * are generating it for has been saved (the create function was used). * * @param string $name The model definition name. * * @return object */ private function factory($name) { if ($this->factoryMuffin->isPendingOrSaved($this->model)) { return $this->factoryMuffin->create($name); } return $this->factoryMuffin->instance($name); }
/** * @param array $models ex: [ Model1::className(), Model2::className() ] * @throws ModelException * @throws ModelNotFoundException * @throws \League\FactoryMuffin\Exceptions\DefinitionAlreadyDefinedException */ public function __construct($models = []) { parent::__construct(new ModelStoreYii()); if (!empty($models)) { $this->loadModelDefinitions($models); } }
/** * @param array $models ex: [ Model1::className(), Model2::className() ] * @throws ModelException * @throws ModelNotFoundException * @throws \League\FactoryMuffin\Exceptions\DefinitionAlreadyDefinedException */ public function __construct($models = []) { parent::__construct(new ModelStoreYii()); // map factory muffin CRUD methods to Yii2's methods $this->setSaveMethod('save')->setDeleteMethod('delete'); if (!empty($models)) { $this->loadModelDefinitions($models); } }
/** * Generates and saves a record multiple times. * * ```php * $I->haveMultiple('User', 10); // create 10 users * $I->haveMultiple('User', 10, ['is_active' => true]); // create 10 active users * ``` * * @param $name * @param $times * @param array $extraAttrs * * @return \object[] */ public function haveMultiple($name, $times, array $extraAttrs = []) { return $this->factoryMuffin->seed($times, $name, $extraAttrs); }
/** * Generate, and return the attribute. * * The value returned is the result of calling the callable. * * @return mixed */ public function generate() { $saved = $this->factoryMuffin->isPendingOrSaved($this->model); return call_user_func($this->kind, $this->model, $saved); }