-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed return type hint for Resultset::getFirst
See: #15027
- Loading branch information
1 parent
18afd6e
commit f435b74
Showing
8 changed files
with
157 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
* For the full copyright and license information, please view the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Mvc\Model; | ||
|
@@ -307,8 +307,30 @@ abstract class Resultset | |
|
||
/** | ||
* Get first row in the resultset | ||
* | ||
* ```php | ||
* $model = new Robots(); | ||
* $manager = $model->getModelsManager(); | ||
* | ||
* // \Robots | ||
* $manager->createQuery('SELECT * FROM Robots') | ||
* ->execute() | ||
* ->getFirst(); | ||
* | ||
* // \Phalcon\Mvc\Model\Row | ||
* $manager->createQuery('SELECT r.id FROM Robots AS r') | ||
* ->execute() | ||
* ->getFirst(); | ||
* | ||
* // NULL | ||
* $manager->createQuery('SELECT r.id FROM Robots AS r WHERE r.name = "NON-EXISTENT"') | ||
* ->execute() | ||
* ->getFirst(); | ||
* ``` | ||
* | ||
* @return ModelInterface|Row|null | ||
*/ | ||
public function getFirst() -> <ModelInterface> | null | ||
public function getFirst() -> var | null | ||
{ | ||
if this->count == 0 { | ||
return null; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE.txt | ||
* file that was distributed with this source code. | ||
* For the full copyright and license information, please view the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Mvc\Model; | ||
|
@@ -49,8 +49,10 @@ interface ResultsetInterface | |
|
||
/** | ||
* Get first row in the resultset | ||
* | ||
* @return ModelInterface|Row|null | ||
*/ | ||
public function getFirst() -> <ModelInterface> | null; | ||
public function getFirst() -> var | null; | ||
|
||
/** | ||
* Returns the current hydration mode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Phalcon Framework. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phalcon\Test\Database\Mvc\Model\Resultset; | ||
|
||
use DatabaseTester; | ||
use Phalcon\Mvc\Model\ManagerInterface; | ||
use Phalcon\Mvc\Model\Row; | ||
use Phalcon\Storage\Exception; | ||
use Phalcon\Test\Fixtures\Migrations\InvoicesMigration; | ||
use Phalcon\Test\Fixtures\Traits\DiTrait; | ||
use Phalcon\Test\Fixtures\Traits\RecordsTrait; | ||
use Phalcon\Test\Models\Invoices; | ||
|
||
class GetFirstCest | ||
{ | ||
use DiTrait; | ||
use RecordsTrait; | ||
|
||
/** | ||
* @var InvoicesMigration | ||
*/ | ||
private $invoiceMigration; | ||
|
||
/** | ||
* Executed before each test | ||
* | ||
* @param DatabaseTester $I | ||
* @return void | ||
*/ | ||
public function _before(DatabaseTester $I): void | ||
{ | ||
try { | ||
$this->setNewFactoryDefault(); | ||
} catch (Exception $e) { | ||
$I->fail($e->getMessage()); | ||
} | ||
|
||
$this->setDatabase($I); | ||
|
||
$this->invoiceMigration = new InvoicesMigration($I->getConnection()); | ||
} | ||
|
||
/** | ||
* Tests Mvc\Model\Resultset :: getFirst() - Issue 15027 | ||
* | ||
* @param DatabaseTester $I | ||
* | ||
* @author Phalcon Team <[email protected]> | ||
* @since 2020-05-06 | ||
* @issue 15027 | ||
* | ||
* @group mysql | ||
* @group pgsql | ||
*/ | ||
public function mvcModelResultsetGetFirst(DatabaseTester $I) | ||
{ | ||
$I->wantToTest('Mvc\Model\Resultset :: getFirst() - #15027'); | ||
|
||
/** | ||
* @todo The following tests are skipped for sqlite because we will get | ||
* a General Error 5 database is locked error. | ||
*/ | ||
$invId = ('sqlite' === $I->getDriver()) ? 'null' : 'default'; | ||
|
||
$this->insertDataInvoices($this->invoiceMigration, 7, $invId, 2, 'ccc'); | ||
|
||
/** @var ManagerInterface $manager */ | ||
$manager = $this->getService('modelsManager'); | ||
|
||
$sql = sprintf('SELECT i.inv_id FROM [%s] AS i', Invoices::class); | ||
$I->assertInstanceOf(Row::class, $manager->createQuery($sql)->execute()->getFirst()); | ||
|
||
$sql = sprintf('SELECT * FROM [%s] AS i', Invoices::class); | ||
$I->assertInstanceOf(Invoices::class, $manager->createQuery($sql)->execute()->getFirst()); | ||
|
||
$sql = sprintf('SELECT i.inv_id FROM [%s] AS i WHERE inv_total = -42', Invoices::class); | ||
$I->assertNull($manager->createQuery($sql)->execute()->getFirst()); | ||
} | ||
} |