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']]]
];
}