Skip to content

Commit

Permalink
now can use a string for the find orm method
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Aug 14, 2017
1 parent bbe9d1a commit aad39bc
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions Model.php
Original file line number Diff line number Diff line change
@@ -499,6 +499,33 @@ public function createUpdateQuery($where_clause)
return $sql = "UPDATE `$table` SET $val WHERE $where_clause LIMIT 1";
}

public function createUpdateQueryFromPost($post)
{
$this->makeDatabaseConnection();
$val = "";
foreach ($post as $key => $value) {
$val .= "`".$key."` = :".$key.",";
}

$val = rtrim($val, ",");

$table = $this->getTableName();

if (isset($post['id'])) {
$where_clause = "id = :id";
}

$sql = "UPDATE `$table` SET $val WHERE $where_clause LIMIT 1";

$prepared_query = $this->db_engine->prepare($sql);

foreach ($post as $key => $value) {
$prepared_query->bindValue($key, $value);
}

return $prepared_query;
}

public function createInsertQueryFromPost($post)
{
$this->makeDatabaseConnection();
@@ -746,9 +773,14 @@ public function get($where_clause = 1)
return $this->getCall();
}

public function find($id)
public function find($value)
{
$result = $this->get("id = '$id'");
if (is_int($id)) {
$result = $this->get("id = '$value'");
} else {
$result = $this->get($value);
}

$new_instance = new $this;

$new_instance->update = true;
@@ -828,20 +860,12 @@ public function update($where_clause = 1)
return $query;
}

public function updateFromPost($post, $where_clause = 1)
public function updateFromPost()
{
$this->triggerEvent('updating');
$this->makeDatabaseConnection();
$values = "";
foreach ($post as $key => $value) {
$values .= "`".$key."` = '".$value."',";
}

$val = str_replace(":", "", $values);
$val = rtrim($val, ",");

$table = $this->getTableName();
$query = $this->db_engine->query("UPDATE `$table` SET $val WHERE $where_clause LIMIT 1");
$query =$this->createUpdateQueryFromPost($_POST);
$query->execute();
return $query;
}

0 comments on commit aad39bc

Please sign in to comment.