views:

70

answers:

1

Hi i have the below table datas

   id  parent_id  name
    1          Machine
    2    3       Ram
    3    4       Cpu
    4          Computer
    5     6        food1
    6              food2

My need is to select possible parents

    for example 
    1) if we select 'machine' all others can parents
    2) if we select 'cpu' then 'ram' and 'cpu' should not be there
    3) if we take 'food2' then 'food2', 'food1' should not be there all other have possibility .

How i write a (php,mysql recursive) function in a class . Note :- Only use single function Pls help me Thanks in advance .

+2  A: 

i have the same issue

class test {

public static $roleIds;

public function availableParents() { self::$roleIds = null;

    $discardRoleIds = implode(',',$this->unAvailableRoles());

    $parentRoles = Acl_Model_Role::fetchAll("id NOT IN ({$discardRoleIds}) AND isactive = true" );

    return $parentRoles;        
}

/**
 * Getting  not available role id
 * 
 * @author Linto
 * @since 2009-11-25
 * @return Array 
 */
public function unAvailableRoles()
{
    $where = "parent_id = {$this->getId()}"; 

    self::$roleIds[] = $this->getId();       

    $count = Acl_Model_Role::count($where);

    if($count != 0) {
        foreach(Acl_Model_Role::fetchAll($where) as $role){                   
            $role ->unAvailableRoles();                
        }         
    }
    return  self::$roleIds; 

}

}

it will work fine, if we use one time

if situation is like this $role //created object with id 1

$role->availableParents()

$role //created object with id 2 $role->availableParents()

$role //created object with id 3 $role->availableParents()

we will not get correct answer (first lines will change the second model too.)

Linto davis