1: <?php namespace Laravel\Database\Eloquent\Relationships;
2:
3: use Laravel\Database\Eloquent\Model;
4: use Laravel\Database\Eloquent\Query;
5:
6: abstract class Relationship extends Query {
7:
8: /**
9: * The base model for the relationship.
10: *
11: * @var Model
12: */
13: protected $base;
14:
15: /**
16: * Create a new has one or many association instance.
17: *
18: * @param Model $model
19: * @param string $associated
20: * @param string $foreign
21: * @return void
22: */
23: public function __construct($model, $associated, $foreign)
24: {
25: $this->foreign = $foreign;
26:
27: // We will go ahead and set the model and associated instances on the
28: // relationship to match the relationship targets passed in from the
29: // model. These will allow us to gather the relationship info.
30: if ($associated instanceof Model)
31: {
32: $this->model = $associated;
33: }
34: else
35: {
36: $this->model = new $associated;
37: }
38:
39: // For relationships, we'll set the base model to be the model being
40: // associated from. This model contains the value of the foreign
41: // key needed to connect to the associated model.
42: if ($model instanceof Model)
43: {
44: $this->base = $model;
45: }
46: else
47: {
48: $this->base = new $model;
49: }
50:
51: // Next we'll set the fluent query builder for the relationship and
52: // constrain the query such that it only returns the models that
53: // are appropriate for the relationship.
54: $this->table = $this->table();
55:
56: $this->constrain();
57: }
58:
59: /**
60: * Get the foreign key name for the given model.
61: *
62: * @param string $model
63: * @param string $foreign
64: * @return string
65: */
66: public static function foreign($model, $foreign = null)
67: {
68: if ( ! is_null($foreign)) return $foreign;
69:
70: // If the model is an object we'll simply get the class of the object and
71: // then take the basename, which is simply the object name minus the
72: // namespace, and we'll append "_id" to the name.
73: if (is_object($model))
74: {
75: $model = class_basename($model);
76: }
77:
78: return strtolower(basename($model).'_id');
79: }
80:
81: /**
82: * Get a freshly instantiated instance of the related model class.
83: *
84: * @param array $attributes
85: * @return Model
86: */
87: protected function fresh_model($attributes = array())
88: {
89: $class = get_class($this->model);
90:
91: return new $class($attributes);
92: }
93:
94: /**
95: * Get the foreign key for the relationship.
96: *
97: * @return string
98: */
99: public function foreign_key()
100: {
101: return static::foreign($this->base, $this->foreign);
102: }
103:
104: /**
105: * Gather all the primary keys from a result set.
106: *
107: * @param array $results
108: * @return array
109: */
110: public function keys($results)
111: {
112: $keys = array();
113:
114: foreach ($results as $result)
115: {
116: $keys[] = $result->get_key();
117: }
118:
119: return array_unique($keys);
120: }
121:
122: /**
123: * The relationships that should be eagerly loaded by the query.
124: *
125: * @param array $includes
126: * @return Relationship
127: */
128: public function with($includes)
129: {
130: $this->model->includes = (array) $includes;
131:
132: return $this;
133: }
134:
135: }