public static function setupRedBeans() { $config = Config::get(); if (!isset($config['db']['host'])) { throw new Exception('Config[db][host] not set. Check api.conf in project folder.'); } if (!isset($config['db']['db'])) { throw new Exception('Config[db][db] not set. Check api.conf in project folder.'); } if (!isset($config['db']['user'])) { throw new Exception('Config[db][user] not set. Check api.conf in project folder.'); } if (!isset($config['db']['password'])) { throw new Exception('Config[db][password] not set. Check api.conf in project folder.'); } R::setup('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['db'], $config['db']['user'], $config['db']['password'], true); R::exec('SET NAMES utf8mb4'); R::freeze(true); }
static function deleteModel($model) { return R::exec('UPDATE models SET deleted=1 WHERE name = ?', [$model]); }
<?php require_once 'models/Answer.php'; use RedBean_Facade as R; $payload = $app->request()->getBody(); $params = array('userid' => 'userid', 'questionid' => 'questionid', 'answer' => 'answer'); $payload = array_intersect_key($payload, $params); if (empty($payload['questionid'])) { throw new \Exception('questionid is required!', 412); } $quetion = R::findOne('question', '`id` = ?', array($payload['questionid'])); if (!$quetion) { throw new \Exception('Question not found', 404); } $answer = new Answer($payload, $quetion); $answer->validateAnswer(); R::exec('INSERT INTO `answer` (`userid`, `questionid`, `answer`) VALUES (:userid, :questionid, :answer)', array(':userid' => $answer->getValue('userid'), ':questionid' => $answer->getValue('questionid'), ':answer' => $answer->getValue('answer'))); $app->result = $answer->getValues();