Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
method inside a model
  • Hi I'm trying to make a method inside Model_User which will allow me to get upcoming events, or passed_events of that user I call. So, if I was on a profile page, I could do $user->upcomining_events(); And if I was on my profile, as I have $current_user set as a template global, I want to be able to do $current_user->upcoming_events(); I decided it is better to do it this way than alter the relationship to handle the date before/after today, as I still want to be able to do $user->events to get ALL their events, regardless of past or future. My code so far; which isn't working..
    <?php
    
    class Model_User extends \Orm\Model
    {
     protected static $_has_many = array('events', 'attendees', 'friends');
     
     protected static $_observers = array(
      'Orm\Observer_CreatedAt' => array(
       'events' => array('before_insert'),
       'mysql_timestamp' => false,
      ),
      'Orm\Observer_UpdatedAt' => array(
       'events' => array('before_save'),
       'mysql_timestamp' => false,
      ),
     );
     
     public static function upcoming_events()
     {
      $query = Model_Event::find()
       ->where('start_date', '>=', date('m/d/Y'))
       ->where('user_id', $this->id)
       ->get();
            
      return $query;
     }
    

    It gives me the error:
    ErrorException [ Error ]: Using $this when not in object context

    However, if I replace $this->id with 1 (my ID) it works! Not sure what to do.. Thanks for your help :)
  • Remove static keyword from method declaration.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion