简体   繁体   English

如何将Blob数据添加到Phalcon \\ Mvc \\ Model

[英]How to add blob data to Phalcon\Mvc\Model

I have a class extending Phalcon\\Mvc\\Model that includes a blob field. 我有一个扩展Phalcon \\ Mvc \\ Model的类,其中包括一个blob字段。 I'm currently with Phalcon 1.2.1. 我目前在使用Phalcon 1.2.1。

how do i read data? 我如何读取数据? I tried the following 我尝试了以下

ModelClass::findFirst(
  array(
    "name = :name: AND blob = :blob:",
    "bind" => array(
       "name" => $name,"blob" => $base64
    )
  )
)

I also don't know how to write the blob. 我也不知道怎么写blob。 but this process should be equal. 但是这个过程应该是平等的。

The "work with models" guide didn't help me either. “使用模型” 指南也没有帮助我。

I found the answer myself by testing. 我通过测试自己找到了答案。 Phalcon seems to work with the same raw datatypes like mysql. Phalcon似乎可以使用与mysql相同的原始数据类型。 For example there are no boolean values. 例如,没有布尔值。 You need to store 0 or 1 for tinyint. 您需要为tinyint存储0或1。 The same problem with blob values. Blob值也有同样的问题。 You need to pass a binary string like: 您需要传递一个二进制字符串,例如:

$hex = "22aabb332299";
ModelClass::findFirst(
  array(
    "name = :name: AND blob = :blob:",
    "bind" => array(
       "name" => $name,"blob" => pack("H*",$hex)
    )
  )
)

It might not be the perfect way but worked for me when it comes to this problem. 这可能不是一个完美的方法,但在解决此问题时对我有用。

This way you can store image data or other files directly like 这样,您可以直接存储图像数据或其他文件,例如

ModelClass::findFirst(
  array(
    "name = :name: AND blob = :blob:",
    "bind" => array(
       "name" => $name,"blob" => file_get_contents($path)
    )
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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