Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
invited_by_user_id accessible
  • Hi I have the following properties in my model however I would like the 'invited_by' accessible, it should reference the users table. Can someone point me in the right direction to get this working? I've read the docs but don't quite get how it's done, once I'm told I'll be able to relate this to other models requiring similar functions.
    protected static $_belongs_to = array('event', 'user');
     
     protected static $_properties = array(
      'id',
      'event_id',
      'user_id',
      'invited',
      'invited_by_user_id',
      'attending',
      'maybe',
      'declined',
      'created_at',
      'updated_at'
     );
    

    Thanks for your time :) Much appreciated!
  • What I want to be able to do is on my dashboard list all of the invites I have, so I would look for rows where user_id is == me and then do something like
    $invite->invited_by->name
    

    Or if someone is attending an event, see who they were invited by
    $attendee->invited_by->user
    
    but at the same time still be able to do
    $attendee->user->name
    
    Thanks! :D
  • This should be a many-to-many relation, as a user can invite multiple people, and someone can be invited by more then one user. It is no problem defining multiple relations between two models. Many-to-many relations are documented here: http://docs.fuelphp.com/packages/orm/relations/many_many.html In this can you will have to use the full definition, as your relation name ('invited_by') differs from the model name (model_to). And pay attention to the key_from and key_through_from.
  • Thanks. Il try my best and share my output today :-)
  • Would I put this in the users model or in the attendee model?
  • An attendee belongs to an event, and a user. An event can have many attendees, but an attendee doesn't have many users. So with the properties, commented inside..
    protected static $_properties = array(
      'id',
      'event_id', // event table
      'user_id', // users table for who is attending
      'invited', // 1 or 0, IF 0 then there will be no 'inivted_by_user_id' found
      'invited_by_user_id', -> //users table lookup
     );
    

    Is many to many still the right way for this?
  • I'm a bit clueless with what you mean by this. From what I understand, you have a users table, which contains users that can be attendees of an event, and can be invited to an event by another user. And I assume a user can be an attentee at multiple events? If correct, this means you have a many-to-many relation between User and Event, and 'invited' is a property of your relation table (as the user is invited by another user for that specific event). As the ORM does not support join fields, you will have to split the relationship, and create a User, an Event and an EventUser model, and define these relations: User -> has_one -> EventUser
    EventUser -> has_many -> User
    Event -> has_one -> EventUser
    EventUser -> has_many -> Event
    EventUser -> has_one -> User (via invited_user_id)
    User -> belongs_to -> EventUser (via invited_user_id) The first for are standard, the last two you need to define as an array (call the relation 'invited') in which you define 'key_from' and 'key_to' as they are non-standard. There is no need for an 'invited' boolean, the ORM will tell you whether or not there is a related 'invited' user.
  • Thanks :) I'll give it a go!

Howdy, Stranger!

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

In this Discussion