wp-orm icon indicating copy to clipboard operation
wp-orm copied to clipboard

Table Relationships

Open khushroo-mistry opened this issue 10 years ago • 4 comments

All the examples are related to a simple table. What about tables with relationships.

E.g. An Organisation has many employees: So when I create a Organisation object at design time I will also have a array of employee set in the organisation object. Thus composing the employee object with the organisation object.

After doing that if I save organisation can i be assured that I will have a record in the organisation table and 1 or many employees in the employee table and their foreign keys set.

Please let me know.

khushroo-mistry avatar Apr 10 '14 08:04 khushroo-mistry

@brandonwamboldt What about adding the following method to the Query class?

public function join($table, $one, $two, $type = 'join',  $operator = '=') {
    $this->joins[] = compact('table', 'one', 'two', 'type', 'operator');
}

The method could then be used like (real world example):

$popular_posts = Post::query()
    ->limit(3 - count($hero_posts))
    ->offset(0)
    ->join('postmeta', 'postmeta.post_id', 'post.ID')
    ->where_not_in(BasePost::get_primary_key(), $exclude)
    ->where_in(BasePost::get_primary_key(), $sticky_posts)
    ->where('postmeta.meta_key', 'turnstile_post_views_count')
    ->where_gte('post_date', date("F jS, Y", strtotime("-7 days")))
    ->sort_by('postmeta.meta_value')
    ->order('DESC')
    ->find();

josegonzalez avatar Sep 12 '14 18:09 josegonzalez

@josegonzalez That's what I'm planning on doing, I've just been gathering a bunch of examples of things I do manually that I'd want this to be flexible enough to handle :smile:

brandonwamboldt avatar Sep 12 '14 19:09 brandonwamboldt

@brandonwamboldt did you make any progress on this? thanks

jelofsson avatar Jul 09 '15 20:07 jelofsson

Any updates on this issue?

helarqjsc avatar Dec 21 '15 06:12 helarqjsc