Jump to content

PHP Active Record, get number of likes and if logged in user has liked post


Recommended Posts

1
Total number of likes work, but I'm guessing it can be done differently, any input would be appreciated.
 
/controllers/indexController.php

$posts = Post::find('all', [
      'limit' => 4,
      'include' => ['likes']
    ]);
    
    foreach ($posts as $post) {
      $post->assign_attribute('likeCount', count($post->likes));
    }
}

 
/models/Post.php

class Post extends ActiveRecord\Model {
  
  static $has_many = [
    ['comments', 'class_name' => 'PostComments'],
    ['likes', 'class_name' => 'UserLikePosts']
  ];
  
}

 
/models/UserLikePosts.php

class UserLikePosts extends ActiveRecord\Model {
    
  static $belongs_to = [
    ['user'],
    ['post']
  ];

}

2
The big question is, how can I get a true/false for "if current user has liked post"? I was thinking something like this, but using vars won't work here, and I would have to use a ternary or a function call to do it like this as the session var might not be set (guest).
 

class Post extends ActiveRecord\Model {

  static $has_many = [
    ['comments'],
    ['likes', 'class_name' => 'UserLikePosts'],
    ['user_likes', 'class_name' => 'UserLikePosts', 'conditions' => ['user_id = ?', [$_SESSION['user']['id']]]
  ];

}
Edited by JimL

I have no idea what you are showing us.

It's from a hobby project I'm working on using Active Record. An Object Relational Mapper (ORM) which basically is an abstraction layer for database interaction.

 

http://www.phpactiverecord.org

AR is just a pattern. One of the more popular frameworks getting around these days (Laravel) uses this same pattern for its ORM Eloquent. So, the pattern is around, I've just never heard of that particular library before.

 

AR isn't a particularly good pattern to use for an ORM anyway though in my opinion as it tightly couples you models to their persistence implementation. The Data Mapper pattern gives you a much cleaner end result. Maybe you'd be interested in looking at that? If so, I would highly recommend Doctrine.

AR is just a pattern. One of the more popular frameworks getting around these days (Laravel) uses this same pattern for its ORM Eloquent. So, the pattern is around, I've just never heard of that particular library before.

 

AR isn't a particularly good pattern to use for an ORM anyway though in my opinion as it tightly couples you models to their persistence implementation. The Data Mapper pattern gives you a much cleaner end result. Maybe you'd be interested in looking at that? If so, I would highly recommend Doctrine.

 

Yeah PHP-AR is being used in a project at work, so was trying to get my head around it.

 

For other projects at home I'm using Doctrine2 :)

 

Thanks!

Edited by JimL
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.