Insert for each multi select

edited April 2019 in Modules

I have a multi select of groups of users (User_Group table) and then a message text area so I can send a message to multiple groups.

I also have it insert into the table group_messages as group_id and message_text

On_after_save loops through the groups and sends the email via an email API I use.

Works great except inserting into the table one row per group_id. I obviously can just do a $this->db->insert() for each iteration but is there a way for FUEL to do this automatically like it does if it were a single insert?

Comments

  • Can you post the model and the on_after_save method?

  •    function form_fields($values = array(), $related = array())
        {  
            $fields = parent::form_fields($values, $related);
    
            // put in your own specific code to manipulate the fields here
            $fields['timeposted']['type'] = "hidden";
            $fields['timeposted']['value'] = date('Y-m-d H:i:s',time());
    
            $fields['packages_id']['type'] = 'multi';
            $fields['packages_id']['options'] = $this->get_packages();
    
            return $fields;
        }
    

    public function on_after_save($values){

    $packages = $values['packages_id'];
    if(!empty($packages)) {
        foreach ($packages as $package_id) {
    
            if ($package_id == 23) {
                $this->freeUsers($values);
            } else {
    
                $selection = $this->getSelectionById($values['id']);
    
    
                $users = $this->getUsersForSelection($package_id);
                $this->email->initialize(array(
                    'protocol' => 'smtp',
                   mail stuff here
                ));
    
                foreach ($users as $u) {
                    if (isset($u['email_address'])) {
                        $email_addresses[] = $u['email_address'];
                    }
                    if (isset($u['cellnumber'])) {
                        $phone = $this->Clients_model->sanatizePhone($u['cellnumber']);
                        if ($phone !== false) {
                            $sms[] = $phone;
                        }
                        //$sms[] = $u['cellnumber'];
                    }
                }
                //$sms = array('14057605923', '14058889912', '14056132391');
                $bcc_emails = implode(",", $email_addresses);
    
                $this->email->set_mailtype('html');
               sendgrid stuff
    
    
    
    
                if (!empty($sms)) {
                    if ($_SERVER['SERVER_NAME'] == '192.168.0.230') {
                        require '/var/www/html/vendor/autoload.php';
                    } else {
                        // Required if your environment does not handle autoloading
                        require '/chroot/home/a7f60686/xxx.com/html/vendor/autoload.php';
                    }
    
    
    
                    $sid = $this->twilioSID;
                    $token = $this->twilioAUTH;
                    $client = new Client($sid, $token);
                    foreach ($sms as $num) {
                      Twilliio Stuff here
                }
    
    
            }
        }
    }
    

    }

  • The model's save() method can handle a single array of values or an array of array values and it will detect and save a single or multiple (check out the MY_Model::save() method). Save will detect if there is a supplied primary key value and if it exists it will use update, otherwise it will use insert.

Sign In or Register to comment.