{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/11457","repository_url":"https://api.github.com/repos/phalcon/cphalcon","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11457/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11457/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11457/events","html_url":"https://github.com/phalcon/cphalcon/issues/11457","id":136798070,"number":11457,"title":"[2.1.x] Relation manyToMany saved double because of afterCreate event","user":{"login":"Zaszczyk","id":7737337,"avatar_url":"https://avatars.githubusercontent.com/u/7737337?v=3","gravatar_id":"","url":"https://api.github.com/users/Zaszczyk","html_url":"https://github.com/Zaszczyk","followers_url":"https://api.github.com/users/Zaszczyk/followers","following_url":"https://api.github.com/users/Zaszczyk/following{/other_user}","gists_url":"https://api.github.com/users/Zaszczyk/gists{/gist_id}","starred_url":"https://api.github.com/users/Zaszczyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zaszczyk/subscriptions","organizations_url":"https://api.github.com/users/Zaszczyk/orgs","repos_url":"https://api.github.com/users/Zaszczyk/repos","events_url":"https://api.github.com/users/Zaszczyk/events{/privacy}","received_events_url":"https://api.github.com/users/Zaszczyk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-26T19:56:59Z","updated_at":"2016-02-26T20:52:02Z","closed_at":null,"body":"I have model Activity, which has a lot relations, including hasMany and manyToMany:\r\n\r\n```\r\n$this->hasMany('id', 'XXX\\Models\\Entities\\Activity\\ActivityParameter', 'activity_id', [\r\n            'alias' => 'parameters',\r\n            'reusable' => true,\r\n            'foreignKey' => [\r\n                'action' => Relation::ACTION_CASCADE\r\n            ]\r\n        ]);\r\n\r\n$this->hasManyToMany(\r\n            'id',\r\n            'XXX\\Models\\Entities\\Activity\\ActivityCollaborator',\r\n            'activity_id', 'collaborator_id',\r\n            'XXX\\Models\\Entities\\User\\User',\r\n            'id',\r\n            ['alias' => 'collaborators', 'reusable' => true]\r\n        );\r\n```\r\nUser's relation:\r\n```\r\n\r\n$this->hasManyToMany(\r\n            'id',\r\n            'XXX\\Models\\Entities\\Activity\\ActivityCollaborator',\r\n            'user_id', 'activity_id',\r\n            'FitCompany\\Models\\Entities\\Activity\\Activity',\r\n            'id', ['alias' => 'collaboratedActivities', 'reusable' => true]\r\n        );\r\n```\r\n\r\nActivityParameter's relation:\r\n\r\n```\r\n$this->belongsTo('activity_id', 'XXX\\Models\\Entities\\Activity\\Activity', 'id', [\r\n            'alias' => 'activity',\r\n            'reusable' => true,\r\n            'foreignKey' => [\r\n                'allowNulls' => false,\r\n                'message' => 'The activity doest not exist'\r\n            ]\r\n        ]);\r\n\r\n```\r\n\r\nand it was working right, I've created new objects and relations were OK. But I added afterCreate event to model Activity with code:\r\n```\r\npublic function afterCreate(Event $event, Activity $activity)\r\n{\r\n$aService = new ActivityService();\r\n$activity->parameters = [$aService->getSpecialParameter() $adService->getAnotherSpecialParameter()];\r\n\r\n            if (!$activity->save()) {\r\n                $messages = $activity->getMessages();\r\n                throw new CreateEntityException($messages[0]);\r\n            }\r\n}\r\n```\r\n\r\nThis code adds extra parameters to Activity object and save again new object. After this saving there are duplicated records in databse for collaborators related. For example, when I create new Activity and relate 1 User via collaborators relation, it is stored right, but afterCreate event duplicate relation collaborators for this new Activity object. If I comment afterCreate event, it will work right again.\r\n\r\nIs it problem with Phalcon or with my implementation of relations?\r\n\r\nPHP 5.6\r\nPhalcon 2.1.x\r\nMariaDB"},"comment":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/comments/189479333","html_url":"https://github.com/phalcon/cphalcon/issues/11457#issuecomment-189479333","issue_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11457","id":189479333,"user":{"login":"sergeyklay","id":1256298,"avatar_url":"https://avatars.githubusercontent.com/u/1256298?v=3","gravatar_id":"","url":"https://api.github.com/users/sergeyklay","html_url":"https://github.com/sergeyklay","followers_url":"https://api.github.com/users/sergeyklay/followers","following_url":"https://api.github.com/users/sergeyklay/following{/other_user}","gists_url":"https://api.github.com/users/sergeyklay/gists{/gist_id}","starred_url":"https://api.github.com/users/sergeyklay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sergeyklay/subscriptions","organizations_url":"https://api.github.com/users/sergeyklay/orgs","repos_url":"https://api.github.com/users/sergeyklay/repos","events_url":"https://api.github.com/users/sergeyklay/events{/privacy}","received_events_url":"https://api.github.com/users/sergeyklay/received_events","type":"User","site_admin":false},"created_at":"2016-02-26T20:52:02Z","updated_at":"2016-02-26T20:52:02Z","body":"var_dump/die detected :smile: "}}