Skip to content

Instantly share code, notes, and snippets.

@vespakoen
Created April 2, 2014 14:01
Show Gist options
  • Save vespakoen/9934735 to your computer and use it in GitHub Desktop.
Save vespakoen/9934735 to your computer and use it in GitHub Desktop.
Bolt fields
+ {% if field.type in app.extensions.getFields|keys %}
+ {% include 'custom/fields/' ~ field.type ~ '.twig' %}
+ {% endif %}
+ public function addField($type, $boltType, $doctrineType = null)
+ {
+ $this->app['extensions']->addField($type, $boltType, $doctrineType);
+ }
+ foreach($this->app['extensions']->getFields() as $type => $field) {
+ if($this->fieldtype($key) == $type) {
+ $this->values[$key] = $field->unserialize($this->values[$key]);
+ }
+ }
+use Doctrine\DBAL\Types\Type;
+use Doctrine\DBAL\Platforms\AbstractPlatform;
+ /**
+ * List of registered fields.
+ *
+ * @var array
+ */
+ private $fields = array();
+ public function addField($type, $boltType, $doctrineType = null)
+ {
+ if(class_exists($boltType))
+ {
+ $field = new $boltType($this->app);
+ $this->fields[$type] = $field;
+ }
+
+ if(is_null($doctrineType))
+ {
+ $doctrineType = $field->getDoctrineType();
+ }
+ else
+ {
+ $field->setDoctrineType($doctrineType);
+ }
+
+ $platform = $this->app['db']->getDatabasePlatform();
+ Type::addType($type, $doctrineType);
+
+ // Since doctrineTypeComments may already be initialized check if added type requires comment
+ if (Type::getType($type)->requiresSQLCommentHint($platform)) {
+ $platform->markDoctrineTypeCommented($type);
+ }
+
+ return $this;
+ }
+
+ public function getFields()
+ {
+ return $this->fields;
+ }
<?php
namespace Bolt;
use Bolt\Fields\FieldInteface;
abstract class Field {
protected $app;
protected $doctrineType = 'Doctrine\DBAL\Types\StringType';
public function __construct($app)
{
$this->app = $app;
}
public function getDoctrineType()
{
return $this->doctrineType;
}
public function setDoctrineType($type)
{
$this->doctrineType = $type;
}
}
<?php
namespace Bolt\Fields;
interface FieldInterface {
public function serialize($value);
public function unserialize($value);
}
+ foreach($this->app['extensions']->getFields() as $type => $field) {
+ if($values['type'] == $type) {
+ $fieldvalues[$key] = $field->serialize($fieldvalues[$key]);
+ }
+ }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment