kfirufk / zend-db-model-generator

Automatically exported from code.google.com/p/zend-db-model-generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not autoloading sub rows

GoogleCodeExporter opened this issue · comments

Hello all,

I have the following database setup:

Category
CREATE TABLE `Category` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(250) CHARACTER SET latin1 DEFAULT NULL,
  `name_url` varchar(250) CHARACTER SET latin1 DEFAULT NULL,
  `description` tinytext CHARACTER SET latin1,
  `order` int(11) DEFAULT NULL,
  `enabled` tinyint(1) DEFAULT '1',
  `updated` int(11) DEFAULT NULL,
  `created` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Forum
CREATE TABLE `Forum` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(250) CHARACTER SET latin1 DEFAULT NULL,
  `name_url` varchar(250) CHARACTER SET latin1 DEFAULT NULL,
  `description` tinytext CHARACTER SET latin1,
  `meta_description` varchar(400) CHARACTER SET latin1 DEFAULT NULL,
  `meta_keywords` varchar(400) CHARACTER SET latin1 DEFAULT NULL,
  `threads` int(11) unsigned DEFAULT '0',
  `views` int(11) unsigned DEFAULT '0',
  `private` tinyint(1) DEFAULT '0',
  `enabled` tinyint(1) DEFAULT '1',
  `updated` int(11) DEFAULT NULL,
  `created` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Category_Forum
CREATE TABLE `Category_Forum` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `category_id` int(11) unsigned DEFAULT NULL,
  `forum_id` int(11) unsigned DEFAULT NULL,
  `order` tinyint(3) DEFAULT NULL,
  `enabled` tinyint(1) DEFAULT '1',
  `updated` int(11) DEFAULT NULL,
  `created` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `category_id` (`category_id`),
  KEY `forum_id` (`forum_id`),
  CONSTRAINT `category_forum_ibfk_2` FOREIGN KEY (`forum_id`) REFERENCES `Forum` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `category_forum_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `Category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

I have this following code:
    public function categoryAction () {

        $this->mapper = new ORM_Model_Mapper_Category();
        $this->model = new ORM_Model_Category();
        $this->dbTable = new ORM_Model_DbTable_Category();

        if (isset($this->variables['id']) && is_numeric($this->variables['id'])) {
            $output = $this->model->find($this->variables['id'])->toObject();

            if ($output->id == null) {
                return $this->_setError(404);
            }

        } else {
            $output = $this->mapper->fetchListToObject(array('1 = 1'));
        }

        $output = $this->_loadRelatedCategory($output);
        $this->data = $output;
    }


    public function _loadRelatedCategory ($objects) {

        $single = false;

        if (!is_array($objects)) {
            $objects = array($objects);
            $single = true;
        }

        foreach ($objects as $object) {
            $this->model->setOptions((array) $object);

            $object->forums = $this->model->getCategoryForum(true);


            if (!empty($object->forums)) {
                foreach ($object->forums as $forum) {
                    $forum = $forum->toArray();
                }
            }

            if ($single) {
                return $object;
            }
        }
        return $objects;
    }


Which results with this output:
{
id: "1",
name: "Wuggawoo",
name_url: "wuggawoo",
description: null,
order: "1",
enabled: "1",
updated: null,
created: null,
forums: [
{ },
{ },
{ },
{ }
]
},
{
id: "2",
name: "Miscellaneous interest",
name_url: "miscellaneous-interest",
description: null,
order: "2",
enabled: "1",
updated: null,
created: null,
forums: [
{ },
{ },
{ },
{ }
]
},
{
id: "3",
name: "Private",
name_url: "private",
description: null,
order: "3",
enabled: "1",
updated: null,
created: null,
forums: [
{ },
{ },
{ },
{ }
]
},
{
id: "5",
name: "Websites",
name_url: "websites",
description: null,
order: "5",
enabled: "1",
updated: null,
created: null,
forums: [
{ },
{ },
{ },
{ }
]
}
]


Note that forums is empty and each forums array has 4 items. There are 2 items 
for some, 4 for others.

The ORM is not behaving as I would expect. It should load the forums related to 
that category via the getCategoryForum(true)

(note that FetchListToObject is the same as FetchListToArray but with (object) 
cast before)

Original issue reported on code.google.com by Aa...@theaudience.com on 21 Sep 2012 at 5:57