Skip to content

Commit

Permalink
[#14488] - WIP to figure out why the column map does not map properly…
Browse files Browse the repository at this point in the history
… properties
  • Loading branch information
niden committed Dec 1, 2019
1 parent 858d0e5 commit 07e4c9a
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 3 deletions.
28 changes: 26 additions & 2 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,30 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,

protected dirtyState = 1;

/**
* @var array
*/
protected dirtyRelated = [];

/**
* @var array
*/
protected errorMessages = [];

protected modelsManager;

protected modelsMetaData;

/**
* @var array
*/
protected related = [];

protected operationMade = 0;

/**
* @var array
*/
protected oldSnapshot = [];

protected skipped;
Expand All @@ -122,8 +134,11 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
/**
* Phalcon\Mvc\Model constructor
*/
final public function __construct(var data = null, <DiInterface> container = null, <ManagerInterface> modelsManager = null)
{
final public function __construct(
var data = null,
<DiInterface> container = null,
<ManagerInterface> modelsManager = null
) {
/**
* We use a default DI if the user doesn't define one
*/
Expand Down Expand Up @@ -741,6 +756,15 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
// Change the dirty state to persistent
instance->setDirtyState(dirtyState);

print_r(PHP_EOL);
print_r("data");
print_r(PHP_EOL);
print_r(data);
print_r(PHP_EOL);
print_r("map");
print_r(PHP_EOL);
print_r(columnMap);
print_r(PHP_EOL);
for key, value in data {
// Only string keys in the data are valid
if typeof key !== "string" {
Expand Down
5 changes: 5 additions & 0 deletions phalcon/Mvc/Model/MetaData.zep
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
var data;

let data = this->readColumnMapIndex(model, self::MODELS_COLUMN_MAP);
print_r(PHP_EOL);
print_r("getColumnMap");
print_r(PHP_EOL);
print_r(data);
print_r(PHP_EOL);

if unlikely (data !== null && typeof data != "array") {
throw new Exception("The meta-data is invalid or is corrupt");
Expand Down
10 changes: 10 additions & 0 deletions phalcon/Mvc/Model/Query.zep
Original file line number Diff line number Diff line change
Expand Up @@ -3786,6 +3786,11 @@ class Query implements QueryInterface, InjectionAwareInterface

cache->set(key, result, lifetime);
}
print_r(PHP_EOL);
print_r("result");
print_r(PHP_EOL);
print_r(result);
print_r(PHP_EOL);

/**
* Check if only the first row must be returned
Expand All @@ -3795,6 +3800,11 @@ class Query implements QueryInterface, InjectionAwareInterface
} else {
let preparedResult = result;
}
print_r(PHP_EOL);
print_r("prepared");
print_r(PHP_EOL);
print_r(result);
print_r(PHP_EOL);

return preparedResult;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/_data/fixtures/Migrations/ObjectsMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Fixtures\Migrations;

use Phalcon\Db\Adapter\AdapterInterface;

class ObjectsMigration
{
public function __invoke(AdapterInterface $db)
{
$sql = <<<SQL
drop table if exists objects
SQL;
$db->execute($sql);

$sql = <<<SQL
create table objects
(
obj_id int(10) auto_increment primary key,
obj_name varchar(100) not null,
obj_type tinyint(3) unsigned not null
);
SQL;
$db->execute($sql);

$sql = <<<SQL
insert into objects ( obj_id, obj_name, obj_type )
values (1, 'random data', 1);
SQL;
$db->execute($sql);

$sql = <<<SQL
drop table if exists stuff
SQL;
$db->execute($sql);

$sql = <<<SQL
create table stuff
(
stf_id int(10) auto_increment primary key,
stf_name varchar(100) not null,
stf_type tinyint(3) unsigned not null
);
SQL;
$db->execute($sql);

$sql = <<<SQL
insert into stuff ( stf_id, stf_name, stf_type )
values (2, 'stuff data', 1);
SQL;
$db->execute($sql);
}
}
21 changes: 21 additions & 0 deletions tests/_data/fixtures/models/Objects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Models;

use Phalcon\Mvc\Model;

class Objects extends Model
{
public $obj_id;
public $obj_name;
public $obj_type;
}
21 changes: 21 additions & 0 deletions tests/_data/fixtures/models/Stuff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Models;

use Phalcon\Mvc\Model;

class Stuff extends Model
{
public $stf_id;
public $stf_name;
public $stf_type;
}
68 changes: 67 additions & 1 deletion tests/integration/Mvc/Model/Resultset/GetFirstCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
namespace Phalcon\Test\Integration\Mvc\Model\Resultset;

use IntegrationTester;
use Phalcon\Test\Fixtures\Migrations\ObjectsMigration;
use Phalcon\Test\Fixtures\Traits\DiTrait;

/**
* Class GetFirstCest
*/
class GetFirstCest
{
use DiTrait;

/**
* Tests Phalcon\Mvc\Model\Resultset :: getFirst()
*
Expand All @@ -28,6 +32,68 @@ class GetFirstCest
public function mvcModelResultsetGetFirst(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\Resultset - getFirst()');
$I->skipTest('Need implementation');

$this->setNewFactoryDefault();
$this->setDiMysql();

/**
* Setup the table
*/
(new ObjectsMigration())($this->container->get('db'));

$manager = $this->container->get('modelsManager');
// $results = $manager
// ->executeQuery(
// 'SELECT o.* FROM Phalcon\Test\Models\Objects AS o LIMIT 1'
// )
// ;
// $record = $results->getFirst();
// $id = $record->obj_id;
// $I->assertEquals(1, $id);
//
// $results = $manager
// ->executeQuery(
// 'SELECT obj_id FROM Phalcon\Test\Models\Objects AS o LIMIT 1'
// )
// ;
// $record = $results->getFirst();
// $id = $record->obj_id;
// $I->assertEquals(1, $id);
//
// $results = $manager
// ->executeQuery(
// 'SELECT o.obj_id AS obj_id FROM Phalcon\Test\Models\Objects AS o LIMIT 1'
// )
// ;
// $record = $results->getFirst();
// $id = $record->obj_id;
// $I->assertEquals(1, $id);

$results = $manager
->executeQuery(
'SELECT
o.obj_id,
s.stf_id
FROM
Phalcon\Test\Models\Objects AS o,
Phalcon\Test\Models\Stuff AS s
WHERE
o.obj_type = s.stf_type
LIMIT 1'
)
;
$record = $results->getFirst();
var_dump($record);
die();
$id = $record->obj_id;
$I->assertEquals(1, $id);

// $resultset = $manager
// ->executeQuery(
// 'SELECT o.obj_id FROM Objects AS o LIMIT 1' // err
// // Objects.obj_id // err
// // o.obj_id AS obj_id // ok
// // obj_id // ok
// )->getFirst()->obj_id;
}
}

0 comments on commit 07e4c9a

Please sign in to comment.