Reputation: 1541
Is it possible to have several representations of one db field in Doctrine2 entity?
Live example: I have encrypted object in database field. I've created special data type for decrypting that field on-the-fly. Now I need to have both encrypted and decrypted values in entity after selecting from db.
I've tried to solve that problem this way:
/**
* @var array
* @ORM\Column(type="EncryptedData", name="data")
*/
private $data;
/**
* @var string
* @ORM\Column(type="text", name="data")
*/
private $encryptedData;
But doctrine generates error about duplicate fields.
Upvotes: 0
Views: 206
Reputation: 5280
You are getting the duplicate fields error because both fields are called "data" in your annotations. If you want to have both fields in your database table try something like this:
/**
* @var array
* @ORM\Column(type="text", name="data")
*/
private $data;
/**
* @var string
* @ORM\Column(type="text", name="encrypteddata")
*/
private $encryptedData;
That said, storing sensitive unencrypted data in your database is not a good idea security-wise.
Hope it helps.
Upvotes: 0
Reputation: 1188
Yet in database should be one field? You need something like this:
/**
* @var array
* @ORM\Column(type="EncryptedData", name="data")
*/
private $data;
private $encryptedData;
public function getData()
{
return $this->data;
}
public function getEncryptedData()
{
if ($this->encryptedData === null) {
// do with that data whatever you need
$this->encryptedData = processSomehow($this->data);
}
return $this->encryptedData;
}
Upvotes: 2