Skip to content

Commit a045497

Browse files
committed
Adding first step to embedsOne
1 parent 78c4ae0 commit a045497

File tree

6 files changed

+615
-386
lines changed

6 files changed

+615
-386
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Jenssegers\Mongodb\Eloquent\Builder;
66
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
77
use Jenssegers\Mongodb\Relations\EmbedsMany;
8+
use Jenssegers\Mongodb\Relations\EmbedsOne;
89

910
use Carbon\Carbon;
1011
use DateTime;
@@ -84,6 +85,42 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
8485
return new EmbedsMany($query, $this, $instance, $localKey, $foreignKey, $relation);
8586
}
8687

88+
/**
89+
* Define an embedded one-to-many relationship.
90+
*
91+
* @param string $related
92+
* @param string $collection
93+
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
94+
*/
95+
protected function embedsOne($related, $localKey = null, $foreignKey = null, $relation = null)
96+
{
97+
// If no relation name was given, we will use this debug backtrace to extract
98+
// the calling method's name and use that as the relationship name as most
99+
// of the time this will be what we desire to use for the relatinoships.
100+
if (is_null($relation))
101+
{
102+
list(, $caller) = debug_backtrace(false);
103+
104+
$relation = $caller['function'];
105+
}
106+
107+
if (is_null($localKey))
108+
{
109+
$localKey = '_' . $relation;
110+
}
111+
112+
if (is_null($foreignKey))
113+
{
114+
$foreignKey = snake_case(class_basename($this));
115+
}
116+
117+
$query = $this->newQuery();
118+
119+
$instance = new $related;
120+
121+
return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation);
122+
}
123+
87124
/**
88125
* Convert a DateTime to a storable MongoDate object.
89126
*

0 commit comments

Comments
 (0)