We don't currently have built-in support for BLOB or CLOB yet and you would need to write a getter and setter with the SQL query in it.
How do I get the database connection, and How do I handle this?
Can you give me an example?
Thanks!
1 public function get_myfield() 2 { 3 $db = self::connection()->connection; 4 $stmt = $db->prepare( 'select myfield from my_table where id = ?' ); 5 $stmt->execute( array( $this->id ) ); 6 $row = $stmt->fetch(); 7 $myfield = stream_get_contents( $row[0] ); 8 return $myfield; 9 }
Here is a sample getter. You can just pull the PDO connection out from the model and go from there. Here is a good resource on LOB with PDO:
http://wezfurlong.org/blog/2005/oct/lob-support-added-to-pdo-oci-in-php-5-1-cvs-finally/
http://www.phpactiverecord.xyz/docs/ActiveRecord/Model#method__get
http://www.phpactiverecord.xyz/docs/ActiveRecord/Model#method__set
Thanks very much!
I ran through the same issue and I've applied a patch to my local version feel free to fix it on your side as well:
Edit Column.php file:
/**
* @package ActiveRecord
*/
namespace ActiveRecord;
/**
* Class for a table column.
*
* @package ActiveRecord
*/
class Column
{
// types for $type
const STRING = 1;
const INTEGER = 2;
const DECIMAL = 3;
const DATETIME = 4;
const DATE = 5;
const TIME = 6;
const LOB = 7;
/**
* Map a type to an column type.
* @static
* @var array
*/
static $TYPE_MAPPING = array(
'datetime' => self::DATETIME,
'timestamp' => self::DATETIME,
'date' => self::DATE,
'time' => self::TIME,
'int' => self::INTEGER,
'tinyint' => self::INTEGER,
'smallint' => self::INTEGER,
'mediumint' => self::INTEGER,
'bigint' => self::INTEGER,
'float' => self::DECIMAL,
'double' => self::DECIMAL,
'numeric' => self::DECIMAL,
'decimal' => self::DECIMAL,
'dec' => self::DECIMAL,
'blob' => self::LOB,
'clob' => self::LOB,
);
/**
* The true name of this column.
* @var string
*/
public $name;
/**
* The inflected name of this columns .. hyphens/spaces will be => _.
* @var string
*/
public $inflected_name;
/**
* The type of this column: STRING, INTEGER, ...
* @var integer
*/
public $type;
/**
* The raw database specific type.
* @var string
*/
public $raw_type;
/**
* The maximum length of this column.
* @var int
*/
public $length;
/**
* True if this column allows null.
* @var boolean
*/
public $nullable;
/**
* True if this column is a primary key.
* @var boolean
*/
public $pk;
/**
* The default value of the column.
* @var mixed
*/
public $default;
/**
* True if this column is set to auto_increment.
* @var boolean
*/
public $auto_increment;
/**
* Name of the sequence to use for this column if any.
* @var boolean
*/
public $sequence;
/**
* Casts a value to the column's type.
*
* @param mixed $value The value to cast
* @param Connection $connection The Connection this column belongs to
* @return mixed type-casted value
*/
public function cast($value, $connection)
{
if ($value === null)
return null;
switch ($this->type)
{
case self::STRING: return (string)$value; break;
case self::INTEGER: return (int)$value; break;
case self::DECIMAL: return (double)$value; break;
case self::DATETIME:
case self::DATE:
if (!$value)
return null;
if ($value instanceof DateTime)
return $value;
if ($value instanceof \DateTime)
return new DateTime($value->format('Y-m-d H:i:s T'));
return $connection->string_to_datetime($value);
break;
case self::LOB:
if(is_resource($value))
{
return stream_get_contents($value);
}
break;
}
return $value;
}
/**
* Sets the $type member variable.
* @return mixed
*/
public function map_raw_type()
{
if ($this->raw_type == 'integer')
$this->raw_type = 'int';
if (array_key_exists($this->raw_type,self::$TYPE_MAPPING))
$this->type = self::$TYPE_MAPPING[$this->raw_type];
else
$this->type = self::STRING;
return $this->type;
}
}
?>
(1-5/5)
Subject: issue about blob type of oracle
table "chapter_content" constructor:
id char,
content BLOB
then
I use the statements
"
$chapter_content= ChapterContent::find('first',array('select'=>'id,content'));
var_dump($chapter_content->content);
"
finally I get the result:
string(15) "Resource id #23"
How do I get the data of content?