JimL Posted June 21, 2014 Share Posted June 21, 2014 (edited) 1Total 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'] ]; } 2The 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 June 21, 2014 by JimL Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/ Share on other sites More sharing options...
ginerjm Posted June 22, 2014 Share Posted June 22, 2014 I have no idea what you are showing us. Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483009 Share on other sites More sharing options...
JimL Posted June 22, 2014 Author Share Posted June 22, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483012 Share on other sites More sharing options...
trq Posted June 23, 2014 Share Posted June 23, 2014 I'm not sure you're going to find much help here with such an obscure little library. Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483063 Share on other sites More sharing options...
JimL Posted June 23, 2014 Author Share Posted June 23, 2014 I'm not sure you're going to find much help here with such an obscure little library. Yeah, guessing it's bigger in ruby than in php. Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483076 Share on other sites More sharing options...
trq Posted June 23, 2014 Share Posted June 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483109 Share on other sites More sharing options...
JimL Posted June 23, 2014 Author Share Posted June 23, 2014 (edited) 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 June 23, 2014 by JimL Quote Link to comment https://forums.phpfreaks.com/topic/289232-php-active-record-get-number-of-likes-and-if-logged-in-user-has-liked-post/#findComment-1483110 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.