/** * @param null $number * @return $this */ public function setTotalNumberOfPage($number = null) { if (is_null($number)) { $modelClass = Inflector::getClassNameFromNamespace(get_class($this->model)); $table = Inflector::tabilize($modelClass); $numRecords = $this->model->select($this->count() . " AS " . $this->numCount)->findMany(); $this->totalNumOfPage = $numRecords[0]->{$this->numCount}; return $this; } $this->totalNumOfPage = $number; return $this; }
/** * We will get all column schema from database. * * @return mixed */ public function getColumns() { $table = $this->table->connect($this->database, Inflector::tabilize($this->model)); return $table->{__FUNCTION__}(); }
/** * You cannot create an instance of Schema class * directly * * @param $model */ protected function __construct($model) { $this->klass = $model; if (class_exists(get_class($this->klass))) { $reflectionClass = new \ReflectionClass(get_class($this->klass)); /* | We will set the database connection name here */ if (property_exists($this->klass, 'database')) { $reflectionProperty = $reflectionClass->getProperty('database'); $reflectionProperty->setAccessible(true); $this->database = $reflectionProperty->getValue($this->klass); } else { $this->database = $this->getDefaultConnection(); } /* | We will set the primary key of the table schema */ if (property_exists($this->klass, 'primaryKey')) { $reflectionPropertyKey = $reflectionClass->getProperty('primaryKey'); $reflectionPropertyKey->setAccessible(true); $this->primaryKey = $reflectionPropertyKey->getValue($this->klass); } /* | Set database connection name */ $this->setDatabaseConnection($this->database); if (!property_exists($this->klass, 'tableName')) { $this->tableName = Inflector::tabilize(get_class($this->klass)); } } }
private function replaceModelTemplate($content) { $content = str_replace('%StaticModelName%', $this->command->getModel(), $content); $primaryKey = $this->command->table()->getPrimaryKey(); $content = str_replace('{%Apps%}', APP_NS, $content); $content = str_replace('{%primaryKey%}', $primaryKey, $content); $content = str_replace('%modelName%', Inflector::tabilize($this->command->getModel()), $content); $content = str_replace('%databaseName%', $this->command->getDatabase(), $content); $content = str_replace('{%rules%}', $this->replaceValidationRules(), $content); return $content; }
/** * @param string $template */ public function replaceTemplateByInput($template = 'Migration') { #replace with table name - {%className%} $file = $this->getTemplatePath() . $template . EXT; file_exists($file) or die("Base template doesn't exists"); /*read operation ->*/ // Open the file to get existing content $fileContent = file_get_contents($file); $content = str_replace('{%className%}', Inflector::classify(strtolower($this->command->argumentName)), $fileContent); $content = str_replace('{%database%}', $this->command->getDatabaseName(), $content); $content = str_replace('{%table_name%}', Inflector::tabilize($this->command->argumentName), $content); $contentAppendWith = ''; $contentAppendWith .= '<?php ' . PHP_EOL; $this->replacedContent = $contentAppendWith . $content; }
/** * You cannot create an instance of Schema class * directly. * * @param $model * @param null $database * * @throws \ErrorException */ protected function __construct($model = null, $database = null) { if (!is_null($model) && is_object($model)) { $this->klass = $model; if (!class_exists(get_class($this->klass))) { throw new \ErrorException(sprintf("Class %s doesn't exists", get_class($this->klass))); } $reflectionClass = new \ReflectionClass(get_class($this->klass)); /* | We will set the database connection name here */ if (property_exists($this->klass, 'database')) { $reflectionProperty = $reflectionClass->getProperty('database'); $reflectionProperty->setAccessible(true); $database = $reflectionProperty->getValue($this->klass); } // If table name not provided we will format and consider class name as table if (!property_exists($this->klass, 'tableName')) { $model = Inflector::tabilize(get_class($this->klass)); } } $this->setDatabase(!is_null($database) ? $database : $this->getDefaultConnection()); $this->setTableName($model); }
/** * @return mixed */ public function getTotalNumberOfPages() { $numRecords = null; $modelClass = Inflector::getClassNameFromNamespace(get_class($this->model)); $table = Inflector::tabilize($modelClass); $numRecords = $this->model->query("SELECT " . $this->count() . " as " . $this->numCount . " FROM `" . $table . "`")->getAll(); return $numRecords[0]->{$this->numCount}; }
/** * We will get all column schema from database * @return mixed */ private function getColumns() { return $this->tableSchema->connect($this->database, Inflector::tabilize($this->model))->getColumns(); }
/** * The finder make use of __callStatic() to invoke * undefined static methods dynamically. This magic method is mainly used * for dynamic finders * * @param $method String * @param $arguments array * @return object * */ public static function __callStatic($method, $arguments) { $params = []; $class = self::model(); switch ($method) { case substr($method, 0, 6) == 'findBy': if (strpos($method, 'And') !== false) { return self::callFinderBy($method, $class, $arguments, 'And'); // findByAnd } if (strpos($method, 'Or') !== false) { return self::callFinderBy($method, $class, $arguments, 'Or'); // findByOr } $columnName = Inflector::tabilize(substr($method, 6)); $operator = isset($arguments[1]) ? $arguments[1] : '='; $params = [$columnName, $operator, $arguments[0]]; return self::model()->query()->find('findBy', $params); break; case 'joinWith': return static::$ar->joinWith($class, $arguments); break; } //Use the power of PDO methods directly via static functions return static::callDynamicMethod([self::model()->query()->resolveConnection(), $method], $arguments); }
/** * Quote a string that is used as an identifier * (table names, column names or table.column types etc) or an array containing * multiple identifiers. * * @param $identifier * * @return string */ protected function quoteIdentifier($identifier) { if (is_array($identifier)) { $result = array_map([$this, 'quoteOneIdentifier'], $identifier); return implode(', ', $result); } return Inflector::tabilize($this->quoteOneIdentifier(lcfirst($identifier))); }
public function joinWith($arguments) { $class = static::model(); $tableWith = Inflector::tabilize($arguments[0]); $params = [$class->tableName . '.' . $class->primaryKey, '=', $tableWith . '.' . Inflector::singularize($class->tableName) . self::DEFAULT_FOREIGN_KEY_SUFFIX]; if (isset($arguments[1])) { $params = $arguments[1]; } return $this->fluentQuery()->leftOuterJoin($tableWith, $params, $arguments[2]); }
public function testTabilizeMethod() { $this->assertEquals('user_info', Inflector::tabilize('UserInfo')); $this->assertEquals('user', Inflector::tabilize('User')); }
/** * Replace the model name with original model name. * * @param $content * * @return mixed */ private function replaceModelName($content) { $newContent = ''; $content = str_replace('new %modelName%', 'new ' . $this->model, $content); $content = str_replace('%StaticModelName%', $this->model, $content); $content = str_replace('new %StaticModelName%()', $this->model, $content); $newContent = str_replace('%modelName%', Inflector::tabilize($this->model), $content); return $newContent; }
/** * This method is mainly used for building where conditions as array * for dynamic finders. * * * @param $method String * @param $arguments array * @param $type string * @throws \Exception * @return object * */ public function buildFindersWhereCondition($method, $arguments, $type = 'And') { $condition = []; $condition = explode($type, str_replace('findBy', '', $method)); if (count($condition) == count($arguments[0])) { foreach ($condition as $key => $value) { $field = Inflector::tabilize($value); $whrValue = isset($arguments[0][$key]) ? trim($arguments[0][$key]) : ''; if ($type == 'And') { static::$query->select('all')->where($field, '=', $whrValue); } else { static::$query->select('all')->orWhere($field, '=', $whrValue); } } } else { throw new Exception("Arguments doesn't matched with number of fields"); } return static::$query; }
/** * Get the current table name * @return mixed */ public function table() { return Inflector::tabilize(Inflector::getClassNameFromNamespace(self::getModel())); }
/** * Quote a string that is used as an identifier * (table names, column names etc) or an array containing * multiple identifiers. This method can also deal with * dot-separated identifiers eg table.column */ protected function quoteIdentifier($identifier) { if (is_array($identifier)) { $result = array_map(array($this, 'quoteOneIdentifier'), $identifier); return join(', ', $result); } else { return Inflector::tabilize($this->quoteOneIdentifier(lcfirst($identifier))); } }
/** * We will get all column schema from database. * * @return mixed */ private function getColumns() { $table = $this->table->connect($this->database, Inflector::tabilize($this->tableName)); return $table->getColumns(); }
protected function with($class, $arguments) { $tableWith = Inflector::tabilize($arguments[0]); $params = array($class->tableName . '.' . $class->primaryKey, '=', $tableWith . '.' . Inflector::singularize($class->tableName) . '_id'); if (isset($arguments[1])) { $params = $arguments[1]; } return $this->fluentQuery()->leftOuterJoin($tableWith, $params, $arguments[2]); }