Created
August 15, 2010 03:36
-
-
Save romansklenar/525030 to your computer and use it in GitHub Desktop.
Doctrine 2, BLOB datatype implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
* | |
* This software consists of voluntary contributions made by many individuals | |
* and is licensed under the LGPL. For more information, see | |
* <http://www.doctrine-project.org>. | |
*/ | |
namespace Doctrine\DBAL\Types; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
/** | |
* Type that maps a PHP binary data to a blob SQL type. | |
* | |
* @since 2.0 | |
* @author Roman Sklenar <[email protected]> | |
*/ | |
class Blob extends Type | |
{ | |
const BLOB = 'blog'; | |
public function getName() | |
{ | |
return self::BLOB; | |
} | |
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | |
{ | |
return $platform->getDoctrineTypeMapping('BLOB'); | |
} | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
return ($value === null) ? null : base64_encode($value); | |
} | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
return ($value === null) ? null : base64_decode($value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
$em = Doctrine\ORM\EntityManager::create($conn, $config, $evm); | |
// types registration | |
Doctrine\DBAL\Types\Type::addType('blob', 'Doctrine\DBAL\Types\Blob'); | |
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'blob'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Entities; | |
/** | |
* @Entity | |
* @Table(name="Photos") | |
*/ | |
class Photo extends Base | |
{ | |
/** @Column(type="integer") @Id @GeneratedValue */ | |
protected $id; | |
// ... | |
/** @Column(type="blob") */ | |
protected $data; | |
/** | |
* Converts instance to Nette\Image. | |
* | |
* @return Image | |
*/ | |
public function toImage() { | |
return Nette\Image::fromString($this->data); | |
} | |
} |
@Monomachus
It worked like magic! :)
Thank You for your time.
Appreciate it.
Avoid instanciating the entity manager and check type:
$conn = $this->container->get('doctrine')->getConnection();
// If you have multiple connections, iterate over get('...')->getConnections();
if (!Type::hasType('blob')) {
Type::addType('blob', 'YOURDOMAIN\YOURBUNDLE\Doctrine\DBAL\Type\BlobType');
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');
}
We have had problems using this implementation when we tried to duplicate entries in the database by copying/cloning. The operation always resulted in a 0-sized entry in the blob field.
Once you add an is_file()-check and decide from there it was working however.
How I can get value from parameters.yml in Class *** extended Type? I need to take public key for RSA encrypt.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@VishwaKumar
hmmm hadn't this exact situation. But yeah I think you are right.
I think you now have to do it like this in both your bundles. Like that you verify that it is not already registered.