Skip to content

Commit

Permalink
Merge pull request #1317 from sjinks/issue-1314
Browse files Browse the repository at this point in the history
[1.3.0] Fix #1314
  • Loading branch information
Phalcon committed Sep 30, 2013
2 parents 9d16ea9 + 5643691 commit 60200da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
20 changes: 7 additions & 13 deletions ext/mvc/model/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, getWriteConnectionService){
PHP_METHOD(Phalcon_Mvc_Model_Manager, notifyEvent){

zval *event_name, *model, *status = NULL, *behaviors, *entity_name = NULL;
zval *models_behaviors, *behavior = NULL, *events_manager;
zval *models_behaviors, *behavior = NULL, *events_manager, *mgr;
zval *fire_event_name = NULL, *custom_events_manager;
HashTable *ah0;
HashPosition hp0;
Expand All @@ -798,20 +798,16 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, notifyEvent){
/**
* Dispatch events to the global events manager
*/
PHALCON_OBS_VAR(behaviors);
phalcon_read_property_this(&behaviors, this_ptr, SL("_behaviors"), PH_NOISY_CC);
behaviors = phalcon_fetch_nproperty_this(this_ptr, SL("_behaviors"), PH_NOISY_CC);
if (Z_TYPE_P(behaviors) == IS_ARRAY) {

PHALCON_INIT_VAR(entity_name);
phalcon_get_class(entity_name, model, 1 TSRMLS_CC);
if (phalcon_array_isset(behaviors, entity_name)) {
if (phalcon_array_isset_fetch(&models_behaviors, behaviors, entity_name)) {

/**
* Notify all the events on the behavior
*/
PHALCON_OBS_VAR(models_behaviors);
phalcon_array_fetch(&models_behaviors, behaviors, entity_name, PH_NOISY);

phalcon_is_iterable(models_behaviors, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
Expand All @@ -833,8 +829,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, notifyEvent){
/**
* Dispatch events to the global events manager
*/
PHALCON_OBS_VAR(events_manager);
phalcon_read_property_this(&events_manager, this_ptr, SL("_eventsManager"), PH_NOISY_CC);
events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY_CC);
if (Z_TYPE_P(events_manager) == IS_OBJECT) {

PHALCON_INIT_VAR(fire_event_name);
Expand All @@ -850,19 +845,18 @@ PHP_METHOD(Phalcon_Mvc_Model_Manager, notifyEvent){
/**
* A model can has a specific events manager for it
*/
PHALCON_OBS_VAR(custom_events_manager);
phalcon_read_property_this(&custom_events_manager, this_ptr, SL("_customEventsManager"), PH_NOISY_CC);
custom_events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_customEventsManager"), PH_NOISY_CC);
if (Z_TYPE_P(custom_events_manager) == IS_ARRAY) {

PHALCON_INIT_NVAR(entity_name);
phalcon_get_class(entity_name, model, 1 TSRMLS_CC);
if (phalcon_array_isset(custom_events_manager, entity_name)) {
if (phalcon_array_isset_fetch(&mgr, custom_events_manager, entity_name)) {

PHALCON_INIT_NVAR(fire_event_name);
PHALCON_CONCAT_SV(fire_event_name, "model:", event_name);

PHALCON_INIT_NVAR(status);
phalcon_call_method_p2(status, custom_events_manager, "fire", fire_event_name, model);
phalcon_call_method_p2(status, mgr, "fire", fire_event_name, model);
if (PHALCON_IS_FALSE(status)) {
RETURN_CTOR(status);
}
Expand Down
3 changes: 3 additions & 0 deletions unit-tests/ModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ protected function _executeTestsNormal($di){
$persona->refresh();
$this->assertEquals($personaData, $persona->toArray());

// Issue 1314
$parts = new Parts2();
$parts->save();
}

protected function _executeTestsRenamed($di)
Expand Down
13 changes: 13 additions & 0 deletions unit-tests/models/Parts2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class Parts2 extends \Phalcon\Mvc\Model
{
public function initialize()
{
$eventsManager = new \Phalcon\Events\Manager();

$eventsManager->attach('model', function($event, $robot) { return true; });
$this->setEventsManager($eventsManager);
$this->setSource('parts');
}
}

0 comments on commit 60200da

Please sign in to comment.