/** * @return bool|Error */ public function execute() { $set_string = ""; foreach ($this->fields as $k => $v) { $set_string .= $k . "=:" . $k . ","; } $set_string = substr($set_string, 0, -1); $sql = 'UPDATE ' . $this->table . ' SET ' . $set_string . ' WHERE id=:id'; $db = $this->fmPdo->getConnection(); try { $query = $db->prepare($sql); } catch (Exception $e) { return new Error($e); } $query->bindParam(':id', $this->id, PDO::PARAM_STR); foreach ($this->fields as $k => $v) { $query->bindParam(':' . $k, $v[0], PDO::PARAM_STR); } try { if (!$query) { return new Error($this->fmpdo->errorInfo()); } $result = $query->execute(); } catch (Exception $e) { return new Error($e); } return $result; }
public function testGetRecordByID() { //Query Stubbing $queryStub = $this->getMock('MockPdoStatement', array('execute', 'fetch')); $queryStub->expects($this->once())->method('execute')->will($this->returnSelf()); $queryStub->expects($this->once())->method('fetch')->will($this->returnValue(array('id' => 1, 'username' => 'baptiste'))); //PDO stubbing $pdoStub = $this->getMock('\\RjakesTest\\FmPdo\\MockPdo', array('prepare')); $pdoStub->expects($this->once())->method('prepare')->with($this->equalTo("SELECT * FROM users WHERE id='1' LIMIT 1;"))->will($this->returnValue($queryStub)); $this->fmPdo->setConnection($pdoStub); $this->assertInstanceOf('\\Rjakes\\FmPdo\\Record', $this->fmPdo->getRecordByID('users', 1)); }
public function testFindConstruct() { //Query Stubbing $queryStub = $this->getMock('\\RjakesTest\\FmPdo\\MockPdoStatement', array('execute', 'fetchAll')); $queryStub->expects($this->once())->method('execute')->will($this->returnSelf()); $queryStub->expects($this->any())->method('fetchAll')->will($this->returnValue(array(array('id' => 1, 'username' => 'baptiste')))); //PDO stubbing $pdoStub = $this->getMock('\\RjakesTest\\FmPdo\\MockPdo', array('prepare')); $pdoStub->expects($this->once())->method('prepare')->with($this->equalTo("SELECT * FROM users WHERE username=:username ;"))->will($this->returnValue($queryStub)); $this->fmPdo->setConnection($pdoStub); $findC = new Find('users', $this->fmPdo); $findC->addFindCriterion('username', 'baptiste'); $resultSet = $findC->execute(); $this->assertInstanceOf('\\Rjakes\\FmPdo\\Result', $resultSet); /** @var Record $record */ $record = $resultSet->getFirstRecord(); $this->assertInstanceOf('\\Rjakes\\FmPdo\\Record', $record); $this->assertEquals('baptiste', $record->getField('username')); $this->assertEquals('1', $record->getField('id')); }
/** * Assembles the object properties and executes the SQL SELECT statement * @return Error|Result */ public function execute() { $db = $this->fmPdo->getConnection(); try { $query = $db->prepare($this->assemble()); if (!$query) { return new Error($db->errorInfo()); } foreach ($this->findCriteria as $k => $v) { $query->bindParam(':' . $k, $v['value'], PDO::PARAM_STR); } $result = $query->execute(); } catch (Exception $e) { return new Error($e); } $rows = $query->fetchAll(); if (count($rows) > 0) { return new Result($this->table, $rows); } else { return new Error("No records found", "401"); } }