2014-08-23 09:54
浏览 16


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');

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

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, '', MCRYPT_MODE_ECB);

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.


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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!

    打赏 评论

相关推荐 更多相似问题