Re: Using DB_DataOjbect and Quickform together

From: Date: Thu, 06 Mar 2003 14:27:33 +0000
Subject: Re: Using DB_DataOjbect and Quickform together
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-4062@lists.php.net to get a copy of this message
It's probably worth wrapping DataObjects and adding a toArray() or a toQF() method to the new base class - this may be simpler function toQF($key) { foreach($this->_get_table() as $k=>$v) $ret["{$key}[{$k}]"] = $this->$k; } return $ret; } myForm = & new UserForm('update', $dataObject->toQuickForm('user')); Regards Alan Demian Turner wrote:
Hi, I'm trying to integrate Quickform and DB_DataOjbect for a simple UserManager, the problem is how to feed a DB_DataOjbect object to the setDefaults() method in Quickform, so the form can be pre-populated. The form is setup so all the attributes are elements of the array 'user':
    user[username]
    user[password], etc
so the whole user entity can be cast to an object easily
    $oUser = (object)$_POST['user']
Any DB_DataOjbect will know this is very handy for updating entities:
    $modUser->setFrom($oUser);
The problem I'm having is that a major transform of the entity is necessary to get it into a format compantible with QF. Apologies in advance for the following code, there's probably an easier way of doing this ;-) The question is, is there any interest in creating a setDefaultsFromDataObject() method in Quickform? Two such great packages it seems a shame not to use them in conjunction. cheers, Demian class example {
    function process($input, $output)
    {
        //    method receive a userID from POST
            $output->template = 'userMgrAdd.html';
            $oUser = & new DataObjects_User;
            $oUser->get($input->userID);
            //    convert object to array
            $aUser = (array)$oUser;
            //    remove blank elements or array_flip fails
            $aUserClean1 = array_filter($aUser, 'strlen');
            //    flip $keys/$values in order to be able
            //    to perform array_walk
            $aUserSwap = array_flip($aUserClean1);
            //    remove private members, anything
            //    prefixed with '_'
            $aUserClean2 = array_filter($aUserSwap,
                array($this, 'dbDataObjectClean'));
            //    modify elements so assoc. array
            //    (originally User ojb.)
            //    can be passed to quickform, of format
            //        user[username] = 'fred';
            //        user[passwd] = 'bloggs'
            //        ... etc
            array_walk($aUserClean2,
                array($this, 'dbDataObject2Quickform'));
            //    flip back to be passed
            //    to $oQuickForm->setDefaults()
            $aUserQF = array_flip($aUserClean2);
            //    create QF form wrapper object
            $myForm = & new UserForm('update', $aUserQF);
            $output->form = $myForm->init();
    }
    function dbDataObjectClean($element)
    {
        $pos = strpos($element, "_");
        return ($pos == 1)?true:false;
    }
    function dbDataObject2Quickform(&$item, $key)
    {
        $item = "user[".$item . "]";
    }
}


« previous php.pear.general (#4062) next »