Skip to content

Commit f02f6e1

Browse files
Add ManyToMany* for Terms, Users, and Comments
1 parent 3783aea commit f02f6e1

File tree

8 files changed

+936
-2
lines changed

8 files changed

+936
-2
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Contains the class definition for ManyToManyComments
4+
*
5+
* @author Iron Bound Designs
6+
* @since 2.0
7+
* @license MIT
8+
* @copyright Iron Bound Designs, 2016.
9+
*/
10+
11+
namespace IronBound\DB\Relations;
12+
13+
use IronBound\DB\Collection;
14+
use IronBound\DB\Model;
15+
use IronBound\DB\Query\FluentQuery;
16+
use IronBound\DB\Table\Association\CommentAssociationTable;
17+
use IronBound\DB\WP\Comments;
18+
19+
/**
20+
* Class ManyToManyComments
21+
*
22+
* @package IronBound\DB\Relations
23+
*/
24+
class ManyToManyComments extends ManyToMany {
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $update_meta_cache = true;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function __construct( Model $parent, CommentAssociationTable $association, $attribute ) {
35+
parent::__construct( '', $parent, $association, $attribute );
36+
37+
$this->join_on = 'comment_ID';
38+
}
39+
40+
/**
41+
* Update the comment meta cache when loading this relation.
42+
*
43+
* @since 2.0
44+
*
45+
* @param bool $update
46+
*
47+
* @return $this
48+
*/
49+
public function update_meta_cache( $update = true ) {
50+
$this->update_meta_cache = $update;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
protected function make_query_object( $model_class = false ) {
59+
return new FluentQuery( new Comments() );
60+
}
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
public function get_results() {
66+
$results = parent::get_results();
67+
$comments = $results->toArray();
68+
69+
update_comment_cache( $comments, $this->update_meta_cache );
70+
71+
return $results;
72+
}
73+
74+
/**
75+
* @inheritDoc
76+
*/
77+
protected function register_events( Collection $results ) {
78+
// no-op there is no corresponding model to keep synced
79+
}
80+
81+
/**
82+
* @inheritDoc
83+
*/
84+
protected function register_cache_events() {
85+
// no-op
86+
}
87+
88+
/**
89+
* @inheritDoc
90+
*/
91+
public function eager_load( array $models, $callback = null ) {
92+
$loaded = parent::eager_load( $models, $callback );
93+
$comments = $loaded->toArray();
94+
95+
update_comment_cache( $comments, $this->update_meta_cache );
96+
97+
return $loaded;
98+
}
99+
}

src/Relations/ManyToManyTerms.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Contains the class definition for ManyToManyTerms
4+
*
5+
* @author Iron Bound Designs
6+
* @since 2.0
7+
* @license MIT
8+
* @copyright Iron Bound Designs, 2016.
9+
*/
10+
11+
namespace IronBound\DB\Relations;
12+
13+
use IronBound\DB\Collection;
14+
use IronBound\DB\Model;
15+
use IronBound\DB\Query\FluentQuery;
16+
use IronBound\DB\Table\Association\TermAssociationTable;
17+
use IronBound\DB\WP\Terms;
18+
use IronBound\DB\WP\TermTaxonomy;
19+
20+
/**
21+
* Class ManyToManyTerms
22+
*
23+
* @package IronBound\DB\Relations
24+
*/
25+
class ManyToManyTerms extends ManyToMany {
26+
27+
/**
28+
* @var bool
29+
*/
30+
protected $update_meta_cache = true;
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function __construct( Model $parent, TermAssociationTable $association, $attribute ) {
36+
parent::__construct( '', $parent, $association, $attribute );
37+
38+
$this->join_on = 'term_id';
39+
}
40+
41+
/**
42+
* Update the term meta cache when loading this relation.
43+
*
44+
* @since 2.0
45+
*
46+
* @param bool $update
47+
*
48+
* @return $this
49+
*/
50+
public function update_meta_cache( $update = true ) {
51+
$this->update_meta_cache = $update;
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
protected function make_query_object( $model_class = false ) {
60+
$query = new FluentQuery( new Terms() );
61+
$query->join( new TermTaxonomy(), 'term_id', 'term_taxonomy_id' );
62+
63+
return $query;
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function get_results() {
70+
$results = parent::get_results();
71+
$terms = $results->toArray();
72+
73+
update_term_cache( $terms );
74+
75+
if ( $this->update_meta_cache ) {
76+
$ids = array_map( function ( $term ) {
77+
return $term->term_id;
78+
}, $terms );
79+
80+
update_meta_cache( 'term', $ids );
81+
}
82+
83+
return $results;
84+
}
85+
86+
/**
87+
* @inheritDoc
88+
*/
89+
protected function register_events( Collection $results ) {
90+
// no-op there is no corresponding model to keep synced
91+
}
92+
93+
/**
94+
* @inheritDoc
95+
*/
96+
protected function register_cache_events() {
97+
// no-op
98+
}
99+
100+
/**
101+
* @inheritDoc
102+
*/
103+
public function eager_load( array $models, $callback = null ) {
104+
$loaded = parent::eager_load( $models, $callback );
105+
$terms = $loaded->toArray();
106+
107+
update_term_cache( $terms );
108+
109+
if ( $this->update_meta_cache ) {
110+
$ids = array_map( function ( $term ) {
111+
return $term->term_id;
112+
}, $terms );
113+
114+
update_meta_cache( 'term', $ids );
115+
}
116+
117+
return $loaded;
118+
}
119+
}

src/Relations/ManyToManyUsers.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Contains the class definition for ManyToManyUsers
4+
*
5+
* @author Iron Bound Designs
6+
* @since 2.0
7+
* @license MIT
8+
* @copyright Iron Bound Designs, 2016.
9+
*/
10+
11+
namespace IronBound\DB\Relations;
12+
13+
use IronBound\DB\Collection;
14+
use IronBound\DB\Model;
15+
use IronBound\DB\Query\FluentQuery;
16+
use IronBound\DB\Table\Association\UserAssociationTable;
17+
use IronBound\DB\WP\Users;
18+
19+
/**
20+
* Class ManyToManyPosts
21+
*
22+
* @package IronBound\DB\Relations
23+
*/
24+
class ManyToManyUsers extends ManyToMany {
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $update_meta_cache = false;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function __construct( Model $parent, UserAssociationTable $association, $attribute ) {
35+
parent::__construct( '', $parent, $association, $attribute );
36+
37+
$this->join_on = 'ID';
38+
}
39+
40+
/**
41+
* Update the user meta cache when loading this relation.
42+
*
43+
* @since 2.0
44+
*
45+
* @param bool $update
46+
*
47+
* @return $this
48+
*/
49+
public function update_meta_cache( $update = true ) {
50+
$this->update_meta_cache = $update;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
protected function make_query_object( $model_class = false ) {
59+
return new FluentQuery( new Users() );
60+
}
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
public function get_results() {
66+
$results = parent::get_results();
67+
$users = $results->toArray();
68+
69+
$ids = array();
70+
71+
foreach ( $users as $user ) {
72+
update_user_caches( $user );
73+
74+
$ids[] = $user->ID;
75+
}
76+
77+
if ( $this->update_meta_cache ) {
78+
update_meta_cache( 'user', $ids );
79+
}
80+
81+
return $results;
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*/
87+
protected function register_events( Collection $results ) {
88+
// no-op there is no corresponding model to keep synced
89+
}
90+
91+
/**
92+
* @inheritDoc
93+
*/
94+
protected function register_cache_events() {
95+
// no-op
96+
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
public function eager_load( array $models, $callback = null ) {
102+
$loaded = parent::eager_load( $models, $callback );
103+
$users = $loaded->toArray();
104+
105+
$ids = array();
106+
107+
foreach ( $users as $user ) {
108+
update_user_caches( $user );
109+
110+
$ids[] = $user->ID;
111+
}
112+
113+
if ( $this->update_meta_cache ) {
114+
update_meta_cache( 'user', $ids );
115+
}
116+
117+
return $loaded;
118+
}
119+
}

0 commit comments

Comments
 (0)