开发者

Recursive function·

开发者 https://www.devze.com 2022-12-14 00:57 出处:网络
4Computer 56food1 6food2 I need to select possible parents. For example: If we select \'machine\' all other开发者_Python百科s can parents
    4             Computer
    5      6      food1
    6             food2

I need to select possible parents.

For example:

  1. If we select 'machine' all other开发者_Python百科s 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 others have possibility.

How do I write a (PHP, MySQL recursive) function in a class? Note: only using a single function.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消