Recursive function·
4 Computer
5 6 food1
6 food2
I need to select possible parents.
For example:
- If we select 'machine' all other开发者_Python百科s can parents
- If we select 'cpu' then 'ram' and 'cpu' should not be there.
- 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.)
精彩评论