Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Get events I'm invited to, and attending only.
  • Hi Making a small events site for Twitter which I've been working on for a few weeks with thanks to some of you guys here. However, since I'm new to Fuel I'm not sure where to go next with this. I'll explain what I want to achieve and then show you my controller. What I'd like to do is users to be able to go to their dashboard and see a list of all events they've been invited to, and are attending. Once you accept an invite, it'll automatically create the row in the attendees table and delete it from the invites table. <i>Do give me suggestions though if you think it would be better in the attendees table all as one.</i> Basically so far I'm just trying to deal with the invites, I've managed to get the IDs for the events I'm invited to. By the following code:
    $invited = Model_Invite::find()
       ->where('user_id', $this->current_user->id)
       ->related(array('invited_by', 'event'))
       ->get();
      
      foreach ($invited as $i)
       $invites[] = $i->event_id;
    

    And then code for the get events:
    if (count($invites) > 0)
      {
       $events = $this->data['events'] = Model_Event::find()
        ->where('start_date', '>=', date('Y-m-d'))
        ->where('id', 'IN', $invites)
        ->related(array('user', 'attendees'))
        ->order_by('start_date', 'asc')
        ->order_by('start_time', 'asc')
        ->get();
      }
    

    The only problem is, in the view I want to be able to see if an event is an invite, or something I'm attending. I could setup a function in the User model which would be something like is_attending(event_id); etc.. but seems like I'm trying to do too much more than what's necessary. Hopefully someone will be able to point me in the right direction as where to go next with this. I have in my head it would be better adding a type to the rows in the foreach on the invites, type either 'invite' or 'attendee' but I wouldn't know where's best to do that either. Hopefully someone can help me. Maybe getting rid of the invites table and just using the attendees table would be better. Put in the invited_by columns in there, but that's another time maybe.
    Thanks for your support guys, again!
  • From a design point of view, I would say that 'invited' and 'attending' are statusses of the relation between user and event. So I would create the following:
    Users -> has_many <- UserEvents, UserEvents -> belongs_to Users
    Events -> has_many <- UserEvents, UserEvents -> belongs_to Events
    

    By relating a User to an Event, you already indicate they have a relation. Then add a status field in UserEvents to indicate that relation it is, for example using an ENUM('I', 'A') do distinguish invitations from attendance. You can then easily query all users that are invited or attending an event, or list all events a user is connected to. If you make the 'I' the default column value, you can also create a direct many_many relation between Users and Events, using UserEvents as through table. This allows you to directly relate a user to an event, and because of the status default the user will be automatically 'invited'.

Howdy, Stranger!

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

In this Discussion