简体   繁体   中英

Doctrine 2 write to BLOB type

I am in need to write to BLOB column with doctrine 2. As documentation suggests BLOB type maps to PHP resource. So I tried fwrite() and it doesn't work.

/**
 * @Entity @Table(name="products")
 **/
class Product
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

    /** @Column(type="blob") **/
    protected $fileResource;

    public function getFileResource() {
       return $this->fileResource;
    }

    // .. (other code)
}

My test case (couldn't find a official example):

$product = new Product();
fwrite($product->getFileResource(), "The Data");
$em->persist($product);
$em->flush();

What is my mistake?

You do not need to write it like a stream. Just use simple assignment (or setter method).

$product = new Product();
$product->setFileResource("The Data");
$em->persist($product);
$em->flush();

The type names listed here equal those that can be passed to the factory method in order to retrieve the desired type instance

<?php

// Returns instance of \Doctrine\DBAL\Types\IntegerType
$type = \Doctrine\DBAL\Types\Type::getType('integer')
?>

May be its help you...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM