Exemplo n.º 1
0
 /**
  * Get
  *
  * @param string $fieldName
  *
  * @see Soup_Access::get()
  */
 public function get($fieldName)
 {
     // 		print "\n Get: " . $fieldName . "\n";
     if (!isset($this->_data[$fieldName]) and !array_key_exists($fieldName, $this->_data)) {
         if ($this->getTable()->hasRelation($fieldName)) {
             $foreignKey = $this->getTable()->getForeignKey($fieldName);
             // 				print $fieldName . "\n";
             // 				print_r($foreignKey);
             // 				exit();
             $newResult = new Soup_Result($foreignKey['class']);
             if (!$this->{$foreignKey['local']}) {
                 throw new Soup_Result_Exception('This relation field is not set.');
             }
             if ($foreignKey['type'] == Soup_Table::RELATION_ONE) {
                 $freshResult = $newResult->findOneBy($foreignKey['foreign'], $this->{$foreignKey['local']});
             } else {
                 $freshResult = $newResult->findBy($foreignKey['foreign'], $this->{$foreignKey['local']});
             }
             $this->addWhere($foreignKey['foreign'] . ' = ?', $this->{$foreignKey['local']}, $foreignKey['alias']);
             // 				print $fieldName;
             // 				print $freshResult;
             if (!$freshResult) {
                 // 					print 'pipi';
                 $className = Soup_Inflector::classify($fieldName);
                 $freshResult = new $className();
                 // 					$freshResult->set($foreignKey['foreign'], $this->{$foreignKey['local']});
                 // 					$freshResult->getTable()
                 //                     return $freshResult;
             }
             // 					throw new Soup_Result_Exception('This relation field is not set.');
             $this->set($fieldName, $freshResult);
             return end($this->_data);
         } elseif ($this->getTable()->hasField($fieldName)) {
             //				return NULL;
         } elseif (is_numeric($fieldName) or is_null($fieldName)) {
             //				$this->add ( new Soup_Result ( $this->getTable () ) );
             //				return end ( $this->_data );
         } else {
             //				Zend_Debug::dump($this->_data);
             throw new Soup_Result_Exception('Can\'t found field or relation such "' . $fieldName . '"');
         }
         //			if (isset ( $fieldName )) {
         //				$this->set ( $fieldName, new Soup_Result () );
         //			} else {
         //				$this->add ( new Soup_Result () );
         //			}
         //			return end ( $this->_data );
     } else {
         return $this->_data[$fieldName];
     }
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
 /**
  * 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;
         }
     }
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * Gelen değerleri ilgili tablo ile ilişkilendirir.
  * @param array $node
  * @param array $skeleton
  * @return array 
  */
 private function _setDataRows($node, $skeleton)
 {
     $stack = array();
     foreach ($skeleton as $key => $bone) {
         if (is_array($bone) && sizeof($bone) > 0) {
             $stack[Soup_Inflector::classify($key)] = $node[$key];
             $stack[Soup_Inflector::classify($key)] = array_merge($stack[Soup_Inflector::classify($key)], $this->_setDataRows($node, $bone));
         } else {
             $stack[Soup_Inflector::classify($key)] = $node[$key];
         }
     }
     return $stack;
 }
Exemplo n.º 6
0
 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;
 }
Exemplo n.º 7
0
 /**
  * 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());
     }
 }