views:

359

answers:

1

Hi,

I'm using Doctrine 1.1.2 as my ORM framework in combination with Zend Framework and a MSSQL server back-end. What I am trying to do is to store a blob to the database, but Doctrine is throwing me errors when I try. The column in the database is of type varbinary(max), and the model in Doctrine is configured as follows:

$this->hasColumn('binary_data', 'blob', null, array(
         'type' => 'blob',
         'notnull' => false,
         'primary' => false,
         'autoincrement' => false,
         ));

This is generated with Doctrine::generateModelsFromDB, and should be correct. The source of the blob is a file upload. I have verified that the file upload succeeds, and that I am able to print the binary data (shows as a bunch of garbled data).

When I try to save the binary data by using the Doctrine model, I get different error messages based on what I do. I have so far tried this (probably some more too that I don't remember):

// Error:SQLSTATE[HY000]: General error: 10007 Incorrect syntax near '%PDF-1.5 %µµµµ...
$model->binary_data = fread(fopen($_FILES["file"]["tmp_name"], 'r'), filesize($_FILES["file"]["tmp_name"]));
$model->save();
---
// Error: same as above
$fileHandle = fopen($_FILES["file"]["tmp_name"], "r");
$fileContent = fread($fileHandle, filesize($_FILES["file"]["tmp_name"]));
$model->binary_data = $fileContent;
$model->save();
---
// This should work on 1.2, but I have 1.1.2
// Error: Validation failed in class Clazz 1 field had validation error: * 1 validator failed on binary_data (type)
$model->binary_data = file($_FILES["file"]["tmp_name"]);
$model->save();

This should be straight forward, but I just can't figure out what I'm doing wrong.

A: 

You can try base64_encode-ing your data prior to saving and base64_decode it after you pull it from the database.

Marko
Will that work with a varbinary column? That would result in string if I'm not mistaken. I could change the column to a varchar(max), but I would rather store it as a varbinary. I'll try it out and see what works. I just can't believe that it's not possible to store blobs into an mssql server with doctrine, so I would really like to see an example of how to do this ;)
Erik
You wrote: "The column in the database is of type varchar(max)" that's why I thought that there's trouble with the binary data. Check your schema, maybe you've got a typo on the column type and have been trying saving binary data into string column.
Marko
Oh sorry, the typo is in the text above. The column type is varbinary(max) in the database.
Erik