Skip to content

Commit

Permalink
if you pass an unknown method to ActiveRecord it will pass it to the …
Browse files Browse the repository at this point in the history
…database connection
  • Loading branch information
n0nag0n committed Jul 4, 2024
1 parent cc90316 commit 6af8efe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public function __construct($databaseConnection = null, ?string $table = '', arr
}

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

Expand Down
8 changes: 8 additions & 0 deletions tests/ActiveRecordPdoIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use flight\ActiveRecord;
use flight\database\pdo\PdoAdapter;
use flight\database\pdo\PdoStatementAdapter;
use flight\tests\classes\Contact;
use flight\tests\classes\User;
use PDO;
Expand Down Expand Up @@ -626,4 +627,11 @@ public function testTextBasedPrimaryKeyDuplicateKey()
$this->expectExceptionMessage('UNIQUE constraint failed: my_text_table.my_pk');
$myTextTable2->save();
}

public function testCallMethodPassingToPdoConnection()
{
$result = $this->ActiveRecord->prepare('SELECT * FROM user');
$this->assertInstanceOf(PdoStatementAdapter::class, $result);
$this->assertNotInstanceOf(ActiveRecord::class, $result);
}
}

0 comments on commit 6af8efe

Please sign in to comment.