Skip to content

Commit

Permalink
Apply CQL default values if we do not have the dao marked as loaded (#74
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bajb authored Jun 23, 2021
1 parent 340e16f commit eec8400
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Foundation/AbstractDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ public function getDaoProperties()
return array_keys(static::$_properties[$this->_calledClass]);
}

/**
* Return the default values from this dao
*
* @return array
*/
public function getDefaultDaoValues()
{
return static::$_properties[$this->_calledClass];
}

/**
* Hydrate the DAO with raw data
*
Expand Down
3 changes: 2 additions & 1 deletion src/Ql/Cql/CqlDataStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ protected function _assemble(IStatement $statement, $forPrepare = true)

protected function _getInsertData(QlDao $dao)
{
$changes = $this->_getDaoChanges($dao);
$initial = $dao->isDaoLoaded() ? [] : array_filter($dao->getDefaultDaoValues());
$changes = array_merge($initial, $this->_getDaoChanges($dao));
foreach($dao->getDaoIDProperties() as $prop)
{
if(!isset($changes[$prop]))
Expand Down
27 changes: 27 additions & 0 deletions tests/Ql/Cql/CqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Packaged\Dal\Tests\Ql\Cql\Mocks\MockCqlConnection;
use Packaged\Dal\Tests\Ql\Cql\Mocks\MockCqlDao;
use Packaged\Dal\Tests\Ql\Cql\Mocks\MockCqlDataStore;
use Packaged\Dal\Tests\Ql\Cql\Mocks\MockPrefillCqlDao;
use Packaged\Dal\Tests\Ql\Cql\Mocks\UnpreparedExecuteClient;
use Packaged\Dal\Tests\Ql\Cql\Mocks\UnpreparedPrepareClient;
use Packaged\Dal\Tests\Ql\Cql\Mocks\WriteTimeoutClient;
Expand Down Expand Up @@ -515,4 +516,30 @@ public function testAlwaysInsertID()
$connection->getQueries()
);
}

public function testAlwaysInsertIDWithPrefil()
{
$datastore = new MockCqlDataStore();
$connection = new CqlQueryObserverConnection();
$this->_configureConnection($connection);
$datastore->setConnection($connection);
$connection->connect();
$connection->setResolver(new DalResolver());

$dao = new MockPrefillCqlDao();
$dao->id = uniqid('daotest');
$dao->id2 = 12345;
$dao->username = 'abc';
$datastore->save($dao);
self::assertEquals(
[
[
'runQuery',
'INSERT INTO "mock_ql_daos" ("intVal", "id", "id2", "username") VALUES (?, ?, ?, ?)',
[1, $dao->id, 12345, 'abc'],
],
],
$connection->getQueries()
);
}
}
7 changes: 7 additions & 0 deletions tests/Ql/Cql/Mocks/MockPrefillCqlDao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Packaged\Dal\Tests\Ql\Cql\Mocks;

class MockPrefillCqlDao extends MockCqlDao
{
public $intVal = 1;
}

0 comments on commit eec8400

Please sign in to comment.