Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T13433 metadata cache exception #14108

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
- Fixed `Phalcon\Mvc\View::getRender()` to call `view->finish()` instead of `ob_end_clean()`. [#14095](https://github.com/phalcon/cphalcon/issues/14095)
- Fixed `Phalcon\Cache\Adapter\Libmemcached` failing to set values when `Phalcon\Mvc\Model\MetaData\Libmemcached` was in use. [#14100](https://github.com/phalcon/cphalcon/issues/14100)
- Fixed `Phalcon\Db\Column` to recognize `tinyint`, `smallint`, `mediumint`, `integer` as valid autoIncrement columns. [#14102](https://github.com/phalcon/cphalcon/issues/14102)

- Fixed `Phalcon\Mvc\Model\MetaData::write()` to throw an exception if `orm.exception_on_failed_metadata_save` is set to true. [#13433](https://github.com/phalcon/cphalcon/issues/13433)
## Removed
- Removed `Phalcon\Session\Factory`. [#13672](https://github.com/phalcon/cphalcon/issues/13672)
- Removed `Phalcon\Factory` and `Phalcon\FactoryInterface`. [#13672](https://github.com/phalcon/cphalcon/issues/13672)
Expand Down
6 changes: 5 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@
"type": "bool",
"default": true
},
"orm.exception_on_failed_save": {
"orm.exception_on_failed_metadata_save": {
"type": "bool",
"default": false
},
"orm.exception_on_failed_save": {
"type": "bool",
"default": true
},
"orm.ignore_unknown_columns": {
"type": "bool",
"default": false
Expand Down
81 changes: 43 additions & 38 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -2748,23 +2748,27 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
public static function setup(array! options) -> void
{
var disableEvents, columnRenaming, notNullValidations,
exceptionOnFailedSave, phqlLiterals, virtualForeignKeys,
lateStateBinding, castOnHydrate, ignoreUnknownColumns,
updateSnapshotOnSave, disableAssignSetters,
exceptionOnFailedSave, exceptionOnFailedMetaDataSave, phqlLiterals,
virtualForeignKeys, lateStateBinding, castOnHydrate,
ignoreUnknownColumns, updateSnapshotOnSave, disableAssignSetters,
caseInsensitiveColumnMap, prefetchRecords, lastInsertId;

/**
* Enables/Disables globally the internal events
*/
if fetch disableEvents, options["events"] {
globals_set("orm.events", disableEvents);
if fetch caseInsensitiveColumnMap, options["caseInsensitiveColumnMap"] {
globals_set(
"orm.case_insensitive_column_map",
caseInsensitiveColumnMap
);
}

if fetch lastInsertId, options["castLastInsertIdToInt"] {
globals_set("orm.cast_last_insert_id_to_int", lastInsertId);
}

/**
* Enables/Disables virtual foreign keys
* Enables/Disables automatic cast to original types on hydration
*/
if fetch virtualForeignKeys, options["virtualForeignKeys"] {
globals_set("orm.virtual_foreign_keys", virtualForeignKeys);
if fetch castOnHydrate, options["castOnHydrate"] {
globals_set("orm.cast_on_hydrate", castOnHydrate);
}

/**
Expand All @@ -2774,11 +2778,15 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
globals_set("orm.column_renaming", columnRenaming);
}

if fetch disableAssignSetters, options["disableAssignSetters"] {
globals_set("orm.disable_assign_setters", disableAssignSetters);
}

/**
* Enables/Disables automatic not null validation
* Enables/Disables globally the internal events
*/
if fetch notNullValidations, options["notNullValidations"] {
globals_set("orm.not_null_validations", notNullValidations);
if fetch disableEvents, options["events"] {
globals_set("orm.events", disableEvents);
}

/**
Expand All @@ -2788,12 +2796,15 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
globals_set("orm.exception_on_failed_save", exceptionOnFailedSave);
}

if fetch exceptionOnFailedMetaDataSave, options["exceptionOnFailedMetaDataSave"] {
globals_set("orm.exception_on_failed_metadata_save", exceptionOnFailedMetaDataSave);
}

/**
* Enables/Disables literals in PHQL this improves the security of
* applications
* Allows to ignore unknown columns when hydrating objects
*/
if fetch phqlLiterals, options["phqlLiterals"] {
globals_set("orm.enable_literals", phqlLiterals);
if fetch ignoreUnknownColumns, options["ignoreUnknownColumns"] {
globals_set("orm.ignore_unknown_columns", ignoreUnknownColumns);
}

/**
Expand All @@ -2804,41 +2815,35 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
}

/**
* Enables/Disables automatic cast to original types on hydration
* Enables/Disables automatic not null validation
*/
if fetch castOnHydrate, options["castOnHydrate"] {
globals_set("orm.cast_on_hydrate", castOnHydrate);
if fetch notNullValidations, options["notNullValidations"] {
globals_set("orm.not_null_validations", notNullValidations);
}

/**
* Allows to ignore unknown columns when hydrating objects
* Enables/Disables literals in PHQL this improves the security of
* applications
*/
if fetch ignoreUnknownColumns, options["ignoreUnknownColumns"] {
globals_set("orm.ignore_unknown_columns", ignoreUnknownColumns);
if fetch phqlLiterals, options["phqlLiterals"] {
globals_set("orm.enable_literals", phqlLiterals);
}

if fetch caseInsensitiveColumnMap, options["caseInsensitiveColumnMap"] {
globals_set(
"orm.case_insensitive_column_map",
caseInsensitiveColumnMap
);
if fetch prefetchRecords, options["prefetchRecords"] {
globals_set("orm.resultset_prefetch_records", prefetchRecords);
}

if fetch updateSnapshotOnSave, options["updateSnapshotOnSave"] {
globals_set("orm.update_snapshot_on_save", updateSnapshotOnSave);
}

if fetch disableAssignSetters, options["disableAssignSetters"] {
globals_set("orm.disable_assign_setters", disableAssignSetters);
/**
* Enables/Disables virtual foreign keys
*/
if fetch virtualForeignKeys, options["virtualForeignKeys"] {
globals_set("orm.virtual_foreign_keys", virtualForeignKeys);
}

if fetch prefetchRecords, options["prefetchRecords"] {
globals_set("orm.resultset_prefetch_records", prefetchRecords);
}

if fetch lastInsertId, options["castLastInsertIdToInt"] {
globals_set("orm.cast_last_insert_id_to_int", lastInsertId);
}
}

/**
Expand Down
29 changes: 23 additions & 6 deletions phalcon/Mvc/Model/MetaData.zep
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,18 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
*/
public function write(string! key, array data) -> void
{
var result;
var result, option;

let result = this->adapter->set(key, data);
try {
let option = globals_get("orm.exception_on_failed_metadata_save"),
result = this->adapter->set(key, data);

if !result {
throw new Exception(
"Failed to store metaData to the cache adapter."
);
if false === result {
this->throwWriteException(option);
}

} catch \Exception {
this->throwWriteException(option);
}
}

Expand Down Expand Up @@ -866,4 +870,17 @@ abstract class MetaData implements InjectionAwareInterface, MetaDataInterface
*/
this->{"write"}(prefixKey, modelColumnMap);
}

private function throwWriteException(var option) -> void
{
if option {
throw new Exception(
"Failed to store metaData to the cache adapter"
);
} else {
trigger_error(
"Failed to store metaData to the cache adapter"
);
}
}
}
19 changes: 15 additions & 4 deletions phalcon/Mvc/Model/MetaData/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,23 @@ class Stream extends MetaData
*/
public function write(string! key, array data) -> void
{
var path;
var path, option;

let path = this->metaDataDir . prepare_virtual_path(key, "_") . ".php";
try {
let path = this->metaDataDir . prepare_virtual_path(key, "_") . ".php",
option = globals_get("orm.exception_on_failed_metadata_save");

if file_put_contents(path, "<?php return " . var_export(data, true) . "; ") === false {
throw new Exception("Meta-Data directory cannot be written");
file_put_contents(path, "<?php return " . var_export(data, true) . "; ");
} catch \Exception {
if option {
throw new Exception(
"Meta-Data directory cannot be written"
);
} else {
trigger_error(
"Meta-Data directory cannot be written"
);
}
}
}
}
68 changes: 68 additions & 0 deletions tests/integration/Mvc/Model/MetaData/Libmemcached/WriteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@

namespace Phalcon\Test\Integration\Mvc\Model\MetaData\Libmemcached;

use function file_exists;
use function file_put_contents;
use function getOptionsLibmemcached;
use IntegrationTester;
use function outputDir;
use Phalcon\Cache\AdapterFactory;
use Phalcon\Mvc\Model\Exception;
use Phalcon\Mvc\Model\MetaData\Libmemcached;
use Phalcon\Mvc\Model\MetaData\Stream;
use Phalcon\Storage\SerializerFactory;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use Phalcon\Test\Models\Robots;
use function var_dump;

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

/**
* Tests Phalcon\Mvc\Model\MetaData\Libmemcached :: write()
*
Expand All @@ -30,4 +44,58 @@ public function mvcModelMetadataLibmemcachedWrite(IntegrationTester $I)
$I->wantToTest('Mvc\Model\MetaData\Libmemcached - write()');
$I->skipTest('Need implementation');
}

/**
* Tests Phalcon\Mvc\Model\MetaData\Libmemcached :: write() - exception
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function mvcModelMetadataLibmemcachedWriteException(IntegrationTester $I)
{
$I->wantToTest('Mvc\Model\MetaData\Libmemcached - write() - exception');

$this->setNewFactoryDefault();
$this->setDiMysql();
$this->container->set(
'modelsMetadata',
function () {
$options = getOptionsLibmemcached();
$options['servers'][0]['port'] = 4000;

$serializer = new SerializerFactory();
$factory = new AdapterFactory($serializer);

return new Libmemcached($factory, $options);
}
);

$I->expectThrowable(
new \Exception('Failed to store metaData to the cache adapter'),
function () use ($I) {

Robots::setup(
[
'exceptionOnFailedMetaDataSave' => true,
]
);

Robots::findFirst(1);
}
);

$I->expectThrowable(
new \Exception('Failed to store metaData to the cache adapter', 1024),
function () use ($I) {

Robots::setup(
[
'exceptionOnFailedMetaDataSave' => false,
]
);

Robots::findFirst(1);
}
);
}
}
Loading