This article has moved to this location.
Let’s say we have the following XML Schema:
<table name="product_image">
<column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
<column name="name" type="varchar" required="true" size="255" />
<column name="filename" type="blob" required="true" />
<column name="description" type="longvarchar" required="true" />
<column name="extension" type="varchar" size="5" required="true" />
</table>
How to read and write
blob type using
Propel?
- Define the widget and validator for filename field. Here I use sfWidgetFormInputFileEditable widget.
$this->setWidget('filename', new sfWidgetFormInputFileEditable(array(
'file_src' => $this->getObject()->getFilenameSource(),
'edit_mode' => !$this->isNew(),
'is_image' => true,
'with_delete' => true,
)));
$this->setValidator('filename', new sfValidatorFile(array(
'mime_types' => 'web_images',
'required' => false,
)));
In the validator above, we leave the path options is empty, because we store the contents of the image in the database.
-
When setting a BLOB column, you can either pass in a stream or the blob contents.
Override the updateXXXColumn, where XXX is the PHP name of the Propel column.
public function updateFilenameColumn($value)
{
if (!$value) return false;
$this->getObject()->setExtension($value->getOriginalExtension());
return file_get_contents($value->getTempName());
}
The parameter value passed to updateFilenameColumn is an instance of sfValidatedFile
-
The getFilenameSource() function, is a function to read the stream resource, and store the reading result in the specified directory.
public function getFilenameSource()
{
$file_src = false;
if (null !== $fp = $this->getFilename())
{
$file = stream_get_contents($fp);
$file_src = '/uploads/product/'.$this->getId().$this->getExtension();
file_put_contents(sfConfig::get('sf_upload_dir').'/product/'.$this->getId().$this->getExtension(), $file);
}
return $file_src;
}
Should we store media file in the database or whether to store it on the filesystem and just store the reference in the database? The answer is based on your purpose.
I hope it useful.. thanks you
Advertisement
Like this:
Be the first to like this post.
Hi
Thanks for this. I am also looking to show image data, stored as a blob in a template.
I have followed your code, but I do not wish to store the image on the file system.
I have the following code:
public function getImagesPath() { $file_src = false; $fp = $this->getFilename(); if (null !== $fp = $this->getFilename()) { $file = stream_get_contents($fp); $file_src = '/uploads/images/'.$this->getId().'.jpg'; } return $file_src; }The problem I have, is that it is throwing an error:
Is there anyway I can update my code to display the image?
Thanks
Make sure the type of column is blob not clob, because clob type will be treated as string .
Have a look at this forum http://forum.symfony-project.org/viewtopic.php?f=22&t=31207