1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace rsanchez\Deep\Model;
11:
12: use Illuminate\Database\Eloquent\Model;
13: use Illuminate\Database\Eloquent\Builder;
14: use rsanchez\Deep\Model\Entry;
15:
16: 17: 18: 19: 20:
21: class RelationshipEntry extends Entry
22: {
23: 24: 25:
26: protected $hidden = array('channel', 'site_id', 'forum_topic_id', 'ip_address', 'versioning_enabled', 'parent_id', 'field_id', 'grid_field_id', 'grid_col_id', 'grid_row_id', 'child_id', 'order', 'relationship_id');
27:
28: 29: 30:
31: protected $collectionClass = '\\rsanchez\\Deep\\Collection\\RelationshipCollection';
32:
33: 34: 35:
36: protected static function joinTables()
37: {
38: return array_merge(parent::joinTables(), array(
39: 'relationships' => function ($query) {
40: $query->join('relationships', 'relationships.child_id', '=', 'channel_titles.entry_id');
41: },
42: ));
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52: public function scopeParentEntryId(Builder $query, $entryId)
53: {
54: $entryId = is_array($entryId) ? $entryId : array($entryId);
55:
56: return $this->requireTable($query, 'relationships', true)->whereIn('relationships.parent_id', $entryId);
57: }
58:
59: 60: 61: 62: 63: 64: 65:
66: public function scopeParents(Builder $query, $entryId)
67: {
68: $entryId = is_array($entryId) ? $entryId : array($entryId);
69:
70: return $query->join('relationships', 'relationships.parent_id', '=', 'channel_titles.entry_id')
71: ->whereIn('relationships.child_id', $entryId)
72: ->orderBy('relationships.order', 'asc')
73: ->groupBy('relationships.child_id')
74: ->groupBy('relationships.field_id')
75: ->groupBy('channel_titles.entry_id');
76: }
77:
78: 79: 80: 81: 82: 83: 84:
85: public function scopeSiblings(Builder $query, $entryId)
86: {
87: $entryId = is_array($entryId) ? $entryId : array($entryId);
88:
89: $connection = $query->getQuery()->getConnection();
90: $tablePrefix = $connection->getTablePrefix();
91:
92: return $query->join('relationships', 'relationships.child_id', '=', 'channel_titles.entry_id')
93: ->join($connection->raw("`{$tablePrefix}relationships` AS `{$tablePrefix}relationships_2`"), 'relationships_2.parent_id', '=', 'relationships.parent_id')
94: ->addSelect('*')
95: ->addSelect('relationships_2.child_id AS sibling_id')
96: ->whereIn('relationships_2.child_id', $entryId)
97: ->orderBy('relationships.order', 'asc')
98: ->groupBy('relationships_2.child_id')
99: ->groupBy('relationships.field_id')
100: ->groupBy('channel_titles.entry_id');
101: }
102: }
103: