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

Doctrine2 managed #1

Open
wants to merge 1 commit into
base: doctrine2-managed
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added application/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions application/configs/application.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ resources.doctrine.dbal.connections.default.parameters.port = 3306
resources.doctrine.dbal.connections.default.parameters.user = "root"
resources.doctrine.dbal.connections.default.parameters.password = "password"

resources.doctrine.dbal.connections.default.type.point = "Wantlet\ORM\PointType"


; ------------------------------------------------------------------------------
; Doctrine ORM Configuration
Expand All @@ -82,6 +84,9 @@ resources.doctrine.orm.entityManagers.default.metadataDrivers.0.mappingDirs[]
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.0.annotationReaderCache = default

resources.doctrine.orm.entityManagers.default.DQLFunctions.numeric.DISTANCE = "Wantlet\ORM\Distance"
resources.doctrine.orm.entityManagers.default.DQLFunctions.numeric.POINT_STR = "Wantlet\ORM\PointStr"
resources.doctrine.orm.entityManagers.default.mappings.Point = "point"



Expand Down
Binary file added library/.DS_Store
Binary file not shown.
Binary file added library/Bisna/.DS_Store
Binary file not shown.
Binary file added library/Bisna/Application/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions library/Bisna/Application/Container/DoctrineContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private function prepareDBALConfiguration(array $config = array())
'eventSubscribers' => array(),
'configurationClass' => 'Doctrine\DBAL\Configuration',
'sqlLoggerClass' => null,
'type' => array(),
'parameters' => array(
'wrapperClass' => null,
'driver' => 'pdo_mysql',
Expand Down Expand Up @@ -231,6 +232,7 @@ private function prepareORMConfiguration(array $config = array())
'queryCache' => $this->defaultCacheInstance,
'resultCache' => $this->defaultCacheInstance,
'metadataCache' => $this->defaultCacheInstance,
'mappings' => array(),
'metadataDrivers' => array(),
'DQLFunctions' => array(
'numeric' => array(),
Expand Down Expand Up @@ -383,6 +385,11 @@ private function startDBALConfiguration(array $config = array())
$sqlLoggerClass = $config['sqlLoggerClass'];
$configuration->setSQLLogger(new $sqlLoggerClass());
}

// Custom type configuration
foreach($config['type'] as $key => $value){
\Doctrine\DBAL\Types\Type::addType($key, $value);
}

return $configuration;
}
Expand Down Expand Up @@ -528,6 +535,10 @@ private function startORMConfiguration(array $config = array())
$configuration->addCustomStringFunction($name, $className);
}

foreach($config['mappings'] as $key => $value){
$this->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value);
}

return $configuration;
}

Expand Down
Binary file added library/Wantlet/.DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions library/Wantlet/ORM/Distance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Wantlet\ORM;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;

/**
* DQL function for calculating distances between two points
*
* Example: DISTANCE(foo.point, POINT_STR(:param))
*/
class Distance extends FunctionNode {
private $firstArg;
private $secondArg;

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
//Need to do this hacky linestring length thing because
//despite what MySQL manual claims, DISTANCE isn't actually implemented...
return 'GLength(LineString(' .
$this->firstArg->dispatch($sqlWalker) .
', ' .
$this->secondArg->dispatch($sqlWalker) .
'))';
}

public function parse(\Doctrine\ORM\Query\Parser $parser) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->firstArg = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->secondArg = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
36 changes: 36 additions & 0 deletions library/Wantlet/ORM/Point.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Wantlet\ORM;

/**
* Point object for spatial mapping
*/
class Point {
private $latitude;
private $longitude;

public function __construct($latitude, $longitude) {
$this->latitude = $latitude;
$this->longitude = $longitude;
}

public function setLatitude($x) {
$this->latitude = $x;
}

public function getLatitude() {
return $this->latitude;
}

public function setLongitude($y) {
$this->longitude = $y;
}

public function getLongitude() {
return $this->longitude;
}

public function __toString() {
//Output from this is used with POINT_STR in DQL so must be in specific format
return sprintf('POINT(%f %f)', $this->latitude, $this->longitude);
}
}
27 changes: 27 additions & 0 deletions library/Wantlet/ORM/PointStr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Wantlet\ORM;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;

/**
* POINT_STR function for querying using Point objects as parameters
*
* Usage: POINT_STR(:param) where param should be mapped to $point where $point is Wantlet\ORM\Point
* without any special typing provided (eg. so that it gets converted to string)
*/
class PointStr extends FunctionNode {
private $arg;

public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
return 'GeomFromText(' . $this->arg->dispatch($sqlWalker) . ')';
}

public function parse(\Doctrine\ORM\Query\Parser $parser) {
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->arg = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

}
47 changes: 47 additions & 0 deletions library/Wantlet/ORM/PointType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Wantlet\ORM;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* Mapping type for spatial POINT objects
*/
class PointType extends Type {
const POINT = 'point';

/**
* Gets the name of this type.
*
* @return string
*/
public function getName() {
return self::POINT;
}

/**
* Gets the SQL declaration snippet for a field of this type.
*
* @param array $fieldDeclaration The field declaration.
* @param AbstractPlatform $platform The currently used database platform.
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
return 'POINT';
}

public function convertToPHPValue($value, AbstractPlatform $platform) {
//Null fields come in as empty strings
if($value == '') {
return null;
}

$data = unpack('x/x/x/x/corder/Ltype/dlat/dlon', $value);
return new \Wantlet\ORM\Point($data['lat'], $data['lon']);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform) {
if (!$value) return;

return pack('xxxxcLdd', '0', 1, $value->getLatitude(), $value->getLongitude());
}
}