/** * Deletes the current model from the database * * @return mixed */ public function delete() { $query = $this->createQuery(); $query->setAction('delete'); $query->where($this->getTableKey(), '=', $this->id); return $this->database->query($query); }
/** * Returns all rows matching all conditions passed * * @param array $conditionsArray * * @return \Parable\ORM\Model[]|\Parable\ORM\Model */ public function getByConditions(array $conditionsArray) { $query = $this->createQuery(); foreach ($conditionsArray as $conditionArray) { $query->where(...$conditionArray); } $result = $this->database->query($query); $entities = []; if ($result) { $result = $result->fetchAll(\PDO::FETCH_ASSOC); $entities = $this->handleResult($result); } if ($this->returnOne) { return current($entities); } return $entities; }
/** * Do all the setup * * @return $this */ public function run() { /* Set the basedir on paths */ $this->path->setBasedir(BASEDIR); /* Load all known Config files now that we know the baseDir */ $this->hook->trigger('parable_config_load_before'); $this->config->load(); $this->hook->trigger('parable_config_load_after', $this->config); /* Start the session if session.autoEnable is true */ if ($this->config->get('session.autoEnable') !== false) { $this->hook->trigger('parable_session_start_before'); $this->values->session->start(); $this->hook->trigger('parable_session_start_after', $this->values->session); } /* Build the base Url */ $this->url->buildBaseurl(); /* Load the routes */ $this->loadRoutes(); /* Get the current url */ $currentUrl = $this->url->getCurrentUrl(); /* Load the config */ if ($this->config->get('database.type')) { $this->database->setConfig($this->config->get('database')); } /* See if there's an init directory defined in the config */ if ($this->config->get('initLocations')) { $this->loadInits(); } /* And try to match the route */ $this->hook->trigger('parable_route_match_before', $currentUrl); $route = $this->router->matchCurrentRoute(); $this->hook->trigger('parable_route_match_after', $route); if ($route) { $this->hook->trigger('parable_http_200', $route); $this->dispatcher->dispatch($route); } else { $this->response->setHttpCode(404); $this->hook->trigger('parable_http_404', $currentUrl); } $this->hook->trigger('parable_response_send'); $this->response->send(); return $this; }
/** * Outputs the actual query for use, empty string if invalid/incomplete values given * * @return string */ public function __toString() { // If there's no valid PDO instance, we can't quote so no query for you if (!$this->database->getInstance()) { return ''; } $query = []; if ($this->action === 'select') { $query[] = "SELECT " . $this->select; $query[] = "FROM " . $this->getQuotedTableName(); $query[] = $this->buildJoins(); $query[] = $this->buildWheres(); $query[] = $this->buildOrderBy(); $query[] = $this->buildGroupBy(); $query[] = $this->buildLimitOffset(); } elseif ($this->action === 'delete') { $query[] = "DELETE FROM " . $this->getQuotedTableName(); $query[] = $this->buildWheres(); } elseif ($this->action === 'update') { $query[] = "UPDATE " . $this->getQuotedTableName(); // now get the values if (count($this->values) > 0) { // Set the table values to defaults $tableKey = 'id'; $tableKeyValue = null; $values = []; foreach ($this->values as $key => $value) { // skip id, since we'll use that as a where condition if ($key !== $this->tableKey) { if ($value === null) { $correctValue = 'NULL'; } else { $correctValue = $this->database->quote($value); } // Quote the key $key = $this->database->quoteIdentifier($key); // Add key & value combo to the array $values[] = $key . " = " . $correctValue; } else { $tableKey = $key; $tableKeyValue = $value; } } $query[] = "SET " . implode(', ', $values); $query[] = "WHERE " . $this->database->quoteIdentifier($tableKey); $query[] = " = " . $this->database->quote($tableKeyValue); } else { $query = []; } } elseif ($this->action === 'insert') { // set insert to the proper table $query[] = "INSERT INTO " . $this->getQuotedTableName(); // now get the values if (count($this->values) > 0) { $keys = []; $values = []; foreach ($this->values as $key => $value) { // Quote the key $keys[] = $this->database->quoteIdentifier($key); if ($value === null) { $correctValue = 'NULL'; } else { $correctValue = $this->database->quote($value); } $values[] = $correctValue; } $query[] = "(" . implode(', ', $keys) . ")"; $query[] = "VALUES"; $query[] = "(" . implode(', ', $values) . ")"; } else { $query = []; } } // and now implode it into a nice string, if possible if (count($query) == 0) { return ''; } // Now make it nice. $queryString = implode(' ', $query); $queryString = trim($queryString) . ';'; // Since we got here, we've got a query to output return $queryString; }