Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 JimL

JimL

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 21 June 2014 - 03:30 PM

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, 21 June 2014 - 03:36 PM.


#2 ginerjm

ginerjm

    Advanced Member

  • Members
  • PipPipPip
  • 1,917 posts
  • LocationVoorheesville NY

Posted 21 June 2014 - 07:17 PM

I have no idea what you are showing us.

JG

PS - If you're posting here you should be using:
        error_reporting(E_ALL | E_NOTICE);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!


#3 JimL

JimL

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 21 June 2014 - 10:22 PM

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

#4 trq

trq

    Advanced Member

  • Administrators
  • 31,034 posts
  • LocationSydney, Australia.

Posted 23 June 2014 - 07:40 AM

I'm not sure you're going to find much help here with such an obscure little library.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 JimL

JimL

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 23 June 2014 - 10:18 AM

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.



#6 trq

trq

    Advanced Member

  • Administrators
  • 31,034 posts
  • LocationSydney, Australia.

Posted 23 June 2014 - 06:38 PM

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.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#7 JimL

JimL

    Newbie

  • New Members
  • Pip
  • 5 posts

Posted 23 June 2014 - 06:48 PM

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, 23 June 2014 - 06:48 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com