Skip to content

Instantly share code, notes, and snippets.

@shyim
Created March 21, 2018 08:24
Show Gist options
  • Save shyim/edc17de35a5896c2a47c8a9eb90541bd to your computer and use it in GitHub Desktop.
Save shyim/edc17de35a5896c2a47c8a9eb90541bd to your computer and use it in GitHub Desktop.
DatabaseEntitiesBuilder generated example
<?php
namespace ShyimDatabaseEntitiesGenerator\Models\Emotion;
use ShyimDatabaseEntitiesGenerator\Models\AbstractEntity;
/**
* Entity for table s_emotion
*/
class Emotion extends AbstractEntity
{
/** @var int $id */
protected $id;
/** @var int $active */
protected $active;
/** @var string $name */
protected $name;
/** @var int $cols */
protected $cols;
/** @var int $cell_spacing */
protected $cell_spacing;
/** @var int $cell_height */
protected $cell_height;
/** @var int $article_height */
protected $article_height;
/** @var int $rows */
protected $rows;
/** @var \DateTime $valid_from */
protected $valid_from;
/** @var \DateTime $valid_to */
protected $valid_to;
/** @var int $userID */
protected $userID;
/** @var int $show_listing */
protected $show_listing;
/** @var int $is_landingpage */
protected $is_landingpage;
/** @var string $seo_title */
protected $seo_title;
/** @var string $seo_keywords */
protected $seo_keywords;
/** @var string $seo_description */
protected $seo_description;
/** @var \DateTime $create_date */
protected $create_date;
/** @var \DateTime $modified */
protected $modified;
/** @var int $template_id */
protected $template_id;
/** @var string $device */
protected $device = '0,1,2,3,4';
/** @var int $fullscreen */
protected $fullscreen = '0';
/** @var string $mode */
protected $mode = 'masonry';
/** @var int $position */
protected $position = '1';
/** @var int $parent_id */
protected $parent_id;
/** @var int $preview_id */
protected $preview_id;
/** @var string $preview_secret */
protected $preview_secret;
/** @var string $customer_stream_ids */
protected $customer_stream_ids;
/** @var string $replacement */
protected $replacement;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
* @return self
*/
public function setId($id): self
{
$this->id = $id;
return $this;
}
/**
* @return int
*/
public function getActive(): int
{
return $this->active;
}
/**
* @param int $active
* @return self
*/
public function setActive(int $active): self
{
$this->active = $active;
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return self
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return int
*/
public function getCols()
{
return $this->cols;
}
/**
* @param int $cols
* @return self
*/
public function setCols($cols): self
{
$this->cols = $cols;
return $this;
}
/**
* @return int
*/
public function getCellSpacing(): int
{
return $this->cell_spacing;
}
/**
* @param int $cell_spacing
* @return self
*/
public function setCellSpacing(int $cell_spacing): self
{
$this->cell_spacing = $cell_spacing;
return $this;
}
/**
* @return int
*/
public function getCellHeight(): int
{
return $this->cell_height;
}
/**
* @param int $cell_height
* @return self
*/
public function setCellHeight(int $cell_height): self
{
$this->cell_height = $cell_height;
return $this;
}
/**
* @return int
*/
public function getArticleHeight(): int
{
return $this->article_height;
}
/**
* @param int $article_height
* @return self
*/
public function setArticleHeight(int $article_height): self
{
$this->article_height = $article_height;
return $this;
}
/**
* @return int
*/
public function getRows(): int
{
return $this->rows;
}
/**
* @param int $rows
* @return self
*/
public function setRows(int $rows): self
{
$this->rows = $rows;
return $this;
}
/**
* @return \DateTime
*/
public function getValidFrom()
{
return $this->valid_from;
}
/**
* @param \DateTime $valid_from
* @return self
*/
public function setValidFrom($valid_from): self
{
$this->valid_from = $valid_from;
return $this;
}
/**
* @return \DateTime
*/
public function getValidTo()
{
return $this->valid_to;
}
/**
* @param \DateTime $valid_to
* @return self
*/
public function setValidTo($valid_to): self
{
$this->valid_to = $valid_to;
return $this;
}
/**
* @return int
*/
public function getUserID()
{
return $this->userID;
}
/**
* @param int $userID
* @return self
*/
public function setUserID($userID): self
{
$this->userID = $userID;
return $this;
}
/**
* @return int
*/
public function getShowListing(): int
{
return $this->show_listing;
}
/**
* @param int $show_listing
* @return self
*/
public function setShowListing(int $show_listing): self
{
$this->show_listing = $show_listing;
return $this;
}
/**
* @return int
*/
public function getIsLandingpage(): int
{
return $this->is_landingpage;
}
/**
* @param int $is_landingpage
* @return self
*/
public function setIsLandingpage(int $is_landingpage): self
{
$this->is_landingpage = $is_landingpage;
return $this;
}
/**
* @return string
*/
public function getSeoTitle(): string
{
return $this->seo_title;
}
/**
* @param string $seo_title
* @return self
*/
public function setSeoTitle(string $seo_title): self
{
$this->seo_title = $seo_title;
return $this;
}
/**
* @return string
*/
public function getSeoKeywords(): string
{
return $this->seo_keywords;
}
/**
* @param string $seo_keywords
* @return self
*/
public function setSeoKeywords(string $seo_keywords): self
{
$this->seo_keywords = $seo_keywords;
return $this;
}
/**
* @return string
*/
public function getSeoDescription(): string
{
return $this->seo_description;
}
/**
* @param string $seo_description
* @return self
*/
public function setSeoDescription(string $seo_description): self
{
$this->seo_description = $seo_description;
return $this;
}
/**
* @return \DateTime
*/
public function getCreateDate()
{
return $this->create_date;
}
/**
* @param \DateTime $create_date
* @return self
*/
public function setCreateDate($create_date): self
{
$this->create_date = $create_date;
return $this;
}
/**
* @return \DateTime
*/
public function getModified()
{
return $this->modified;
}
/**
* @param \DateTime $modified
* @return self
*/
public function setModified($modified): self
{
$this->modified = $modified;
return $this;
}
/**
* @return int
*/
public function getTemplateId()
{
return $this->template_id;
}
/**
* @param int $template_id
* @return self
*/
public function setTemplateId($template_id): self
{
$this->template_id = $template_id;
return $this;
}
/**
* @return string
*/
public function getDevice()
{
return $this->device;
}
/**
* @param string $device
* @return self
*/
public function setDevice($device): self
{
$this->device = $device;
return $this;
}
/**
* @return int
*/
public function getFullscreen(): int
{
return $this->fullscreen;
}
/**
* @param int $fullscreen
* @return self
*/
public function setFullscreen(int $fullscreen): self
{
$this->fullscreen = $fullscreen;
return $this;
}
/**
* @return string
*/
public function getMode(): string
{
return $this->mode;
}
/**
* @param string $mode
* @return self
*/
public function setMode(string $mode): self
{
$this->mode = $mode;
return $this;
}
/**
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* @param int $position
* @return self
*/
public function setPosition($position): self
{
$this->position = $position;
return $this;
}
/**
* @return int
*/
public function getParentId()
{
return $this->parent_id;
}
/**
* @param int $parent_id
* @return self
*/
public function setParentId($parent_id): self
{
$this->parent_id = $parent_id;
return $this;
}
/**
* @return int
*/
public function getPreviewId()
{
return $this->preview_id;
}
/**
* @param int $preview_id
* @return self
*/
public function setPreviewId($preview_id): self
{
$this->preview_id = $preview_id;
return $this;
}
/**
* @return string
*/
public function getPreviewSecret()
{
return $this->preview_secret;
}
/**
* @param string $preview_secret
* @return self
*/
public function setPreviewSecret($preview_secret): self
{
$this->preview_secret = $preview_secret;
return $this;
}
/**
* @return string
*/
public function getCustomerStreamIds()
{
return $this->customer_stream_ids;
}
/**
* @param string $customer_stream_ids
* @return self
*/
public function setCustomerStreamIds($customer_stream_ids): self
{
$this->customer_stream_ids = $customer_stream_ids;
return $this;
}
/**
* @return string
*/
public function getReplacement()
{
return $this->replacement;
}
/**
* @param string $replacement
* @return self
*/
public function setReplacement($replacement): self
{
$this->replacement = $replacement;
return $this;
}
}
<?php
namespace ShyimDatabaseEntitiesGenerator\Models\Emotion;
use Doctrine\DBAL\Connection;
use ShyimDatabaseEntitiesGenerator\Models\AbstractRepository;
/**
* Repository for table s_emotion
*/
class EmotionRepository extends AbstractRepository
{
/**
* Table name
* @var string
*/
const TABLE = 's_emotion';
/** @var Connection */
private $connection;
/**
* EmotionRepository constructor.
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Fetches all records.
* @param int|null $offset
* @param int|null $limit
* @param array|null $where
* @param array|null $sorters
* @return Emotion[]
*/
public function findBy(int $offset = null, int $limit = null, array $where = null, array $sorters = null): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from(self::TABLE);
if ($offset) {
$qb->setFirstResult($offset);
}
if ($limit) {
$qb->setMaxResults($limit);
}
if ($where) {
foreach ($where as $key => $value) {
$qb->andWhere(sprintf('%s = %s', $key, $qb->createNamedParameter($value)));
}
}
if ($sorters) {
foreach ($sorters as $field => $sort) {
$qb->addOrderBy($field, $sort);
}
}
$result = $qb->execute()->fetchAll();
$records = [];
foreach ($result as $item) {
$records[] = $this->getEntityFromDatabaseArray($item);
}
return $records;
}
/**
* @param array $where
* @return Emotion
*/
public function findOneBy(array $where): Emotion
{
$qb = $this->connection->createQueryBuilder();
$qb->select('*')
->from(self::TABLE);
foreach ($where as $key => $value) {
$qb->andWhere(sprintf('%s = %s', $key, $qb->createNamedParameter($value)));
}
$result = $qb->execute()->fetch();
if (empty($result)) {
return null;
}
return $this->getEntityFromDatabaseArray($result);
}
/**
* @param int $id
* @return Emotion
*/
public function find(int $id): Emotion
{
return $this->findOneBy(['id' => $id]);
}
/**
* Creates a record in the database.
* @param Emotion $entity
* @return Emotion
*/
public function create(Emotion $entity): Emotion
{
$databaseArray = $this->getDatabaseArrayFromEntity($entity);
$this->connection->insert(
self::TABLE,
$databaseArray
);
$entity->setId($this->connection->lastInsertId());
return $entity;
}
/**
* Update a record in the database.
* @param Emotion $entity
* @return Emotion
*/
public function update(Emotion $entity): Emotion
{
$databaseArray = $this->getDatabaseArrayFromEntity($entity);
$this->connection->update(
self::TABLE,
$databaseArray,
['id' => $entity->getId()]
);
return $entity;
}
/**
* Remove a record in the database.
* @param Emotion $entity
* @return Emotion
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException
*/
public function remove(Emotion $entity): Emotion
{
$this->connection->delete(
self::TABLE,
['id' => $entity->getId()]
);
return $entity;
}
/**
* Maps the given entity to the database array.
* @param Emotion $entity
* @return array
*/
public function getDatabaseArrayFromEntity(Emotion $entity): array
{
$array = $entity->toArray();
foreach($array as &$item) {
if ($item instanceof \DateTime) {
$item = $item->format('Y-m-d H:i:s');
} elseif (is_array($item)) {
$item = json_encode($item);
}
}
return $array;
}
/**
* Prepares database array from properties.
* @param array $data
* @return Emotion
*/
public function getEntityFromDatabaseArray(array $data): Emotion
{
$entity = new Emotion();
$entity->setId((int) $data['id']);
$entity->setActive((int) $data['active']);
$entity->setName((string) $data['name']);
$entity->setCols((int) $data['cols']);
$entity->setCellSpacing((int) $data['cell_spacing']);
$entity->setCellHeight((int) $data['cell_height']);
$entity->setArticleHeight((int) $data['article_height']);
$entity->setRows((int) $data['rows']);
$entity->setValidFrom(new \DateTime($data['valid_from']));
$entity->setValidTo(new \DateTime($data['valid_to']));
$entity->setUserID((int) $data['userID']);
$entity->setShowListing((int) $data['show_listing']);
$entity->setIsLandingpage((int) $data['is_landingpage']);
$entity->setSeoTitle((string) $data['seo_title']);
$entity->setSeoKeywords((string) $data['seo_keywords']);
$entity->setSeoDescription((string) $data['seo_description']);
$entity->setCreateDate(new \DateTime($data['create_date']));
$entity->setModified(new \DateTime($data['modified']));
$entity->setTemplateId((int) $data['template_id']);
$entity->setDevice((string) $data['device']);
$entity->setFullscreen((int) $data['fullscreen']);
$entity->setMode((string) $data['mode']);
$entity->setPosition((int) $data['position']);
$entity->setParentId((int) $data['parent_id']);
$entity->setPreviewId((int) $data['preview_id']);
$entity->setPreviewSecret((string) $data['preview_secret']);
$entity->setCustomerStreamIds((string) $data['customer_stream_ids']);
$entity->setReplacement((string) $data['replacement']);
return $entity;
}
}
<?php
namespace ShyimDatabaseEntitiesGenerator\Models\Emotion;
use ShyimDatabaseEntitiesGenerator\Models\AbstractService;
/**
* Service for table s_emotion
*/
class EmotionService extends AbstractService
{
/** @var EmotionRepository */
private $repository;
/**
* EmotionService constructor.
* @param EmotionRepository $repository
*/
public function __construct(EmotionRepository $repository)
{
$this->repository = $repository;
}
/**
* Fetches all records.
* @param int|null $offset
* @param int|null $limit
* @param array|null $where
* @param array|null $sorters
* @return Emotion[]
*/
public function findBy(int $offset = null, int $limit = null, array $where = null, array $sorters = null): array
{
return $this->repository->findBy($offset, $limit, $where, $sorters);
}
/**
* @param array $where
* @return Emotion
*/
public function findOneBy(array $where): Emotion
{
return $this->repository->findOneBy($where);
}
/**
* @param int $id
* @return Emotion
*/
public function find(int $id): Emotion
{
return $this->repository->find($id);
}
/**
* Creates a record in the database.
* @param Emotion $entity
* @return Emotion
*/
public function create(Emotion $entity): Emotion
{
return $this->repository->create($entity);
}
/**
* Update a record in the database.
* @param Emotion $entity
* @return Emotion
*/
public function update(Emotion $entity): Emotion
{
return $this->repository->update($entity);
}
/**
* Remove a record in the database.
* @param Emotion $entity
* @return Emotion
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException
*/
public function remove(Emotion $entity): Emotion
{
return $this->repository->remove($entity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment