I am using CakePHP 3.4+
I have written an application with multi level membership
.
The Pro
members will have benefit to view short url for external links which when shared will record the visit count to that url.
The original url is stored in PostVideos
table for all user.
I have created a table to store uniuqe keys
for short urls
inside short_video_post_urls
with columns
+----+---------------+------------+-------------+
| id | post_video_id | unique_key | visit_count |
+----+---------------+------------+-------------+
Since, count of Pro members will be low than normal users, I don't want to generate unique_key entry
in short_video_post_urls
because It will flood database with useless records.
So, what I want is to generate them dynamically and store them for PRO members only
Now, in template
file I'm using $postVideo->video_url
to display original video url from post_videos
table.
Question
What I want is to tweak video_url
entity call which will check for
- Membership level of logged in user
- If member is pro
- check if unique key exists in
ShortVideoPostUrls
model for the url requested - If no record exists, then create a unique_key in
ShortVideoPostUrls
- return the new url with
unique_key
- check if unique key exists in
But for that I need to access logged_in
user data in the entity class.
What I tried?
class PostVideoLog extends Entity
{
/*
* ----
* ----
*/
protected function _getVideoUrl()
{
$user = $this->Users->get($this->Auth->user('id'), [
'contain' => [
'MembershipLevels'
]
]);
if ($user) {
if (strtolower($user->membership_level->title) === 'pro') {
/**
* check if unique_key exists for this request
*/
$checkShortUrl = $this->ShortVideoPostUrls->find()
->where(['post_video_log_id' => $this->_properties['id']])
->first();
if ($checkShortUrl) {
return $this->_generateUrl($checkShortUrl->unique_key);
}
/**
* create new record
*/
$unique_key_generator = new Hashids(UNIQUE_SHORT_URL_SALT, 4);
$unique_key = $unique_key_generator->encode($this->_properties['id']);
$newShortUrl = $this->ShortVideoPostUrls->newEntity();
$newShortUrl = $this->ShortVideoPostUrls->patchEntity($newShortUrl, [
'unique_key' => $unique_key,
'post_video_log_id' => $this->_properties['id']
]);
if ($this->ShortVideoPostUrls->save($newShortUrl)) {
return $this->_generateUrl($unique_key);
}
}
}
return $this->_properties['video_url'];
}
private function _generateUrl($unique_key)
{
$base_url = Router::url('/', true);
return $base_url . '/u/' . $unique_key;
}
}
I'm not sure, whether my approach is right or wrong.
To load Users
model and other models I'm using in above function requires to use
$this->loadModel('Users');
But, loadModel
seems not to be working here.
What is other approach to do this? Or how to load external model and Auth component in Entity class?
Edit 2
Is there any better alternative to do what I want without entity? or simply some way to call function from template
on each entity?
Ex.
$postVideo->video_url()