I have a Categories Model that references itself to create Child
Categories. It also has multiple Has Many associations with other
models that I use, such as Images, Documents, and Content.
The basic logic is that all associated models have a category_id that
keys into a child category's id, essentially placing every entry in a
child category of a parent category.
This whole system worked fine with the previous main alpha releases.
When I updated to Cake's pre-Beta, I noticed several problems. First,
it seems that recursive association results have changed. When I do a
findAll in Categories with my recursive level set to 2, Child
Categories are returned, but not their associated model entries.
Currently, my Categories model looks like this:
class Category extends AppModel
{
var $name = 'Category';
var $belongsTo = array('ParentCategory'=>
array('className'=>'Category',
'foreignKey'=>'parent_id'
)
);
var $hasMany = array('ChildCategory'=>
array('className'=>'Category',
'foreignKey'=>'parent_id',
'order'=>array('ChildCategory.ordering'=>'ASC'),
'dependent'=>true
),
'Content'=>
array('className'=>'Content',
'foreignKey'=>'category_id',
'order'=>array('Content.ordering'=>'ASC'),
'dependent'=>true
),
'News' =>
array('className' => 'News',
'conditions' => '',
'order' =>
array('News.newsDate'=>'DESC'),
'limit' => '',
'foreignKey' => 'category_id',
'dependent' => true,
'exclusive' => false,
'finderQuery' => ''
),
'Image' =>
array('className' => 'Image',
'conditions' => '',
'order' =>
array('Image.ordering'=>'ASC'),
'limit' => '',
'foreignKey' => 'category_id',
'dependent' => true,
'exclusive' => false,
'finderQuery' => ''
), etc.....
}
Up until the latest alpha of Cake 1.2, any recursive findAll calls to
the Category model would return categories, category children, and the
model associations to the category children. For example:
$this->Document->Category->recursive = 2;
$this->Document->Category-
>findAll(array('Category.model'=>'Document','Category.parent_id'=>'0'),null,array('Category.ordering'=>'ASC'));
Would return:
[2] => Array
(
[Category] => Array
(
[id] => 131
[parent_id] => 0
[numberOfChildren] =>
[options] => a:1:{i:0;s:1:"1";}
[name] => Newsletters
[teaser] =>
[description] =>
[model] => Document
[ordering] => 3
[created] => 2007-10-16 14:02:33
[modified] => 2007-10-16 14:27:26
)
[ParentCategory] => Array
(
[id] =>
[parent_id] =>
[numberOfChildren] =>
[options] =>
[name] =>
[teaser] =>
[description] =>
[model] =>
[ordering] =>
[created] =>
[modified] =>
)
[ChildCategory] => Array
(
[0] => Array
(
[id] => 132
[parent_id] => 131
[numberOfChildren] =>
[options] =>
[name] => Default
[teaser] =>
[description] =>
[model] => Document
[ordering] => 1
[created] => 2007-10-16 14:02:33
[modified] => 2007-10-16 14:02:33
[ParentCategory] => Array
(
[id] => 131
[parent_id] => 0
[numberOfChildren] =>
[options] => a:1:{i:0;s:1:"1";}
[name] => Newsletters
[teaser] =>
Click here for the archive of the Farm to School quarterly newsletter.
[description] =>
[model] => Document
[ordering] => 3
[created] => 2007-10-16 14:02:33
[modified] => 2007-10-16 14:27:26
)
[Content] => Array
(
)
[News] => Array
(
)
[Image] => Array
(
)
[Document] => Array
(
[0] => Array
(
[id] => 122
[category_id] => 132
[name] => 2005 Spring
Newsletter
[description] => 2005
Spring Newsletter
[filename] => spring05.pdf
[filesize] => 622914
[filetype] => application/
pdf
[displayed] => 1
[ordering] => 1
[created] => 2007-10-16
14:36:42
[modified] => 2007-10-16
14:36:42
)
[1] => Array
(
[id] => 121
[category_id] => 132
[name] => 2005 Summer
Newsletter
[description] => 2005
Summer Newsletter
[filename] =>
fts_newssummer05.pdf
[filesize] => 685015
[filetype] => application/
pdf
[displayed] => 1
[ordering] => 2
[created] => 2007-10-16
14:36:22
[modified] => 2007-10-16
14:36:22
)
[2] => Array
(
[id] => 120
[category_id] => 132
[name] => 2005 Fall
Newsletter
[description] => 2005 Fall
Newsletter
[filename] =>
ftsnewsfall05.pdf
[filesize] => 389689
[filetype] => application/
pdf
[displayed] => 1
[ordering] => 3
[created] => 2007-10-16
14:35:57
[modified] => 2007-10-16
14:35:57
)
)
[County] => Array
(
)
)
)
[Content] => Array
(
)
[News] => Array
(
)
[Image] => Array
(
)
[Document] => Array
(
)
[County] => Array
(
)
)
As of the CakePHP pre-Beta, the same call returns:
[2] => Array
(
[Category] => Array
(
[id] => 131
[parent_id] => 0
[numberOfChildren] =>
[options] => a:1:{i:0;s:1:"1";}
[name] => Newsletters
[teaser] =>
[description] =>
[model] => Document
[ordering] => 3
[created] => 2007-10-16 14:02:33
[modified] => 2007-10-16 14:27:26
)
[ParentCategory] => Array
(
[id] =>
[parent_id] =>
[numberOfChildren] =>
[options] =>
[name] =>
[teaser] =>
[description] =>
[model] =>
[ordering] =>
[created] =>
[modified] =>
)
[ChildCategory] => Array
(
[0] => Array
(
[id] => 132
[parent_id] => 131
[numberOfChildren] =>
[options] =>
[name] => Default
[teaser] =>
[description] =>
[model] => Document
[ordering] => 1
[created] => 2007-10-16 14:02:33
[modified] => 2007-10-16 14:02:33
)
)
[Content] => Array
(
)
[News] => Array
(
)
[Image] => Array
(
)
[Document] => Array
(
)
[County] => Array
(
)
)
Notice that all associated data to the ChildCategory is not returned.
I've also noticed that when I try to use saveField with a category
entry within an associated model, I get a database error. For
example, in images_controller.php, in the admin_edit function:
$this->Image->Category->id = $category['ParentCategory']['id'];
$this->Image->Category->saveField('options',serialize($options));
Returns the error:
Query: UPDATE `categories` SET `id` = 67,`parent_id` =
66,`numberOfChildren` = NULL,`options` = 'a:2:{i:0;s:1:\"0\";i:1;s:
0:\"\";}',`name` = 'Default',`teaser` = NULL,`description` =
NULL,`model` = 'Image',`ordering` = 1,`created` = '2007-08-27
17:33:24',`modified` = '2007-08-27 17:33:24' WHERE `id` IN (66)
Warning (512): SQL Error: 1062: Duplicate entry '67' for key 1 [CORE/
cake/libs/model/datasources/dbo_source.php, line 440]
Of course there's a duplicate entry, I'm UPDATE-ing it!
When I try using debug($this->Image->Category) in this situation, I
get an endlessly-increasing readout until my browser crashes from the
amount of data displayed on the screen. It's like an endless loop.
I feel that the above two problems are somehow related. Was there a
change in how Cake handles self-referential model associations?
Any help or advice would be appreciated.
Thanks,
Casey
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---