Skip to content

Instantly share code, notes, and snippets.

@piiskop
Last active March 28, 2018 17:40
Show Gist options
  • Save piiskop/236867f2aa1ced9b42cf94efe23e03c4 to your computer and use it in GitHub Desktop.
Save piiskop/236867f2aa1ced9b42cf94efe23e03c4 to your computer and use it in GitHub Desktop.
<?php
namespace pupils;
class Pupil {
private $id;
private $name;
private $folder;
private $idStudyGroup;
/**
*
* @return mixed
*/
public function getStudyGroup() {
return $this->idStudyGroup;
}
/**
*
* @param mixed $studyGroup
*/
public function setStudyGroup($studyGroup) {
$this->idStudyGroup = $studyGroup;
}
/**
*
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
*
* @return mixed
*/
public function getName() {
return $this->name;
}
/**
*
* @return mixed
*/
public function getFolder() {
return $this->folder;
}
/**
*
* @param mixed $id
*/
public function setId($id) {
$this->id = $id;
}
/**
*
* @param mixed $name
*/
public function setName($name) {
$this->name = $name;
}
/**
*
* @param mixed $folder
*/
public function setFolder($folder) {
$this->folder = $folder;
}
public function __construct($arguments) {
if (isset($arguments['id'])) {
$this->setId($arguments['id']);
}
if (isset($arguments['name'])) {
$this->setName($arguments['name']);
}
if (isset($arguments['folder'])) {
$this->setFolder($arguments['folder']);
}
if (isset($arguments['idStudyGroup'])) {
$this->setStudyGroup($arguments['idStudyGroup']);
}
}
public function insert($arguments) {
if (isset($arguments['batch'])) {
$listOfAllValues = array ();
foreach ($arguments['batch'] as $studyGroup) {
$values = array ();
foreach ($studyGroup as $value) {
$values[] = sprintf('"%1$s"',
$_SESSION['dbEngine']->escape(
array (
'string' => $value
)));
}
$allValues = sprintf('(%1$s)', implode(', ', $values));
$listOfAllValues[] = $allValues;
}
$stringOfAllValues = implode(', ', $listOfAllValues);
} else {
new \o\Response(array (
'message' => 'The index batch in $arguments is missing.',
'status' => 500
));
}
$_SESSION['dbEngine']->queryDatabase(
array (
//@formatter:off
'queryString' => sprintf(
'INSERT INTO Pupil (name, folder, StudyGroup_idStudyGroup) VALUES %1$s',
$stringOfAllValues // 1
)
// @formatter:on
));
}
public static function find($arguments) {
$query = 'SELECT idPupil, name, folder, StudyGroup_idStudyGroup FROM Pupil' . (isset(
$arguments['where']) ? $_SESSION['dbEngine']->buildWhere(
$arguments['where']) : '');
$_SESSION['dbEngine']->queryDatabase(
array (
'queryString' => $query
));
return $_SESSION['dbEngine']->getRecords();
}
public function update() {
$fields = array ();
if (NULL !== $this->getName()) {
$field = sprintf('name = "%1$s"',
$_SESSION['dbEngine']->escape(
array (
'string' => $this->getName()
)));
$fields[] = $field;
}
if (NULL !== $this->getFolder()) {
$field = sprintf('folder = "%1$s"',
$_SESSION['dbEngine']->escape(
array (
'string' => $this->getFolder()
)));
$fields[] = $field;
}
if (NULL !== $this->getStudyGroup()) {
$field = sprintf('StudyGroup_idStudyGroup = %1$u',
$this->getStudyGroup());
$fields[] = $field;
}
$_SESSION['dbEngine']->queryDatabase(
array (
// @formatter:off
'queryString' => sprintf(
'UPDATE Pupil SET %1$s%2$s',
implode(', ', $fields), // 1
$_SESSION['dbEngine']->buildWhere(array (
'idPupil' => array (
'=' => $this->getId()
)
)) // 2
)
// @formatter:on
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment