/**
  * @inheritdoc
  */
 public function process(EloquentModel $model, Config $config)
 {
     $schemaManager = $this->databaseManager->connection()->getDoctrineSchemaManager();
     $prefix = $this->databaseManager->connection()->getTablePrefix();
     if (!$schemaManager->tablesExist($prefix . $model->getTableName())) {
         throw new GeneratorException(sprintf('Table %s does not exist', $prefix . $model->getTableName()));
     }
 }
 /**
  * @inheritdoc
  */
 public function process(EloquentModel $model, Config $config)
 {
     if ($config->get('no_timestamps') === true) {
         $pNoTimestamps = new PropertyModel('timestamps', 'public', false);
         $pNoTimestamps->setDocBlock(new DocBlockModel('Indicates if the model should be timestamped.', '', '@var bool'));
         $model->addProperty($pNoTimestamps);
     }
     if ($config->has('date_format')) {
         $pDateFormat = new PropertyModel('dateFormat', 'protected', $config->get('date_format'));
         $pDateFormat->setDocBlock(new DocBlockModel('The storage format of the model\'s date columns.', '', '@var string'));
         $model->addProperty($pDateFormat);
     }
     if ($config->has('connection')) {
         $pConnection = new PropertyModel('connection', 'protected', $config->get('connection'));
         $pConnection->setDocBlock(new DocBlockModel('The connection name for the model.', '', '@var string'));
         $model->addProperty($pConnection);
     }
 }
 /**
  * @inheritdoc
  */
 public function process(EloquentModel $model, Config $config)
 {
     $schemaManager = $this->databaseManager->connection()->getDoctrineSchemaManager();
     $prefix = $this->databaseManager->connection()->getTablePrefix();
     $tableDetails = $schemaManager->listTableDetails($prefix . $model->getTableName());
     $primaryColumnNames = $tableDetails->getPrimaryKey()->getColumns();
     $columnNames = [];
     foreach ($tableDetails->getColumns() as $column) {
         $model->addProperty(new VirtualPropertyModel($column->getName(), $this->typeRegistry->resolveType($column->getType()->getName())));
         if (!in_array($column->getName(), $primaryColumnNames)) {
             $columnNames[] = $column->getName();
         }
     }
     $fillableProperty = new PropertyModel('fillable');
     $fillableProperty->setAccess('protected')->setValue($columnNames)->setDocBlock(new DocBlockModel('@var array'));
     $model->addProperty($fillableProperty);
     return $this;
 }
 /**
  * @inheritdoc
  */
 public function process(EloquentModel $model, Config $config)
 {
     $className = $config->get('class_name');
     $baseClassName = $config->get('base_class_name');
     $tableName = $config->get('table_name');
     $model->setName(new ClassNameModel($className, $this->helper->getShortClassName($baseClassName)));
     $model->addUses(new UseClassModel(ltrim($baseClassName, '\\')));
     $model->setTableName($tableName ?: $this->helper->getDefaultTableName($className));
     if ($model->getTableName() !== $this->helper->getDefaultTableName($className)) {
         $property = new PropertyModel('table', 'protected', $model->getTableName());
         $property->setDocBlock(new DocBlockModel('The table associated with the model.', '', '@var string'));
         $model->addProperty($property);
     }
 }
 /**
  * @param EloquentModel $model
  * @return $this
  */
 protected function setRelations(EloquentModel $model)
 {
     $foreignKeys = $this->manager->listTableForeignKeys($this->addPrefix($model->getTableName()));
     foreach ($foreignKeys as $tableForeignKey) {
         $tableForeignColumns = $tableForeignKey->getForeignColumns();
         if (count($tableForeignColumns) !== 1) {
             continue;
         }
         $relation = new BelongsTo($this->cropPrefix($tableForeignKey->getForeignTableName()), $tableForeignKey->getLocalColumns()[0], $tableForeignColumns[0]);
         $model->addRelation($relation);
     }
     $tables = $this->manager->listTables();
     foreach ($tables as $table) {
         if ($table->getName() === $this->addPrefix($model->getTableName())) {
             continue;
         }
         $foreignKeys = $table->getForeignKeys();
         foreach ($foreignKeys as $name => $foreignKey) {
             if ($foreignKey->getForeignTableName() === $this->addPrefix($model->getTableName())) {
                 $localColumns = $foreignKey->getLocalColumns();
                 if (count($localColumns) !== 1) {
                     continue;
                 }
                 if (count($foreignKeys) === 2 && count($table->getColumns()) >= 2) {
                     $keys = array_keys($foreignKeys);
                     $key = array_search($name, $keys) === 0 ? 1 : 0;
                     $secondForeignKey = $foreignKeys[$keys[$key]];
                     $secondForeignTable = $secondForeignKey->getForeignTableName();
                     $relation = new BelongsToMany($this->cropPrefix($secondForeignTable), $table->getName(), $localColumns[0], $secondForeignKey->getLocalColumns()[0]);
                     $model->addRelation($relation);
                     break;
                 } else {
                     $tableName = $this->cropPrefix($foreignKey->getLocalTableName());
                     $foreignColumn = $localColumns[0];
                     $localColumn = $foreignKey->getForeignColumns()[0];
                     if ($this->isColumnUnique($table, $foreignColumn)) {
                         $relation = new HasOne($tableName, $foreignColumn, $localColumn);
                     } else {
                         $relation = new HasMany($tableName, $foreignColumn, $localColumn);
                     }
                     $model->addRelation($relation);
                 }
             }
         }
     }
     return $this;
 }
 /**
  * @param EloquentModel $model
  * @param Relation $relation
  * @return string
  */
 protected function createMethodBody(EloquentModel $model, Relation $relation)
 {
     $reflectionObject = new \ReflectionObject($relation);
     $name = Str::camel($reflectionObject->getShortName());
     $arguments = [$model->getNamespace()->getNamespace() . '\\' . Str::singular(Str::studly($relation->getTableName()))];
     if ($relation instanceof BelongsToMany) {
         $defaultJoinTableName = $this->helper->getDefaultJoinTableName($model->getTableName(), $relation->getTableName());
         $joinTableName = $relation->getJoinTable() === $defaultJoinTableName ? null : $relation->getJoinTable();
         $arguments[] = $joinTableName;
         $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($model->getTableName()));
         $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), $this->helper->getDefaultForeignColumnName($relation->getTableName()));
     } elseif ($relation instanceof HasMany) {
         $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($model->getTableName()));
         $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), EmgHelper::DEFAULT_PRIMARY_KEY);
     } else {
         $arguments[] = $this->resolveArgument($relation->getForeignColumnName(), $this->helper->getDefaultForeignColumnName($relation->getTableName()));
         $arguments[] = $this->resolveArgument($relation->getLocalColumnName(), EmgHelper::DEFAULT_PRIMARY_KEY);
     }
     return sprintf('return $this->%s(%s);', $name, $this->prepareArguments($arguments));
 }
 /**
  * @inheritdoc
  */
 public function process(EloquentModel $model, Config $config)
 {
     $model->setNamespace(new NamespaceModel($config->get('namespace')));
 }