I have an existing SQL table the schema of which I cannot modify, and I would like to create a Lithium data model for it. The problem is that one column contains multiple "fields" separated by a special character.
E.g.
data = "username|email|age"
I would need to:
I tried to figure out how to do this, but there seems to be no easy way. Not even a hard way :) Any ideas?
Magic virtual attributes aren't supported in Lithium out of the box. There is an ongoing work and discussion about that here https://github.com/UnionOfRAD/lithium/pull/569.
That said, you can almost solve this problem with instance methods in your Model + filters on save and find.
username
, email
and age
,
attributes to your entity after reading it.data
attributes before saving the entity, and unsets username
, email
and age
(to avoid saving them).Maybe you'll need Model instance methods too like
public function username($entity) {
/* split here $entity->data by "|" and keep only what do you want */
return $username;
}
Then you can call anywhere $user->username() to get the username only.
That should solve your problem until this feature will be shipped in Lithium.