Howdy, Stranger!

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

Sign In with Google Sign In with OpenID

Relationship modeling: User -> Business -> Events?

I have a relationship set up between users and businesses (business belongs_to user) and businesses and events (business has_many event). Is there an elegant, elefant-ish way to find out what events belong to a user? Right now, I'm doing it through PHP and SQL but it occurs to me that there may be something built into elefant that I'm missing.


  • edited September 2018

    I believe in the one direction you could just call $event->business ()->owner (). The other's a bit messier though (all events for all businesses for a user), but could be done like this as a method on the User class:

     * Return events from all businesses owned by a user.
     * Note: Returns generic objects like fetch_orig().
    public function events () {
        $businesses = self::query ()->where ('owner', $this->id)->fetch_field ('id');
        $qmarks = array_fill (0, count ($businesses), '?');
        return DB::fetch (
            'select * from #prefix#myapp_event where id in(' . join(',', $qmarks) . ')',

    Edit: Formatting is screwy. See example here:

  • Thanks, I'll give it a try.

Sign In or Register to comment.