This is a sweet "hack" indeed! I'll keep it in mind when working with difficult nested stuff again! I'll support the award idea ; ).



DJ Spark schrieb:
I use this in my Gallery Model:

var $hasMany =  array(
			'Galleries' => array(
				'className' => 'Gallery',
				'order'		=> 'Galleries.ord ASC',
				'foreignKey'   => 'parent_id'));
var $belongsTo =  array(
			'Galleries' => array(
				'className' => 'Gallery',
				'order'		=> 'Galleries.ord ASC',
				'foreignKey'   => 'parent_id'));

First, note the 'Galleries' (you change that association name for any
other, just dont use  the model name). Second, you might want to know
more about $this->Model->findAllThreaded() . It returns a *VERY*
useful array , with ['child'] and everything. I *heart* CakePHP :)

 spakr


On 6/15/06, I. E. Smith-Heisters <[EMAIL PROTECTED]> wrote:
  
Hi all,

I've found a few threads on related subjects, but none of them offer a
solution:

http://groups.google.com/group/cake-php/browse_thread/thread/8ad0ebbb9171b5a4/82f28d0b433191be?q=finderSQL&rnum=7#82f28d0b433191be
http://groups.google.com/group/cake-php/browse_thread/thread/ed9f11b62fd2d223/c46ddbf9860e0be6?q=finderSQL&rnum=3#c46ddbf9860e0be6
http://groups.google.com/group/cake-php/browse_frm/thread/aba68ec53a2ae064/9988348a93f13d1e#9988348a93f13d1e

I have a table that looks like this:

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra
|
+-----------+------------------+------+-----+---------+----------------+
| id        | int(10) unsigned |      | PRI | NULL    | auto_increment
|
| name      | varchar(255)     | YES  |     | NULL    |
|
| ui_name   | varchar(50)      |      |     |         |
|
| parent_id | int(10) unsigned | YES  |     | NULL    |
|
+-----------+------------------+------+-----+---------+----------------+

which represents nested menus. I would think it would be trivial to
allow each MenuItem to have children (whose parent_id is equal to its
id) by using hasMany.

So, first I tried this:
class MenuItem extends AppModel {
        var $name = 'MenuItem';
        var $useTable = 'tester';

        var $validate = array (
                'name' => VALID_NOT_EMPTY,
        );
        var $hasMany = 'MenuItem';
}

but that just overwrites the main node,
$this->MenuItem->read():
array(1) {
  ["MenuItem"]=>
  array(0) {
  }
}

so I tried something like this instead:

        var $hasMany = array (
                'child' => array (
                        'className' => 'MenuItem',
                )
        );

which at least doesn't overwrite the main object, but still doesn't
populate the child array:
array(2) {
  ["MenuItem"]=>
  array(4) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(9) "community"
    ["ui_name"]=>
    string(9) "Community"
    ["parent_id"]=>
    NULL
  }
  ["child"]=>
  array(0) {
  }
}
So then I tried a finderSQL parameter

        var $hasMany = array (
                'child' => array (
                        'className' => 'MenuItem',
                        'finderSQL' => 'SELECT * FROM tester AS one LEFT JOIN tester AS two
ON one.id = two.parent_id'
                )
        );

but that has the same output on read() as the attempt with finderSQL
undefined. Unfortunately, I haven't found a single successful example
of using finderSQL, so it's hard to tell what sort of output the SQL
statement should have. I just grabbed and modded this statement from
one of the other threads I mentioned.

Okay, so then there's findAllThreaded and guiListTree. The former seems
to work (albeit it's kind of uncontrollable), but I really think this
part of the model belongs in the model definition.. The latter,
guiListTree doesn't seem to work as expected and is marked as
deprecated in the documentation..

So, does anyone have any suggestions for building a tree out of a
self-joining table?

Thanks much,
Ian Smith-Heisters


    


  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to