/** * Insert new record. * * @param EntityInterface $data * @return mixed Returns `false` or {@see \odTimeTracker\Model\ActivityEntity}. */ public function insert(EntityInterface $entity) { $projectId = $entity->getProjectId(); $name = $entity->getName(); $desc = $entity->getDescription(); $tags = $entity->getTags(); $started = $entity->getStarted(); $startedObj = is_null($started) ? new \DateTime() : $started; $startedStr = $startedObj->format(\DateTime::RFC3339); $stopped = $entity->getStopped(); $stoppedObj = is_null($stopped) ? null : ($stopped instanceof \DateTime ? $stopped : new \DateTime($stopped)); $stoppedStr = $stopped instanceof \DateTime ? $stoppedObj->format(\DateTime::RFC3339) : ''; $table = self::TABLE_NAME; $stmt = $this->pdo->prepare(<<<EOT INSERT INTO `{$this->tableName}` (ProjectId, Name, Description, Tags, Started, Stopped) VALUES ( :projectId, :name , :desc , :tags , :started , :stopped ); EOT ); $stmt->bindParam(':projectId', $projectId, \PDO::PARAM_INT); $stmt->bindParam(':name', $name, \PDO::PARAM_STR); $stmt->bindParam(':desc', $desc, \PDO::PARAM_STR); $stmt->bindParam(':tags', $tags, \PDO::PARAM_STR); $stmt->bindParam(':started', $startedStr, \PDO::PARAM_STR); $stmt->bindParam(':stopped', $stoppedStr); $res = $stmt->execute(); if ($res === false || $stmt->rowCount() !== 1) { return false; } return $this->prepareEntity(array('ActivityId' => $this->pdo->lastInsertId(), 'ProjectId' => $projectId, 'Name' => $name, 'Description' => empty($description) ? null : $description, 'Tags' => empty($tags) ? null : $tags, 'Started' => $started, 'Stopped' => $stopped)); }