Enumerated table row ID in Codeigniter - php

Let's suppose there is a table user
id_user | name | id_state
1 | John | 2
and a table user_state
id_status | description
2 | User Active
using Codeigniter, what is the best way to do something related to that id_status, without having integer hardcoded in my code??
example:
if($user->id_status == 2){
// do something
}
..
actually i have used in other projects a class to create "enums":
Enum::Create('UserState', 'inactive', 'active', 'banned', 'deleted');
and them..
echo UserState::GetDatabaseID(UserState::active); // result: 2
.. and i think it is a good solution, but i have never used it in a Codeigniter project

I would make a method in the model that queries both tables and returns an object with the status attribute so you can access the name of the status. I usually create a name or slug field as a lowercase name with no spaces to reference in my code and a *display_name* field for display in views.
So in your model, do something like this:
public function get_user_info($user_id)
{
return $this->db
->select('user.*, user_state.*, user_state.id AS user_state_id') // to avoid a conflicts when referencing the ID
->where('id', $user_id)
->join('user_state', 'user_state.id = user.id_state')
->get('user')
->result();
}
Then you will have access to $user->status->slug in your controller to reference 'active' or whatever status you want, and then $user->status->display_name in your views to print out a user-friendly name.

If this user_state table is small enough (i.e. < 10 states) I would consider just putting an array right down in your application. So define some high level property in your My_Model for instance that says:
$this->user_states = array(
'active'=>2,
'deleted'=>3,
'new'=>4,
'verified'=>5
);
Then later in your code you just use something like
if ($this->id_status == $this->user_states['active']) {
}

If the user_state table (almost) never changes and only has a few records, you may consider removing it from the database completely and just using config values.

Related

Using Laravel whenLoaded() deeper than one level

How do I use whenLoaded() for deeper than one level relations? It seems as if one can only use whenLoaded with the first relation depth like this:
'season' => $this->whenLoaded('origin', function () {
return new SeasonResource($this->origin->season);
}),
But then Laravel, if origin is loaded, but not season, Laravel will load it, which creates an N+1 problem. Both origin and season relationships are conditional, and not always used. Therefor I want to use something like this:
$this->whenLoaded('origin.season', ...)
or this:
'season' => $this->whenLoaded('origin', function () {
return new SeasonResource($this->origin->whenLoaded('season'));
}),
Neither of these work. I suppose the deeper relations are not stored on the model itself, and in the second case, whenLoaded() does not exists on the query builder.
How do I use whenLoaded() for deeper than one level relations?
I believe the reason this is not implemented is because it only makes sense for hasOne and belongsTo relationships (relationships that return an object, not a collection).
But if that is the case for you, you could do this:
'season' => $this->when(
$this->relationLoaded('origin') &&
$this->origin->relationLoaded('season'),
function () {
return new SeasonResource($this->origin->season);
}
),
Basically, use ->when instead of ->whenLoaded and manually check if the relation is loaded using the ->relationLoaded public method on the model.
For anyone who comes across this and looking for many to many relationships, I've found a solution.
Let's say you've something like
User ---> user_lived_cities ----> city
with table structures as
user_lived_cities
user_id | city_id
------ | ------
cities
id | name
------ | ------
You want to load the cities the user lived in with something like
User::with('livedCities.city')->take(n)->get();
In the resource, you can manipulate the values like this
$livedCityRelation = $this->whenLoaded('livedCities');
$livedCities = $this->when(!empty((array)$livedCityRelation), function () use ($livedCityRelation) {
if ($livedCityRelation instanceof MissingValue) return []; // the relation has no value
$relationLoaded = count($livedCityRelation) /* Remember: It's a collection */ && $livedCityRelation[0]->relationLoaded('city');
return $relationLoaded ? $livedCityRelation->pluck('city') : []; // you can manipulate as per you need. I'm just extracting the city relation from here
});
In your resource in toArray method, you can return
return [
...,
'lived_cities' => City::collection($livedCities),
];
Remember, $livedCities is basically a collection at this point and you can manipulate it as you wish.
If I want to just return an array of city names, I can do that as well with this
$livedCities->pluck('name')
You can apply any collection method that you want to manipulate as per your wish.
Don't forget to import this at the top
use Illuminate\Http\Resources\MissingValue;
You can check for each nested relationship in turn. For example:
$postsLoaded = $user->relationLoaded('posts');
$commentsLoaded = $user->posts->first()->relationLoaded('comments');
$likesLoaded = $user->posts->first()->comments->first()->relationLoaded('likes');
This isn't my solution but it solved my problem - posting so it may help others as this was hard to find. All credit goes to SaeedPrez:
https://laracasts.com/discuss/channels/eloquent/this-relationloaded-for-distant-relationships

Combine two tables into one result set with Laravel Eloquent. merge/append/join

I have an expenses and expense_splits table.
I'd like to combine both into one result set. Every expense_splits should have added attributes from the expense it belongs to. It seems like there is a lot of ways to go about this but none seem just right.
For example, a leftjoin like this almost works for me but it deletes all my expenses id #s and only keeps expense_splits.
$expenses = DB::table('expenses')
-leftJoin('expense_splits', 'expenses.id', '=', 'expense_splits.expense_id')
->get();
Ideally, I need all attributes from my expense table to show in my expense_splits result. Unless of course attributes are the same on both tables, then show expense_splits attribute (which a jelftjoin does for me..but it deletes my expenses.id).
I can also merge/push my two collections. This is a quick way to show them within one result set. However, I'm not sure how to add expense attributes to each expense_splits. I need my expense_splits to be treated as a regular expense (and if an expense has splits I can't have it shown..but i'll worry about that later). There will never be a time where I don't need this anywhere on my app.
$splits = ExpenseSplit::all();
$expenses = Expense::all();
$expenses = $expenses->merge($splits);
I can also append to my ExpenseSplits result, but, honestly, idk where to place this or if it even makes sense. I'd need it placed somewhere where it's called everytime I call Expense::all() and I can't copy & paste all over the app.
$expense_split = ExpenseSplit::find(34);
$expense_split->receipt = $expense_split->expense->receipt;
$expense_split->receipt_html = $expense_split->expense->receipt_html;
$expense_split->check_id = $expense_split->expense->check_id;
What is the best place to place this code so it's avaliable as simply as Expense::all() and these splits from my expense_splits table will be included. And of course, how do I go about doing this in the first place. It seems there's a lot of ways to get this done.
Thanks, Patryk
Database schema:
EXPENSES TABLE: (SIMPLIFIED)
| id | expense_date | amount | project_id | receipt | paid_by | check_id
EXPENSE_SPLITS TABLE: (SIMPLIFIED)
| id | expnese_id | amount | project_id |
So I'm not sure how you've done your models but this is an example of what it should look like:
class Expenses extends Model
{
protected $table = 'expenses';
public function splitExpenses()
{
return $this->hasMany('App\Models\ExpenseSplits');
}
}
class ExpenseSplits extends Model
{
protected $table = 'expense_splits';
public function expenses()
{
return $this->belongsTo('App\Models\Expenses');
}
}
$expenses = Expenses::where('id', 1)->with('splitExpenses')->get();
So essentially your $expenses should have all the split expense objects if one exists..
Read up on:
https://laravel.com/docs/5.5/eloquent-relationships
There is no need to use any DB::table since you're using Eloquent. It takes care of everything for you if you set up the relationships correctly.

Yii active record relation limit to one record

I am using PHP Yii framework's Active Records to model a relation between two tables. The join involves a column and a literal, and could match 2+ rows but must be limited to only ever return 1 row.
I'm using Yii version 1.1.13, and MySQL 5.1.something.
My problem isn't the SQL, but how to configure the Yii model classes to work in all cases. I can get the classes to work sometimes (simple eager loading) but not always (never for lazy loading).
First I will describe the database. Then the goal. Then I will include examples of code I've tried and why it failed.
Sorry for the length, this is complex and examples are necessary.
The database:
TABLE sites
columns:
id INT
name VARCHAR
type VARCHAR
rows:
id name type
-- ------- -----
1 Site A foo
2 Site B bar
3 Site C bar
TABLE field_options
columns:
id INT
field VARCHAR
option_value VARCHAR
option_label VARCHAR
rows:
id field option_value option_label
-- ----------- ------------- -------------
1 sites.type foo Foo Style Site
2 sites.type bar Bar-Like Site
3 sites.type bar Bar Site
So sites has an informal a reference to field_options where:
field_options.field = 'sites.type' and
field_options.option_value = sites.type
The goal:
The goal is for sites to look up the relevant field_options.option_label to go with its type value. If there happens to be more than one matching row, pick only one (any one, doesn't matter which).
Using SQL this is easy, I can do it 2 ways:
I can join using a subquery:
SELECT
sites.id,
f1.option_label AS type_label
FROM sites
LEFT JOIN field_options AS f1 ON f1.id = (
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
)
Or I can use a subquery as a column reference in the select clause:
SELECT
sites.id,
(
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
) AS type_label
FROM sites
Either way works great. So how do I model this in Yii??
What I've tried so far:
1. Use "on" array key in relation
I can get a simple eager lookup to work with this code:
class Sites extends CActiveRecord
{
...
public function relations()
{
return array(
'type_option' => array(
self::BELONGS_TO,
'FieldOptions', // that's the class for field_options
'', // no normal foreign key
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = t.type LIMIT 1)",
),
);
}
}
This works when I load a set of Sites objects and force it to eager load type_label, e.g. Sites::model()->with('type_label')->findByPk(1).
It does not work if type_label is lazy-loaded.
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // ERROR: column t.type doesn't exist
2. Force eager loading always
Building on #1 above, I tried forcing Yii to always to eager loading, never lazy loading:
class Sites extends CActiveRecord
{
public function relations()
{
....
}
public function defaultScope()
{
return array(
'with' => array( 'type_option' ),
);
}
}
Now everything always works when I load Sites, but it's no good because there are other models (not pictured here) that have relations that point to Sites, and those result in errors:
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // works now
$other = OtherModel::model()->with('site_relation')->findByPk(1); // ERROR: column t.type doesn't exist, because 't' refers to OtherModel now
3. Make the reference to the base table somehow relative
If there was a way that I could refer to the base table, other than "t", that was guaranteed to point to the correct alias, that would work, e.g.
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = %%BASE_TABLE%%.type LIMIT 1)",
where %%BASE_TABLE%% always refers to the correct alias for table sites. But I know of no such token.
4. Add a true virtual database column
This way would be the best, if I could convince Yii that the table has an extra column, which should be loaded just like every other column, except the SQL is a subquery -- that would be awesome. But again, I don't see any way to mess with the column list, it's all done automatically.
So, after all that... does anyone have any ideas?
EDIT Mar 21/15: I just spent a long time investigating the possibility of subclassing parts of Yii to get the job done. No luck.
I tried creating a new type of relation based on BELONGS_TO (class CBelongsToRelation), to see if I could somehow add in context sensitivity so it could react differently depending on whether it was being lazy-loaded or not. But Yii isn't built that way. There is no place where I can hook in code during query buiding from inside a relation object. And there is also no way I can tell even what the base class is, relation objects have no link back to the parent model.
All of the code that assembles these queries for active records and their relations is locked up in a separate set of classes (CActiveFinder, CJoinQuery, etc.) that cannot be extended or replaced without replacing the entire AR system pretty much. So that's out.
I then tried to see if I can create "fake" database column entries that would actually be a subquery. Answer: no. I figured out how I could add additional columns to Yii's automatically generated schema data. But,
a) there's no way to define a column in such a way that it can be a derived value, Yii assumes it's a column name in way too many places for that; and
b) there also doesn't appear to be any way to avoid having it try to insert/update to those columns on save.
So it really is looking like Yii (1.x) just does not have any way to make this happen.
Limited solution provided by #eggyal in comments: #eggyal has a suggestion that will meet my needs. He suggests creating a MySQL view table to add extra columns for each label, using a subquery to look up the value. To allow editing, the view would have to be tied to a separate Yii class, so the downside is everywhere in my code I need to be aware of whether I'm loading a record for reading only (must use the view's class) or read/write (must use the base table's class, does not have the extra columns). That said, it is a workable solution for my particular case, maybe even the only solution -- although not an answer to this question as written, so I'm not going to put it in as an answer.
OK, after a lot of attempts, I have found a solution. Thanks to #eggyal for making me think about database views.
As a quick recap, my goal was:
link one Yii model (CActiveRecord) to another using a relation()
the table join is complex and could match more than one row
the relation must never join more than one row (i.e. LIMIT 1)
I got it to work by:
creating a view from the field_options base table, using SQL GROUP BY to eliminate duplicate rows
creating a separate Yii model (CActiveRecord class) for the view
using the new model/view for the relation(), not the original table
Even then there were some wrinkles (maybe a Yii bug?) I had to work around.
Here are all the details:
The SQL view:
CREATE VIEW field_options_distinct AS
SELECT
field,
option_value,
option_label
FROM
field_options
GROUP BY
field,
option_value
;
This view contains only the columns I care about, and only ever one row per field/option_value pair.
The Yii model class:
class FieldOptionsDistinct extends CActiveRecord
{
public function tableName()
{
return 'field_options_distinct'; // the view
}
/*
I found I needed the following to override Yii's default table data.
The view doesn't have a primary key, and that confused Yii's AR finding system
and resulted in a PHP "invalid foreach()" error.
So the code below works around it by diving into the Yii table metadata object
and manually setting the primary key column list.
*/
private $bMetaDataSet = FALSE;
public function getMetaData()
{
$oMetaData = parent::getMetaData();
if (!$this->bMetaDataSet) {
$oMetaData->tableSchema->primaryKey = array( 'field', 'option_value' );
$this->bMetaDataSet = TRUE;
}
return $oMetaData;
}
}
The Yii relation():
class Sites extends CActiveRecord
{
// ...
public function relations()
{
return (
'type_option' => array(
self::BELONGS_TO,
'FieldOptionsDistinct',
array(
'type' => 'option_value',
),
'on' => "type_option.field = 'sites.type'",
),
);
}
}
And all that does the trick. Easy, right?!?

Store static read only data, in database or in code?

Suppose I have an Employees data.
Each Employee has its employee type, e.g.: Part-timer, Internee, Manager.
Those types are fixed and used throughout the entire application, and its purpose is read-only.
The question is, should I store those employee types into database, like this :
table employee_type
ID | Name
--------------------
1 | Part-timer
2 | Internee
3 | Manager
or inside the code, as a static constant variables (or enums), like this in PHP code :
class Employee
{
const EMPLOYEE_TYPE_PARTTIMER = 1;
const EMPLOYEE_TYPE_INTERNEE = 2;
const EMPLOYEE_TYPE_MANAGER = 3;
...
...
}
The advantage of using the code is, you can use those employee types like this
$employee = new Employee( Employee::EMPLOYEE_TYPE_MANAGER );
...
...
if ( $employee->getType() == Employee::EMPLOYEE_TYPE_PARTTIMER )
{ ... }
So, which one is better ?
Or maybe there is a better solution than both of them ?
If you have (or may have) other tables in your database in which you store the ID, I'd store the definition in a table to enable integrity constraints for the children rows in other tables. If you want to, you could also use the constants, but this redundancy is a possible source of mistakes because you could end with different values in source and in database
I would suggest you make it into the database. and make a panel/form when you want to change the status of an employee. It will make the application robust and more dynamic.
But like you said in here
"Those types are fixed and used throughout the entire application, and its purpose is read-only."
Static data = Constant Value
Dynamic data = Database
Still i prefer the database.

Database design: Matching sql database keys to php constants?

Well this is a simple design question I've wondered about many times and never found a satisfying solution for. My example is with php-sql, but this certainly applies to other languages too.
I have a small database table containing only very few entries, and that almost never needs updating. eg this usertype table:
usertype_id (primary key) | name | description
---------------------------+------------+-------------------
1 | 'admin' | 'Administrator'
2 | 'reguser' | 'Registered user'
3 | 'guest' | 'Guest'
Now in the php code, I often have to check or compare the type of user I'm dealing with. Since the user types are stored in the database, I can either:
1) Select * from the usertype table at class instantiation, and store it in an array.
Then all the ids are available to the code, and I can do a simple select to get the rows I need. This solution requires an array and a db query every time the class is instantiated.
$query = "SELECT info, foo FROM user WHERE usertype_id = ".$usertypes['admin'];
2) Use the name column to select the correct usertype_id, so we can effectively join with other tables. This is more or less equivalent to 1) but without needing to cache the whole usertype table in the php object:
$query = "SELECT info, foo FROM user JOIN usertype USING (usertype_id) WHERE usertype.name = 'admin' ";
3) Define constants that match the keys in the usertype table:
// As defines
define("USERTYPE_ADMIN",1);
define("USERTYPE_REGUSER",2);
//Or as class constants
const USERTYPE_ADMIN = 1;
const USERTYPE_REGUSER = 2;
And then do a simple select.
$query = "SELECT info, foo FROM user WHERE usertype_id = " . USERTYPE_ADMIN;
This is probably the most resource-efficient solution, but it is bad to maintain, as you have to update both the table and the code if you need to modify something in the usertype table..
4) Scrap the usertype table and only keep the types in the php code. I don't really like this because it lets any value get into the database and get assigned to the type of user. But maybe, all things considered, it isn't so bad and i'm just complicating something that should be simple..
Anyways, to sum it up the solution I like most is #2 because it's coherent and with an index on usertype.name, it can't be that bad. But what I've often ended up using is #3, for efficiency.
How would you do it? Any better solutions?
(edit: fixed query in #2)
I would suggest #3 to avoid useless queries, and prevent risk of behavior changes if existing DB table rows are incidentally modified:
Adding the necessary constants in the model class:
class Role // + use namespaces if possible
{
// A good ORM could be able to generate it (see #wimvds answer)
const ADMIN = 1;
const USER = 2;
const GUEST = 3;
//...
}
Then querying like this makes sense:
$query = "SELECT info, foo FROM user WHERE role_id = ".Role::ADMIN;
With an ORM (e.g. Propel in the example below) you'll end up doing:
$isAdminResults = UserQuery::create()->filterByRoleId(Role::ADMIN);
I almost always go for option 3). You could generate the code needed automatically based on what is available in the DB. The only thing you have to remember then is that you have to run the script to update/rewrite that info when you add another role (but if you're using phing or a similar build tool to deploy your apps, just add a build rule for it to your deploy script and it will always be run whenever you deploy your code :p).
Why not denormalize the DB table so instead of having usertype_id, you'd have usertype with the string type (admin). Then in PHP you can just do define('USERTYPE_ADMIN', 'admin');. It saves you from having to modify two places if you want to add a user type...
And if you're really worried about any value getting in, you could always make the column an ENUM data type, so it would self manage...
For tables that will contain "type" values especially when is expected such table to change over time I tend to use simple approach:
Add Varchar column named hid (comes from "human readable id") with unique key. Then I fill it with id meaningful to humans like:
usertype_id (primary key) | name | description | hid (unique key)
---------------------------+------------+-------------------+---------------
1 | 'admin' | 'Administrator' | 'admin'
2 | 'reguser' | 'Registered user' | 'user'
3 | 'guest' | 'Guest' | 'guest'
When you need the actual id you will have to do select based on hid column, i.e.
select usertype_id from tablename where hid = "admin"
This is not an efficient approach but it will ensure compatibility of your application among different deployments (i.e. one client may have 1.admin, 2. guest; other client 1.admin, 2. user, etc.). For your case I think #3 is pretty suitable but if you expect to have more than 10 different user roles - try the "hid" approach.
Are you using any kind of framework here? Could these values be stored in a single source - a config file - which both creates a list of the objects in PHP and also populates the table when you bootstrap the database? I'm thinking from a Rails perspective, as it's been a while since I've written any PHP. Solution there would probably be fixtures.
Why not to make it just
foreach (getdbarr("SELECT * FROM usertype") as $row) {
define($row['name'],$row['id']);
}
You shouldn't need a JOIN in every query to fetch the information about types/roles. You can keep your 'user' model and 'role' models separate in the data access objects (DAO) -- especially since there are so few records for user types.
In most cases where I have a limited number of options that I'd otherwise be joining against a large table, I cache them in memcached as an associative array. In the event I need some information about a particular relationship (like a role) I just lazy load it.
$user = DAO_User::get(1); // this pulls a JOIN-less record
$role = $user->getRole(); // lazy-load
The code for $user->getRole() can be something like:
public function getRole() {
// This comes from a cache that may be called multiple
// times per request with no penalty (i.e. store in a registry)
$roles = DAO_UserRoles::getAll();
if(isset($roles[$this->role_id]))
return $roles[$this->role_id];
return null; // or: new Model_UserRole();
}
This also works if you want to display a list with 1000 users on it. You can simply render values for that column from a single $roles associative array.
This is a major performance improvement on the SQL end, and it goes a long way to reducing complexity in your code base. If you have several other foreign keys on the user table you can still use this approach to grab the necessary information when you need it. It also means you can have dependable Model_* classes without having to create hybrids for every possible combination of tables you might JOIN -- which is much better than simply getting a result set, iterating it, and freeing it.
Even with more than 100 rows on both sides of your JOIN, you can still use the lazy load approach for infrequent or highly redundant information. With a reasonable caching service in your code, there's no penalty for calling DAO_UserRole::get(1500) multiple times because subsequent calls during the same request shouldn't hit the database twice. In most cases you're only going to be displaying 10-25 rows per page out of 1000s, and lazy loading will save your database engine from having to JOIN all the extraneous rows before you actually need them.
The main reason to do a JOIN is if your WHERE logic requires it, or if you need to ORDER BY data from a foreign key. Treating JOINs as prohibitively expensive is a good habit to be in.
For basicly static lookup tables, I generally make static constant files (such as your #3). I generally use classes such as:
namespace Constants;
class UserTypes {
const ADMIN = 1;
const USER = 2;
const GUEST = 3;
}
$id = Constants\UserTypes::ADMIN;
When I'm using lookup takes that are a bit more variable, then I'll pull it into a object and then cache it for 24 hours. That way it only gets updated once a day. That will save you from making database round trips, but allow you to deal with things in code easily.
Yeah, you're right about avoiding #3 and sticking with #2. As much as possible, look-ups like when you use a usertype table to contain the roles and then relate them to the user table using the id values should stay in the database. If you use constants, then the data must always rely on your php code to be interpreted. Also, you can enforce data integrity by using foreign keys (where servers allow) and it will allow you to port the reporting from your php code to other reporting tools. Maintenance also becomes easier. Database administrators won't need to know php in order to derive the meanings of the numbers if you used #3, should they ever be asked to aid in reports development. It may not seem too relevant, but in terms of maintenance, using stored procedures than embedded sql in your php code would also be maintenance-friendly in several ways, and will also be advantageous to DBAs.
I'd go for option #2 and use the join as it is intended to be used. You never know what the future will throw up, it's always better to be prepared today!
With regards to leaving the database alone as much as possible for such operations, there is also the possibility of caching in the long term. For this route, within PHP an option is to use a file cache, one that will only get updated when time calls for it. For the framework I have created, here's an example; I'd be interested to know what people think:
Note:
(LStore, LFetch, GetFileName) belong to a Cache object which gets called statically.
(Blobify and Unblobify) belong to a SystemComponent object which is always alive
Each piece of cache data has a key. this is the only thing you ever have to remember
public function LStore($key,$data, $blnBlobify=true) {
/* Opening the file in read/write mode */
$h = fopen(self::GetFileName($key, 'longstore'),'a+');
if (!$h) throw new Exception('Could not write to cache');
flock($h,LOCK_EX); // exclusive lock, will get released when the file is closed
fseek($h,0); // go to the start of the file
/* truncate the file */
ftruncate($h,0);
if($blnBlobify==true) { $data = SystemComponent::Blobify(array($data)); }
If (fwrite($h,$data)===false) {
throw new Exception('Could not write to cache');
}
fclose($h);
}
public function LFetch($key) {
$filename = self::GetFileName($key, 'longstore');
if (!file_exists($filename)){ return false;}
$h = fopen($filename,'r');
if (!$h){ return false;}
/* Getting a shared lock */
flock($h,LOCK_SH);
$data = file_get_contents($filename);
fclose($h);
$data = SystemComponent::Unblobify($data);
if (!$data) {
/* If unserializing somehow didn't work out, we'll delete the file */
unlink($filename);
return false;
}
return $data;
}
/* This function is necessary as the framework scales different directories */
private function GetFileName($key, $strCacheDirectory='') {
if(!empty($strCacheDirectory)){
return SystemComponent::GetCacheAdd() . $strCacheDirectory.'/' . md5($key);
} else {
return SystemComponent::GetCacheAdd() . md5($key);
}
}
public function Blobify($Source){
if(is_array($Source)) { $Source = serialize($Source); }
$strSerialized = base64_encode($Source);
return $strSerialized;
}
public function Unblobify($strSerialized){
$Decoded = base64_decode($strSerialized);
if(self::CheckSerialized($Decoded)) { $Decoded = unserialize($Decoded); }
return $Decoded;
}
function CheckSerialized($Source){
$Data = #unserialize($Source);
if ($Source === 'b:0;' || $Data !== false) {
return true;
} else {
return false;
}
}
Now when it comes to accessing the actual data, I just call a fetch. For making sure it is up to date, I tell it to store. In your case, this would be after updating the usertype table.

Categories