Jump to content

JimL

New Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by JimL

  1. 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!

  2. 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']]]
      ];
    
    }
    
×
×
  • 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.