Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit d3b8309

Browse files
author
Nik Barham
committed
Added in new syntax for PartialResults.
Does not yet support filtering
1 parent 334729f commit d3b8309

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

src/Automatorm/Orm/Data.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ public static function groupJoinCount(Collection $collection, $var, $where = [])
315315
return $data['count'];
316316
}
317317
}
318+
319+
public function hasForeignKey($var)
320+
{
321+
return (bool) (
322+
key_exists($var, (array) $this->model['one-to-one'])
323+
or key_exists($var, (array) $this->model['one-to-many'])
324+
or key_exists($var, (array) $this->model['many-to-one'])
325+
or key_exists($var, (array) $this->model['many-to-many'])
326+
);
327+
}
318328

319329
public function join($var, array $where = [])
320330
{

src/Automatorm/Orm/Model.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ final protected function __construct(Data $data)
272272

273273
// Dynamic object properties - Prefer properties set on the model object over column data from the db (Model_Data object)
274274
public function __get($var)
275-
{
275+
{
276+
if ($var == '_') return new PartialResult($this);
277+
276278
// If the property actually exists, then return it rather than looking at the Model_Data object.
277279
if (property_exists($this, $var)) return $this->{$var};
278280

src/Automatorm/Orm/PartialResult.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class PartialResult
1515
protected $database;
1616
protected $route;
1717
protected $multiresult = false;
18+
protected $resolution = null;
1819

1920
public function __construct(Model $source)
2021
{
@@ -25,8 +26,18 @@ public function __construct(Model $source)
2526
$this->database = $source->_data->getDatabase();
2627
}
2728

29+
public function __call($var, $args)
30+
{
31+
$obj = $this->resolve();
32+
33+
return call_user_func_array(array($obj, $var), $args);
34+
}
35+
2836
public function __get($var)
2937
{
38+
if ($this->resolution) return $this->resolution->{$var};
39+
if ($var == '_') return $this->resolve();
40+
3041
if (array_key_exists($var, $this->currentSchema['columns']))
3142
{
3243
// We have column data, resolve!
@@ -53,26 +64,31 @@ public function __get($var)
5364

5465
public function resolve($var = null)
5566
{
56-
// Resolve down to a real Model object, then call __get on it.
57-
$ids = $this->resolveState();
58-
59-
$results = Model::factoryObjectCache($ids, $this->currentTable, $this->database);
60-
61-
if ($this->multiresult && !$results instanceof Collection)
67+
if (!$this->resolution)
6268
{
63-
$results = new Collection([$results]);
69+
// Resolve down to a real Model object, then call __get on it.
70+
$ids = $this->resolveState();
71+
72+
$results = Model::factoryObjectCache($ids, $this->currentTable, $this->database);
73+
74+
if ($this->multiresult && !$results instanceof Collection)
75+
{
76+
$results = new Collection([$results]);
77+
}
78+
79+
if (!$this->multiresult && $results instanceof Collection && $results->count() == 1)
80+
{
81+
$results = $results[0];
82+
}
83+
84+
$this->resolution = $results;
6485
}
6586

66-
if (!$this->multiresult && $results instanceof Collection && $results->count() == 1)
67-
{
68-
$results = $results[0];
69-
}
70-
7187
if (!is_null($var))
7288
{
73-
return $results->{$var};
89+
return $this->resolution->{$var};
7490
}
75-
return $results;
91+
return $this->resolution;
7692
}
7793

7894
protected $joinCount = 0;
@@ -138,8 +154,6 @@ public function pushM2M($col, $target)
138154
$key2 = 'join_' . ++$this->joinCount;
139155
$key2a = $key2 . 'a';
140156

141-
var_dump($target);
142-
143157
$this->route[] =
144158
'Join `' . Schema::underscoreCase($target['pivot']) . '` as ' . $key2a . ' on ' . $key . '.id = ' . $key2a . '.`' . $target['id'] . '` ';
145159
;

0 commit comments

Comments
 (0)