dongqiancui9194 2014-08-23 09:54
浏览 17
已采纳

在Symfony2中使用DataFixtures保留二进制数据

I'm in the process of refactoring a project and have decided to use Symfony2 as my framework. I'm using the same database schema as I was previously but I'm having difficulty creating some test data using DataFixtures and Doctrine2.

The database field in question will hold an encrypted email field, which is being stored as VARBINARY and defined as:

/**
* @ORM\Column(type="string", columnDefinition="VARBINARY(254) NOT NULL")
*/
protected $email;

Note - I know this isn't portable but binary fields aren't yet available in a stable DBAL version.

In my data fixture I'm doing:

$user1 = new User();
$user1->setName('Joe Bloggs');
$user1->setEmail('test@test.com');
...
$manager->persist($user1);
...
$manager->flush();

Which understandably is generating the following error (I've substitued the class path etc for readability):

[Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to {MyClass}::setEmail() must be an instance of binary, string given

I've tried to encrypt the string (using the same process I did previously) but that's still just returning a string:

$encEmail=\mcrypt_encrypt(MCRYPT_RIJNDAEL_128, ENCKEY, 'test@test.com', MCRYPT_MODE_ECB);
$user1->setEmail($encEmail);

I've tried to pack the output into a binary string and I've tried casting it to a binary string but neither have worked.

I know I can manually create the test data directly into the database but in the interests of learning, I would like to get the data fixtures wokring.

Thanks.

  • 写回答

1条回答 默认 最新

  • douningle7944 2014-08-23 15:56
    关注

    After a lot of head scratching I've solved this. So if anyone else has a similar issue - this is what worked for me...

    Can't believe I missed it but it turns out the 'problem' was caused by the mutators that were generated by doctrine and were expecting a binary argument:

    /**
    * Set email
    *
    * @param \binary $email
    * @return User
    */
    public function setEmail(\binary $email)
    {
        $this->email = $email;
    
        return $this;
    }
    

    So I've changed it (and the other fields that are going to be binary) to accept a string and cast it to a binary before returning:

    public function setEmail($email)
    {
        $this->email = (binary)$email;
    
        return $this;
    }
    

    Data is now persisted to the database as expected.

    I'm not 100% sure if I even need to cast to binary before returning. If I don't, the values still get stored in the database and on inspection - look correct. But I feel like I should as that's what the database is expecting!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算