2008年12月8日星期一

Re: [fw-db] MultiCheckBox Populate form, Insert/Update db how to?

Thanks for the help. I will read and try to understand this :)
 
One quick question: Since I have no checks like allowed roles etc. Can't I just delete all rows for the news_id in news2newstpe table and then insert the new data from the form. Or is this a bad idea?
 
-R

 
On 12/9/08, Bart McLeod <mcleod@spaceweb.nl> wrote:
Need some direction/help/sample code to achieve...
1. Save the relationship to the article2tags table when creating a new article

//example from user2role relationship:
    protected function processRoles($username){
        $roles_changed = false;
        $userRole = new CmsUserRole();
        $role = new CmsRole();
        $roles = ListProvider::get('role');
        $role_ids = array_keys($roles);
        $current_user_roles = $userRole->getRolesForUser($username);
        $submitted_roles = $this->getRequest()->getParam('roles');
        if(! is_array($submitted_roles)){
            $submitted_roles = array();
        }
        foreach ($role_ids as $role_id) {
            $role->populate(array('id' => $role_id));
            $role->read(null, true);
            $user_role_data = array(
                'username' => $username,
                'role_id'  => $role_id
            );
            if(in_array($role_id, $submitted_roles)){
                //the user has or gets the role
                if(in_array($role_id, $current_user_roles)){
                    //the user already has the role
                }else{
                    //the user gets the role
                    if(Admin_RolesController::isSuperUser($role_id)){
                        $this->view->message = $this->view->translate("You can't assign the Superuser role.");
                        return false;
                    }
                    $userRole->populate($user_role_data);
                    if(! $userRole->create()){
                        //@todo: improve messaging, allow for multiple errors
                        $this->view->message = $this->view->translate("Could not assing role $role->role to user '$username'");
                    }else{
                        $roles_changed = true;
                    }
                }
            }else{
                //the user has not or loses the rule
                if(in_array($role_id, $current_user_roles)){
                    if(Admin_RolesController::isSuperUser($role_id)){
                        $this->view->message = $this->view->translate("You can't unassign the Superuser role.");
                        return false;
                    }                   
                    //the user loses the role
                    $userRole->populate($user_role_data);
                    if($userRole->delete() !== 1){
                        //@todo: improve messaging, allow for multiple errors
                        $this->view->message = $this->view->translate("Could not remove role $role->role from user '$username'");
                    }else{
                        $roles_changed = true;                 
                    }
                }else{
                    //the user did not have the role to begin with
                }               
            }
        }
        return $roles_changed;
    }   

//oops, that one was quite long, it is called when creating or updating a user, so it applies to question 3 too.

2. Populate form with tags selected (check boxes) when editing the article
//example of checking the roles that apply to the user:
    protected  function populateForm($form, $user){
        $userRole = new CmsUserRole();
        $current_roles = $userRole->getRolesForUser($user->username);
        $role_data = array('roles' => $current_roles);
        $form->populate(array_merge($user->getData(), $role_data));
        $form->addExists();
        $this->_exists = true;//important for grouping!
        $form->getElement('delete')->setAttrib('disabled',null);
       
        if ($this->isUserSuperuser($user->username)) {
              //the user being edited here is a Superuser, you may not delete it, change him etc, unless you are the superuser yourselfs
              $form->getElement('roles')->setAttrib('disabled', true);
              $form->getElement('delete')->setAttrib('disabled', true);
              $form->getElement('save')->setAttrib('disabled', true);
              $form->getElement('password')->setAttrib('disabled', true);
              $form->getElement('is_locked')->setAttrib('disabled', true);
        }
       
        return $form;
    } 
3. Insert/Update the "article2tags" on updating the article
see example 1

Hope this gives you a hint,

-Bart

Raavi Raaj schreef:
Thanks guys.

@ Bart: Yes, i'd like to see a few examples. And yes can wait for your reply.

-R

On Mon, Dec 8, 2008 at 1:08 AM, Bart McLeod <mcleod@spaceweb.nl> wrote:
personally, I process the related records in a separte function, I have quite a few examples if you are interested, but will not have time before tomorrow,
Bart

Raavi Raaj schreef:
Hi,

I have three tables...
1. article
2. tags
3. article2tags".

The "article2tags" stores a a many-to-many relationship (made up of article_id and tag_id).

While creating/editing an article:
Need some direction/help/sample code to achieve...
1. Save the relationship to the article2tags table when creating a new article
2. Populate form with tags selected (check boxes) when editing the article
3. Insert/Update the "article2tags" on updating the article

All help is appreciated.

regards,
-R

P.S. My tables are MyISAM.

 


--
Bart McLeod
Space Web Internet Team
Middenlaan 47
6865 VN Heveadorp
The Netherlands
t +31(0)26 3392952
m 06 51 51 89 71
@ info@spaceweb.nl
www.spaceweb.nl
zce logo zce PHP 5 logo

Bart McLeod is a Zend Certified Engineer.

Click to verify!


没有评论: