/** * Generate SQL SELECT query * * @param mixed $id * @param string $condition * @param string $order * @param integer $limit * @return string */ public function _getSelectQuery_($id, $condition = null, $order = null, $limit = null) { // handle related models $joinFields = $joinTables = null; $joinUsedTables = array(); foreach ($this->RELATED as $field => $params) { $bindModelName = $params['model'] . "Model"; $bindModel = new $bindModelName(); $bindModelTable = $bindModel->getTableName(); $joinFields .= ", " . $bindModelTable . "." . $params['value'] . " as " . $field; if (array_search($bindModelTable, $joinUsedTables) === false) { $joinUsedTables[] = $bindModelTable; $joinTables .= "left join " . $bindModelTable . " on (" . $this->getTableName() . "." . $params['bind'] . " = " . $bindModelTable . "." . $params['match'] . ") "; } } $sql = null; foreach ($this->FIELDS as $field => $desc) { if (array_key_exists($field, $this->RELATED) === false && $desc != 'fake') { $sql .= ($sql ? ", " : "") . $this->getTableName() . ".`" . $field . "` as `" . $field . "`"; } } $sql = "select " . $sql . " " . $joinFields . " from " . $this->getTableName() . " " . $joinTables . " "; $where = $id ? "where " . $this->getTableName() . "." . $this->getPrimaryKeyName() . "='" . $this->_DBLINK->safe($id) . "'" : null; if ($condition) { $where .= $where ? " and " . str_replace("[table]", $this->getTableName(), $condition) : " where " . str_replace("[table]", $this->getTableName(), $condition); } $sql = $sql . $where; if ($order) { $sql .= is_array($order) ? " order by " . $order['field'] . " " . $order['dir'] : " order by " . $order; } if ($limit) { $sql .= is_array($limit) ? " limit " . $limit['offset'] . ", " . $limit['count'] : " limit " . $limit; } return $sql; }