Tip: Working with BLOB column

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

Advertisements

2 responses to “Tip: Working with BLOB column

  1. wiggst3r January 12, 2011 at 6:08 pm

    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:

    stream_get_contents() expects parameter 1 to be resource, string given
    

    Is there anyway I can update my code to display the image?

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: