public function get($name, $arguments = array()) { //return $this->_results; //return $this->_columns; //return $this->_associations; // search in cloumns foreach ($this->_columns as $k => $v) { $k_parts = \explode('_', $k); $k_name = ''; foreach ($k_parts as $k_part) { $k_name .= \ucfirst($k_part); } if ($name == $k_name . 'Exists' || $name == $k . 'Exists') { if (\array_key_exists($k, $this->_results)) { return true; } else { return false; } } if ($name == $k_name || $name == $k) { // NULL ??? if (\array_key_exists($k, $this->_results)) { return $this->_results[$k]; } elseif (\array_key_exists($k_name, $this->_results)) { return $this->_results[$k_name]; } elseif (\array_key_exists($k, $this->_for_save)) { return $this->_for_save[$k]; } elseif (\array_key_exists($k_name, $this->_for_save)) { return $this->_for_save[$k_name]; } else { ////\hat\dbg::level(2); //\hat\dbg::alert(compact('name', 'k_name', 'k')); //\hat\dbg::alert($this->_for_save); //\hat\dbg::alert($this->_results); echo "<pre>\n\n"; \trigger_error($name . ' not found in this result set.', \E_USER_WARNING); throw new \Exception($name . ' not found in this result set.'); return null; } } } // search in associations /** * enable getBlogs(37); where Blogs is assoc alias */ if (isset($this->_associations[$name])) { if (isset($this->_associations[$name]['type'])) { if ($this->_associations[$name]['type'] == 'hasOne') { if (isset($this->_has_one_for_save[$name])) { return $this->_has_one_for_save[$name]; } elseif (isset($this->_results[$name])) { $assoc = $this->_result_query->getTable($name); $assoc->setResultQuery($this->_result_query); $assoc->setResults($this->_results[$name]); // check this assoc for save upon $this->save() $this->_has_one_for_save[$name] = $assoc; return $assoc; } else { // this assoc is added by get() if (isset($this->_result_query)) { $assoc = $this->_result_query->getTable($name); } else { $assoc = \hatwebtech\dal\DAL::load($name); } $assoc->setResultQuery($this->_result_query); $assoc->setResults(array()); $this->_has_one_for_save[$name] = $assoc; return $this->_has_one_for_save[$name]; } } elseif ($this->_associations[$name]['type'] == 'hasMany') { if (isset($arguments) && isset($arguments[0]) && \is_numeric($arguments[0])) { if (isset($this->_has_many_for_save[$name]) && isset($this->_has_many_for_save[$name][$arguments[0]])) { return $this->_has_many_for_save[$name][$arguments[0]]; } elseif (isset($this->_results[$name]) && isset($this->_results[$name][$arguments[0]])) { $_assoc = $this->_result_query->getTable($name); $assoc = clone $_assoc; $assoc->setResultQuery($this->_result_query); //$assoc = \hatwebtech\dal\DAL::load($name); //print_r($assoc); exit; $assoc->setResults($this->_results[$name][$arguments[0]]); // check this assoc for save upon $this->save() if (!isset($this->_has_many_for_save[$name])) { $this->_has_many_for_save[$name] = array(); } $this->_has_many_for_save[$name][$arguments[0]] = $assoc; // print_r($assoc->toArray()); return $assoc; } else { if (isset($this->_result_query)) { // print_r('result_query'); $_assoc = $this->_result_query->getTable($name); $assoc = clone $_assoc; } else { // print_r('dal::load'); $assoc = \hatwebtech\dal\DAL::load($name); } $assoc->setResultQuery($this->_result_query); // this assoc is added by get(_new_index_) so add it to _has_many_for_save[] $assoc->setResults(array()); if (!isset($this->_has_many_for_save[$name])) { $this->_has_many_for_save[$name] = array(); } $this->_has_many_for_save[$name][$arguments[0]] = $assoc; return $this->_has_many_for_save[$name][$arguments[0]]; //throw new \Exception("Invalid index {$arguments[0]} for $name association."); } } else { throw new \Exception("Missing index for {$name} association."); } } else { throw new \Exception('Unknown type in associations info.'); } } else { throw new \Exception('No type in associations info.'); } $assoc->setResults(array()); return $assoc; } // search for association count foreach ($this->_associations as $k => $v) { if ($name == $k . 'Count') { if ($v['type'] == 'hasMany') { $result_count = 0; if (isset($this->_results[$k])) { $result_count = \count($this->_results[$k]); } $for_save_count = 0; if (isset($this->_has_many_for_save[$k])) { $for_save_count = \count($this->_has_many_for_save[$k]); } return $result_count + $for_save_count; } else { // hasOne if (isset($this->_results[$k]) || isset($this->_has_one_for_save[$k])) { return 1; } return 0; } } if ($name == $k . 'Exists') { // if count is 0 then it does not exists :) return $this->get($k . 'Count', $arguments) ? true : false; // if(isset($this->_results[$k])){ // if($v['type'] == 'hasMany'){ // if(isset($arguments) && isset($arguments[0]) && \is_numeric($arguments[0])){ // if(isset($this->_results[$k][$arguments[0]])){ // return true; // } // } // return false; // }else{ // return true; // } // }else{ // return false; // } } } \trigger_error($name . ' not found.', \E_USER_WARNING); return null; }
protected function _prepareQueryParts_join() { // joins $this->_additional_select = array(); foreach ($this->_query_parts['join'] as $i => $join) { if ($join['type'] == 'left') { // example: hi.HatTemplate ht $dot = \strpos($join['join'], '.'); if ($dot !== false) { $left_alias = \substr($join['join'], 0, $dot); $right_part = \substr($join['join'], $dot + 1); $left_table_name = $this->_getTableNameByAlias($left_alias); if (!$left_table_name) { $this->pdo_error_message[] = "Table object for alias {$left_alias} not found in this query."; return false; } // $has_alias = \strripos($right_part, ' as '); if ($has_alias !== false) { $assoc_name = \substr($right_part, 0, $has_alias); $assoc_alias = \substr($right_part, $has_alias + 4); } else { $has_alias = \strrpos($right_part, ' '); if ($has_alias !== false) { $assoc_name = \substr($right_part, 0, $has_alias); $assoc_alias = \substr($right_part, $has_alias + 1); } else { $assoc_name = $assoc_alias = $right_part; } } $associ_info = $this->_tables[$left_table_name]->associationInfo($assoc_name); if (!$associ_info) { $this->pdo_error_message[] = "Association {$assoc_name} not found in {$left_table_name}."; return false; } $model_name = $associ_info['association_name']; $this->_tables[$model_name] = \hatwebtech\dal\DAL::load($model_name); $primary_keys = $this->_tables[$model_name]->getPrimaryKeys(); $table_info = array('model_name' => $model_name, 'model_alias' => $assoc_alias, 'primary_keys' => $primary_keys, 'assoc_name' => $assoc_name, 'assoc_to_alias' => $left_alias, 'assoc_to_name' => $left_table_name, 'assoc_info' => $associ_info, 'join_index' => $i); //\hat\dbg::alert($right_part); if ($right_part == 'HatSystemConfigArray ca') { //\hat\dbg::alert($table_info); //\hat\dbg::alert($this->_tables[$left_table_name]->toArray(), true); } $this->_tables_in_use[] = $table_info; if ($associ_info['type'] == 'hasMany') { //$this->_has_one_to_many_assoc = true; $this->_has_one_to_many_assoc = $associ_info; } $this->_additional_select[$left_alias][] = $associ_info['keys']['local']; $this->_additional_select[$assoc_alias][] = $associ_info['keys']['foreign']; $this->_additional_select[$assoc_alias] = \array_merge($this->_additional_select[$assoc_alias], $this->_tables[$model_name]->getPrimaryKeys()); $this->_join_pairs[$left_alias . '__' . $associ_info['keys']['local']] = $assoc_alias . '__' . $associ_info['keys']['foreign']; $this->_join_node_keys['local'][$left_alias . '__' . $associ_info['keys']['local']] = true; $this->_join_node_keys['foreign'][$assoc_alias . '__' . $associ_info['keys']['foreign']] = true; $assoc_table_name = $this->_tables[$model_name]->getTableName(); $join_sql = " LEFT JOIN {$assoc_table_name} AS {$assoc_alias} ON {$left_alias}.{$associ_info['keys']['local']}={$assoc_alias}.{$associ_info['keys']['foreign']} "; if (isset($join['condition'])) { //\hat\dbg::alert($join['condition'], true); //\hat\dbg::alert(array($i => $join)); //\hat\Object::__d($join); $prepare = $this->_prepareQueryParts_where($join['condition']); //\hat\dbg::alert($prepare, true); if ($prepare) { $parse = $this->_parseQueryParts_where($prepare); if ($this->_query_debug) { //\hat\dbg::alert($parse); } if ($parse && isset($parse[0])) { $join_sql .= " AND {$parse[0]} "; // \hat\dbg::alert($parse); // \hat\dbg::alert($join_sql, true); } } } $this->_JOIN[$assoc_alias] = $join_sql; // $filename = $this->_table_path . $model_name . '.php'; // if(\file_exists($filename)){ // include_once $filename; // $model_class_name = $this->_table_namespace . $model_name; // $this->_tables[$model_name] = new $model_class_name(); // $table_info = array( // 'model_name' => $model_name, // 'model_alias' => $assoc_alias, // 'assoc_name' => $assoc_name, // 'assoc_to_alias' => $left_alias, // 'assoc_to_name' => $left_table_name, // 'assoc_info' => $associ_info // ); // $this->_tables_in_use[] = $table_info; // if($associ_info['type'] == 'hasMany'){ // //$this->_has_one_to_many_assoc = true; // $this->_has_one_to_many_assoc = $associ_info; // } // $this->_additional_select[$left_alias][] = $associ_info['keys']['local']; // $this->_additional_select[$assoc_alias][] = $associ_info['keys']['foreign']; // // $this->_join_pairs[$left_alias . '__' . $associ_info['keys']['local']] = $assoc_alias . '__' . $associ_info['keys']['foreign']; // $this->_join_node_keys['local'][$left_alias . '__' . $associ_info['keys']['local']] = true; // $this->_join_node_keys['foreign'][$assoc_alias . '__' . $associ_info['keys']['foreign']] = true; // // $assoc_table_name = $this->_tables[$model_name]->getTableName(); // $join_slq = " LEFT JOIN $assoc_table_name AS $assoc_alias ON $left_alias.{$associ_info['keys']['local']}=$assoc_alias.{$associ_info['keys']['foreign']} "; // $this->_JOIN[$assoc_alias] = $join_slq; // // }else{ // throw new \Exception("Module not found for $model_name in $this->_table_path."); // } } else { $this->pdo_error_message[] = 'invalid join sintax.'; return false; } } // end left join } // print_r($this->_JOIN); exit; // print_r($this->_additional_select); return true; }