Skip to content

Commit f4b6d70

Browse files
authored
Merge pull request #18 from flightphp/pass-to-database-connection
if you pass an unknown method to ActiveRecord it will pass it to the …
2 parents cc90316 + 6af8efe commit f4b6d70

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/ActiveRecord.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ public function __construct($databaseConnection = null, ?string $table = '', arr
171171
}
172172

173173
/**
174-
* magic function to make calls witch in function mapping stored in $operators and $sqlPart.
175-
* also can call function of PDO object.
174+
* Magic function to make calls to ActiveRecordData::OPERATORS or ActiveRecordData::SQL_PARTS.
175+
* also can call function of databaseConnection object.
176176
* @param string $name function name
177177
* @param array $args The arguments of the function.
178178
* @return mixed Return the result of callback or the current object to make chain method calls.
@@ -193,7 +193,9 @@ public function __call($name, $args)
193193
'operator' => ActiveRecordData::SQL_PARTS[$name],
194194
'target' => implode(', ', $args)
195195
]);
196-
}
196+
} else if(method_exists($this->databaseConnection, $name) === true) {
197+
return call_user_func_array([ $this->databaseConnection, $name ], $args);
198+
}
197199
return $this;
198200
}
199201

tests/ActiveRecordPdoIntegrationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use flight\ActiveRecord;
66
use flight\database\pdo\PdoAdapter;
7+
use flight\database\pdo\PdoStatementAdapter;
78
use flight\tests\classes\Contact;
89
use flight\tests\classes\User;
910
use PDO;
@@ -626,4 +627,11 @@ public function testTextBasedPrimaryKeyDuplicateKey()
626627
$this->expectExceptionMessage('UNIQUE constraint failed: my_text_table.my_pk');
627628
$myTextTable2->save();
628629
}
630+
631+
public function testCallMethodPassingToPdoConnection()
632+
{
633+
$result = $this->ActiveRecord->prepare('SELECT * FROM user');
634+
$this->assertInstanceOf(PdoStatementAdapter::class, $result);
635+
$this->assertNotInstanceOf(ActiveRecord::class, $result);
636+
}
629637
}

0 commit comments

Comments
 (0)