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
 /**
  * Sorgu sonucu dönen kayıtları formatlar
  * @param array $result
  * @return Null|Soup_Result 
  */
 private function _reFormat($result)
 {
     $relations = array();
     $aliases = $this->_alias;
     $somearray = array_values($this->_relations);
     array_walk($somearray, function (&$value) use(&$aliases, &$relations) {
         preg_match('/(.+)\\.(.+)\\s=\\s(.+)\\.(.+)/', $value, $matches);
         $left = $aliases[$matches[3]];
         $right = $aliases[$matches[1]];
         if (sizeof($key = array_keys($relations, $left, TRUE)) > 0) {
             $relations[$key[0] . "/" . $right] = $right;
         } else {
             $relations[$left . "/" . $right] = $right;
         }
     });
     $nestify = function ($array, $delimiter = "/") {
         if (!is_array($array)) {
             return FALSE;
         }
         $nested = array();
         $pattern = "/" . preg_quote($delimiter, "/") . "/";
         foreach ($array as $key => $node) {
             $parts = preg_split($pattern, $key, -1, PREG_SPLIT_NO_EMPTY);
             $leafPart = array_pop($parts);
             $parentArray =& $nested;
             foreach ($parts as $part) {
                 if (!isset($parentArray[$part]) || !is_array($parentArray[$part])) {
                     $parentArray[$part] = array();
                 }
                 $parentArray =& $parentArray[$part];
             }
             if (empty($parentArray[$leafPart])) {
                 $parentArray[$leafPart] = array();
             }
         }
         return $nested;
     };
     if (sizeof($result) > 0) {
         $data = array();
         $skeleton = $nestify($relations);
         foreach ($result as $row) {
             $stack = array();
             foreach ($row as $key => $value) {
                 list($table, $field) = explode("__", $key);
                 $stack[$table][$field] = $value;
             }
             $data[] = sizeof($skeleton) > 0 ? $this->_setDataRows($stack, $skeleton) : $stack;
         }
         foreach ($data as $key => $value) {
             $nValue = array_values($value);
             $newData[$key] = $nValue[0];
         }
         if (strpos($this->_sql, trim(self::SQL_CALC_FOUND_ROWS)) !== FALSE) {
             $newData["foundRows"] = Soup_Manager::getDefaultConnection()->getAdapter()->query("SELECT FOUND_ROWS() AS foundRows")->fetchColumn();
         }
         $originalTable = Soup_Inflector::classify(current(explode(" ", $this->_sqlParts["from"])));
         $resultObject = new Soup_Result($originalTable);
         //             print $originalTable;
         //             print_r($this->_sqlParts["where"]);
         if (isset($this->_sqlParts["where"])) {
             $resultObject->setWhere($this->_sqlParts['where'], $originalTable);
         }
         return $resultObject->populate($newData);
     } else {
         return NULL;
     }
 }