Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom database types not recognized. #257

Closed
tschallacka opened this issue Oct 31, 2018 · 3 comments
Closed

Custom database types not recognized. #257

tschallacka opened this issue Oct 31, 2018 · 3 comments

Comments

@tschallacka
Copy link

I have a geometry type added via extended grammar.

<?php namespace ExitControl\PageManager\Support\Grammar;

use Illuminate\Database\Schema\Grammars\MySqlGrammar;
use Illuminate\Support\Fluent;

/**
 * Extended version of MySqlGrammar with
 * support of 'set' data type
 */
class MysqlExtendedGrammar extends MySqlGrammar {

    /**
     * Create the column definition for an 'set' type.
     *
     * @param  \Illuminate\Support\Fluent  $column
     * @return string
     */
    public function typeBuuid(Fluent $column)
    {
        return "varbinary(16)";
    }
    public function typeGeometry(Fluent $column) {
        return 'geometry';
    }
}

and the blueprint

<?php namespace ExitControl\PageManager\Support\BluePrint;

use Illuminate\Database\Schema\Blueprint;

class ExtendedBlueprint extends BluePrint
{
        /**
	 * Create a new uuid column on the table.
	 *
	 * @param  string  $column
	 * @return \Illuminate\Support\Fluent
	 */
	public function binary_uuid($column)
	{
		return $this->addColumn('buuid', $column);
		
	}

	public function geometry($column)
	{
	    return $this->addColumn('geometry', $column);
	    
	}
}

And my stutter method for inserting things into the database with new typefields

<?php namespace ExitControl\PageManager\Support\Helper;

use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
use ExitControl\PageManager\Support\BluePrint\ExtendedBlueprint;
use Db;
use Event;

/**
 * Not so eloquent ;-)
 * @author tschallacka
 *
 */
class Stutter {
	
	private  $transforms = [
			'mysql' 	=> 'ExitControl\PageManager\Support\Grammar\MysqlExtendedGrammar',
			'postgres' 	=> 'ExitControl\PageManager\Support\Grammar\PostgesExtendedGrammar',
			'sqlite' 	=> 'ExitControl\PageManager\Support\Grammar\SqlLiteExtendedGrammar',
			'sqlsrv' 	=> 'ExitControl\PageManager\Support\Grammar\SqlServerExtendedGrammar',
	];
	
	/**
	 * Set the grammar to a certain driver
	 * @param string $driver mysql, postgres, sqlite, sqlsrv, something else
	 * @param string $grammar Your extended grammar class '\Foo\Bar\MysqlExtendedGrammar'
	 */
	public function setGrammar($driver, $grammar) 
	{
		$this->transforms[$driver] = $grammar;	
	}
	
	public function getGrammar($driver) {
		if(array_key_exists($driver, $this->transforms)) {
			return $this->transforms[$driver];
		}
		throw new UnexpectedValueException("Unsupported database driver $driver\n"
				."Please attach a listener to event tschallacka.get.binary_uuid.grammars\n"
				."and provide an extended grammar for for generating 16 bit binary fields for UUIDs.\n"
				."Take a look at /plugins/tschallacka/dynamicpages/support/MysqlExtendedGrammar.php");
	}
	
	public static function tableName($table_name) {
		$prefix = DB::connection()->getTablePrefix();
		return $prefix . $table_name;
	}
	public static function getExtendedSchema() 
	{
		$stutter = new static();
		Event::fire('tschallacka.get.binary_uuid.grammars',[$stutter]);
		$driver = DB::connection()->getConfig('driver');
		$grammar = $stutter->getGrammar($driver);
		
		DB::connection()->setSchemaGrammar(new $grammar());
		
		$schema = DB::connection()->getSchemaBuilder();
		$schema->blueprintResolver(function($table, $callback) {
			return new ExtendedBlueprint($table, $callback);
		});
		
		return $schema;
	}
}

which is used as

public function up()
    {
        $schema = Stutter::getExtendedSchema();
        $this->log('creating table geo_city');
        
        $schema->create(Stutter::tableName('geo_city'),
            function(ExtendedBlueprint $table) {
            $table->engine = 'InnoDB';

            $table->increments('id');
            $table->geometry('geo_poly')->nullable();

but when I wish to use the builder to add a new model or anything really I get this error:

image

Which is quite annoying. Is there a way to suppress this error or is there an event to register the extended grammars to the builder?

@LukeTowers
Copy link
Contributor

@tschallacka I'd assume you'd do it around

protected static function getSchemaManager()
? If so, feel free to submit a PR with an event to be added and I'll review it

@sardykoivan
Copy link

sardykoivan commented Apr 5, 2019

when use postgresql, creating database table, appears exception, i solve this, when add $platform->registerDoctrineTypeMapping('txid_snapshot', 'text'); to method from previous comment
image

@daftspunk
Copy link
Member

This can be achieved without modification to this plugin. Try this:

Db::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('txid_snapshot', 'text');

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants