/** * Construct */ public function __construct($table = NULL) { if (isset($table) && $table instanceof Soup_Table) { $this->_table = $table; } else { $class = Soup_Inflector::classify(Soup_Inflector::tableize($table)); if (class_exists($class)) { $newClass = new $class(); $this->_table = $newClass->getTable(); $exists = FALSE; } } $this->_oid = self::$_index; self::$_index++; }
public static function importSchema($directory, array $connections = array(), array $options = array()) { $classes = array(); $builder = new Soup_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $definitions = array(); foreach (self::listTables() as $table) { $definition = array(); $definition['tableName'] = $table; $definition['className'] = Soup_Inflector::classify(Soup_Inflector::tableize($table)); $definition['columns'] = self::listTableColumns($table); $definition['connection'] = null; $definition['connectionClassName'] = $definition['className']; try { $definition['relations'] = array(); $relations = self::listTableRelations($table); $relClasses = array(); $counter = array(); foreach ($relations as $relation) { $table = $relation['table']; $class = Soup_Inflector::classify(Soup_Inflector::tableize($table)); $counter[$table] = isset($counter[$table]) ? $counter[$table] + 1 : 1; if (in_array($class, $relClasses)) { $alias = $class . '_' . $counter[$table]; } else { $alias = $class; } $relClasses[] = $class; $definition['relations'][$alias] = array('alias' => $alias, 'class' => $class, 'local' => $relation['local'], 'foreign' => $relation['foreign']); } } catch (Exception $e) { } $definitions[strtolower($definition['className'])] = $definition; $classes[] = $definition['className']; } // Build opposite end of relationships foreach ($definitions as $definition) { $className = $definition['className']; $relClasses = array(); $counter = array(); foreach ($definition['relations'] as $alias => $relation) { $counter[$relation['class']] = isset($counter[$relation['class']]) ? $counter[$relation['class']] + 1 : 1; if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) { $alias = $className . '_' . (count($relClasses) + 1); // $alias = $className . '_' . $counter[$className]; } else { $alias = $className; } $relClasses[] = $relation['class']; $definitions[strtolower($relation['class'])]['relations'][$alias] = array('type' => Soup_Table::RELATION_MANY, 'alias' => $alias, 'class' => $className, 'local' => $relation['foreign'], 'foreign' => $relation['local']); } } // Build records foreach ($definitions as $definition) { $builder->buildRecord($definition); } return $classes; }
/** * Resolves the passed find by field name inflecting the parameter. * * This method resolves the appropriate field name * regardless of whether the user passes a column name, field name, or a Doctrine_Inflector::classified() * version of their column name. It will be inflected with Doctrine_Inflector::tableize() * to get the column or field name. * * @param string $name * @return string $fieldName */ public function resolveFindByFieldName($name) { $fieldName = Soup_Inflector::tableize($name); if ($this->hasColumn($name) || $this->hasField($name)) { return $this->getFieldName($this->getColumnName($name)); } else { if ($this->hasColumn($fieldName) || $this->hasField($fieldName)) { return $this->getFieldName($this->getColumnName($fieldName)); } else { return false; } } }
/** * buildRelationships * * Loop through an array of schema information and build all the necessary relationship information * Will attempt to auto complete relationships and simplify the amount of information required * for defining a relationship * * @param string $array * @return void */ protected function _buildRelationships($array) { // Handle auto detecting relations by the names of columns // User.contact_id will automatically create User hasOne Contact local => contact_id, foreign => id foreach ($array as $className => $properties) { if (isset($properties['columns']) && !empty($properties['columns']) && isset($properties['detect_relations']) && $properties['detect_relations']) { foreach ($properties['columns'] as $column) { // Check if the column we are inflecting has a _id on the end of it before trying to inflect it and find // the class name for the column if (strpos($column['name'], '_id')) { $columnClassName = Soup_Inflector::classify(str_replace('_id', '', $column['name'])); if (isset($array[$columnClassName]) && !isset($array[$className]['relations'][$columnClassName])) { $array[$className]['relations'][$columnClassName] = array(); // Set the detected foreign key type and length to the same as the primary key // of the related table $type = isset($array[$columnClassName]['columns']['id']['type']) ? $array[$columnClassName]['columns']['id']['type'] : 'integer'; $length = isset($array[$columnClassName]['columns']['id']['length']) ? $array[$columnClassName]['columns']['id']['length'] : 8; $array[$className]['columns'][$column['name']]['type'] = $type; $array[$className]['columns'][$column['name']]['length'] = $length; } } } } } foreach ($array as $name => $properties) { if (!isset($properties['relations'])) { continue; } $className = $properties['className']; $relations = $properties['relations']; foreach ($relations as $alias => $relation) { $class = isset($relation['class']) ? $relation['class'] : $alias; if (!isset($array[$class])) { continue; } $relation['class'] = $class; $relation['alias'] = isset($relation['alias']) ? $relation['alias'] : $alias; // Attempt to guess the local and foreign if (isset($relation['refClass'])) { $relation['local'] = isset($relation['local']) ? $relation['local'] : Soup_Inflector::tableize($name) . '_id'; $relation['foreign'] = isset($relation['foreign']) ? $relation['foreign'] : Soup_Inflector::tableize($class) . '_id'; } else { $relation['local'] = isset($relation['local']) ? $relation['local'] : Soup_Inflector::tableize($relation['class']) . '_id'; $relation['foreign'] = isset($relation['foreign']) ? $relation['foreign'] : 'id'; } if (isset($relation['refClass'])) { $relation['type'] = 'many'; } if (isset($relation['type']) && $relation['type']) { $relation['type'] = $relation['type'] === 'one' ? Soup_Relation::ONE : Soup_Relation::MANY; } else { $relation['type'] = Soup_Relation::ONE; } if (isset($relation['foreignType']) && $relation['foreignType']) { $relation['foreignType'] = $relation['foreignType'] === 'one' ? Soup_Relation::ONE : Soup_Relation::MANY; } $relation['key'] = $this->_buildUniqueRelationKey($relation); $this->_validateSchemaElement('relation', array_keys($relation), $className . '->relation->' . $relation['alias']); $this->_relations[$className][$alias] = $relation; } } // Now we auto-complete opposite ends of relationships $this->_autoCompleteOppositeRelations(); // Make sure we do not have any duplicate relations $this->_fixDuplicateRelations(); // Set the full array of relationships for each class to the final array foreach ($this->_relations as $className => $relations) { $array[$className]['relations'] = $relations; } return $array; }
/** * FROM SQL kodunu oluşturur * @param string $table * @param string $alias * @return Soup_Query */ public function from($table, $alias = NULL) { $table = Soup_Inflector::tableize($table); $this->_alias[$alias] = $table; // $this->_sqlParts["from"] = $table . (!is_null($alias) ? self::SQL_AS . $alias : NULL); $this->_sqlParts["from"] = $table; $this->_setTableInfo($table); return $this; }
public function buildAccessors(array $definition) { $accessors = array(); foreach (array_keys($definition['columns']) as $name) { $accessors[] = $name; } foreach ($definition['relations'] as $relation) { $accessors[] = $relation['alias']; } $ret = ''; foreach ($accessors as $name) { // getters $ret .= PHP_EOL . ' public function get' . Soup_Inflector::classify(Soup_Inflector::tableize($name)) . "(\$load = true)" . PHP_EOL; $ret .= " {" . PHP_EOL; $ret .= " return \$this->get('{$name}', \$load);" . PHP_EOL; $ret .= " }" . PHP_EOL; // setters $ret .= PHP_EOL . ' public function set' . Soup_Inflector::classify(Soup_Inflector::tableize($name)) . "(\${$name}, \$load = true)" . PHP_EOL; $ret .= " {" . PHP_EOL; $ret .= " return \$this->set('{$name}', \${$name}, \$load);" . PHP_EOL; $ret .= " }" . PHP_EOL; } return $ret; }
/** * importSchema * * method for importing existing schema to Soup_Record classes * * @param string $directory * @param array $connections Array of connection names to generate models for * @return array the names of the imported classes */ public function importSchema($directory, array $connections = array(), array $options = array()) { try { $classes = array(); print 'start'; $connection = Soup_Manager::getDefaultConnection(); $builder = new Soup_Import_Builder(); $builder->setTargetPath($directory); $builder->setOptions($options); $definitions = array(); print_r($connection->getImport()); foreach ($connection->getImport()->listTables() as $table) { $definition = array(); $definition['tableName'] = $table; $definition['className'] = Soup_Inflector::classify(Soup_Inflector::tableize($table)); $definition['columns'] = $connection->getImport()->listTableColumns($table); $definition['connection'] = $connection->getName(); $definition['connectionClassName'] = $definition['className']; try { $definition['relations'] = array(); $relations = $connection->getImport()->listTableRelations($table); $relClasses = array(); foreach ($relations as $relation) { $table = $relation['table']; $class = Soup_Inflector::classify(Soup_Inflector::tableize($table)); if (in_array($class, $relClasses)) { $alias = $class . '_' . (count($relClasses) + 1); } else { $alias = $class; } $relClasses[] = $class; $definition['relations'][$alias] = array('alias' => $alias, 'class' => $class, 'local' => $relation['local'], 'foreign' => $relation['foreign']); } } catch (Exception $e) { } $definitions[strtolower($definition['className'])] = $definition; $classes[] = $definition['className']; } // Build opposite end of relationships foreach ($definitions as $definition) { $className = $definition['className']; $relClasses = array(); foreach ($definition['relations'] as $alias => $relation) { if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) { $alias = $className . '_' . (count($relClasses) + 1); } else { $alias = $className; } $relClasses[] = $relation['class']; $definitions[strtolower($relation['class'])]['relations'][$alias] = array('type' => Soup_Table::RELATION_MANY, 'alias' => $alias, 'class' => $className, 'local' => $relation['foreign'], 'foreign' => $relation['local']); } } // Build records foreach ($definitions as $definition) { $builder->buildRecord($definition); } return $classes; } catch (Exception $e) { throw new Soup_Import_Exception($e->getMessage()); } }