{"action":"created","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/1945","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1945/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1945/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1945/events","html_url":"https://github.com/phalcon/cphalcon/issues/1945","id":26612892,"number":1945,"title":"hasManyToMany count relation not working properly","user":{"login":"bkwint","id":521069,"avatar_url":"https://avatars.githubusercontent.com/u/521069?v=3","gravatar_id":"","url":"https://api.github.com/users/bkwint","html_url":"https://github.com/bkwint","followers_url":"https://api.github.com/users/bkwint/followers","following_url":"https://api.github.com/users/bkwint/following{/other_user}","gists_url":"https://api.github.com/users/bkwint/gists{/gist_id}","starred_url":"https://api.github.com/users/bkwint/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bkwint/subscriptions","organizations_url":"https://api.github.com/users/bkwint/orgs","repos_url":"https://api.github.com/users/bkwint/repos","events_url":"https://api.github.com/users/bkwint/events{/privacy}","received_events_url":"https://api.github.com/users/bkwint/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/phalcon/cphalcon/labels/Bug%20-%20Medium","name":"Bug - Medium","color":"d7e102"},{"url":"https://api.github.com/repos/phalcon/cphalcon/labels/Unassigned","name":"Unassigned","color":"fad8c7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2014-01-30T16:49:33Z","updated_at":"2015-03-26T09:44:29Z","closed_at":null,"body":"When using the count[Relation] magic method for a model, many to many relations are not counted, instead when count[Relation] is called a Phalcon\\Mvc\\Model\\Resultset\\Simple is returned. For a hasMany relation this does work.\r\n\r\nExample classes:\r\n~~~\r\nnamespace Models\\Translation;\r\n\r\nuse Phalcon\\Mvc\\Model;\r\n\r\nclass Language extends Model\r\n{\r\n    /**\r\n     * The tablename of the table we are interfacing with\r\n     */\r\n    const TABLE_NAME = 'Translation_Language';\r\n    \r\n    /**\r\n     * @var string $_iso Iso language code\r\n     */\r\n    protected $iso;\r\n    /**\r\n     * @var string $_fullname Full language name\r\n     */\r\n    protected $fullname;\r\n\r\n    /**\r\n     * The table name for this model should be returned\r\n     * by this function\r\n     *\r\n     * @return string Table name\r\n     */\r\n    public function getSource()\r\n    {\r\n        return self::TABLE_NAME;\r\n    }\r\n    /**\r\n     * On initialize we prepare the model\r\n     * \r\n     *  - Build relations\r\n     */\r\n    public function initialize()\r\n    {\r\n        $this->hasMany('iso', '\\Models\\Translation\\Message', 'language', array(\r\n            'alias' => 'translations'\r\n        ));\r\n        \r\n        $this->hasManyToMany(\r\n            'iso', \r\n            '\\Models\\Translation\\Message', \r\n            'language', \r\n            'id', \r\n            '\\Models\\Translation\\SourceMessage', \r\n            'id', \r\n            array(\r\n                'alias' => 'sourceMessages'\r\n            )\r\n        );\r\n    }\r\n}\r\n\r\nclass Message extends Model\r\n{\r\n    /**\r\n     * The name of the database table we are interfacing with\r\n     */\r\n    const TABLE_NAME = 'Translation_Message';\r\n    \r\n    /**\r\n     * The id of the source message we are interfacing with\r\n     * \r\n     * @var int\r\n     */\r\n    protected $id;\r\n    /**\r\n     * The iso code for the language we are working with\r\n     * \r\n     * @var string\r\n     */\r\n    protected $language;\r\n    /**\r\n     * The actual translation for the value\r\n     * \r\n     * @var string \r\n     */\r\n    protected $translation;\r\n    \r\n    /**\r\n     * What is the name of the table we should be interfacing with\r\n     * \r\n     * @return string\r\n     */\r\n    public function getSource()\r\n    {\r\n        return self::TABLE_NAME;\r\n    }\r\n    /**\r\n     * On initialize we define the relations that this model is supposed\r\n     * to have.\r\n     */\r\n    public function initialize()\r\n    {\r\n        $this->hasOne('language', '\\Models\\Translation\\Language', 'iso', array(\r\n            'alias' => 'lang'\r\n        ));\r\n    }\r\n}\r\n\r\nclass SourceMessage extends Model\r\n{\r\n    /**\r\n     * The database table we are interfacing with\r\n     */\r\n    const TABLE_NAME = 'Translation_SourceMessage';\r\n    \r\n    /**\r\n     * Message identifier\r\n     * \r\n     * @var int\r\n     */\r\n    protected $id;\r\n    /**\r\n     * Category for grouping messages to specific parts of the system\r\n     * \r\n     * @var string\r\n     */\r\n    protected $category;\r\n    /**\r\n     * The message that needs to be translated\r\n     * \r\n     * @var string\r\n     */\r\n    protected $message;\r\n    \r\n    /**\r\n     * Sets the tablename for the model\r\n     * \r\n     * @return string\r\n     */\r\n    public function getSource()\r\n    {\r\n        return self::TABLE_NAME;\r\n    }\r\n    /**\r\n     * Sets up the model for use\r\n     */\r\n    public function initialize()\r\n    {\r\n        $this->hasMany('id', '\\Models\\Translation\\Message', 'id', array(\r\n            'alias' => 'translations'\r\n        ));\r\n\r\n        $this->hasManyToMany(\r\n            'id', \r\n            '\\Models\\Translation\\Message', \r\n            'id', \r\n            'language', \r\n            '\\Models\\Translation\\Language', \r\n            'iso', \r\n            array(\r\n                'alias' => 'languages'\r\n        ));\r\n    }\r\n}\r\n~~~\r\n\r\nDatabase defintion:\r\n~~~\r\nCREATE TABLE IF NOT EXISTS `Translation_SourceMessage` (\r\n  `id` INT(11) NOT NULL AUTO_INCREMENT,\r\n  `category` VARCHAR(32) NOT NULL,\r\n  `message` TEXT NOT NULL,\r\n  PRIMARY KEY (`id`))\r\nENGINE = InnoDB\r\nAUTO_INCREMENT = 3\r\nDEFAULT CHARACTER SET = latin1;\r\n\r\nCREATE TABLE IF NOT EXISTS `Translation_Language` (\r\n  `iso` VARCHAR(7) NOT NULL,\r\n  `fullname` VARCHAR(45) NOT NULL,\r\n  PRIMARY KEY (`iso`))\r\nENGINE = InnoDB;\r\n\r\nCREATE TABLE IF NOT EXISTS `Translation_Message` (\r\n  `id` INT(11) NOT NULL DEFAULT '0',\r\n  `language` VARCHAR(7) NOT NULL,\r\n  `translation` TEXT NOT NULL,\r\n  PRIMARY KEY (`id`, `language`),\r\n  INDEX `fk_Translation_Message_Translation_Language1_idx` (`language` ASC),\r\n  CONSTRAINT `FK_Message_SourceMessage`\r\n    FOREIGN KEY (`id`)\r\n    REFERENCES `Translation_SourceMessage` (`id`)\r\n    ON DELETE CASCADE,\r\n  CONSTRAINT `fk_Translation_Message_Translation_Language1`\r\n    FOREIGN KEY (`language`)\r\n    REFERENCES `Translation_Language` (`iso`)\r\n    ON DELETE CASCADE\r\n    ON UPDATE CASCADE)\r\nENGINE = InnoDB\r\nDEFAULT CHARACTER SET = latin1;\r\n~~~\r\n\r\nExample code:\r\n~~~\r\n$language = Language::findFirst();\r\n$sourceMessage = $language->countSourceMessages(); //Phalcon\\Mvc\\Model\\Resultset\\Simple is returned in stead of an integer\r\n~~~\r\n\r\n<bountysource-plugin>\r\n---\r\nWant to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1675109-hasmanytomany-count-relation-not-working-properly?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F50707&utm_medium=issues&utm_source=github).\r\n</bountysource-plugin>"},"comment":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/comments/86424732","html_url":"https://github.com/phalcon/cphalcon/issues/1945#issuecomment-86424732","issue_url":"https://api.github.com/repos/phalcon/cphalcon/issues/1945","id":86424732,"user":{"login":"Rigiytip","id":2422544,"avatar_url":"https://avatars.githubusercontent.com/u/2422544?v=3","gravatar_id":"","url":"https://api.github.com/users/Rigiytip","html_url":"https://github.com/Rigiytip","followers_url":"https://api.github.com/users/Rigiytip/followers","following_url":"https://api.github.com/users/Rigiytip/following{/other_user}","gists_url":"https://api.github.com/users/Rigiytip/gists{/gist_id}","starred_url":"https://api.github.com/users/Rigiytip/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Rigiytip/subscriptions","organizations_url":"https://api.github.com/users/Rigiytip/orgs","repos_url":"https://api.github.com/users/Rigiytip/repos","events_url":"https://api.github.com/users/Rigiytip/events{/privacy}","received_events_url":"https://api.github.com/users/Rigiytip/received_events","type":"User","site_admin":false},"created_at":"2015-03-26T09:44:28Z","updated_at":"2015-03-26T09:44:28Z","body":"+1"}}