Welcome Guest, Not a member yet? Register   Sign In
How insert null on save in select field
#1

(This post was last modified: 10-26-2021, 04:13 AM by includebeer. Edit Reason: Formatting )

Hi sorry for english , this  is my select :

PHP Code:
<div class='row'>

            <div class='col-md-12'>
                <label class='col-md-12 col-xs-12 control-label'>Mezzo</label>
                <div class='col-md-12 col-xs-12'>
                <select id='id_id_mezzi'  class='form-control' name='id_mezzi'>
                    <option value='NULL'>Seleziona un valore...</option>
                                              <option value='1'

                                                          >

                            Van Linea lusso
                        
</option>

                                              <option value='2'

                                                          >

                            Auto Linea lusso
                        
</option>

                                              <option value='3'

                                                          >

                            minibus 19 posti
                        
</option>

                                              <option value='4'

                                                          >

                            Minibus 16 posti
                        
</option>

                                              <option value='5'

                                                          >

                            Auto
                        
</option>

                                              <option value='6'

                                                          >

                            bus 55 posti
                        
</option>

                                              <option value='7'

                                                          >

                            Van
                        
</option>

                                              <option value='8'

                                                          >

                            Bus 30 posti
                        
</option>

                                      </select>
                </div>
            </div>
    </div

In db table this field must bu NULL , where is the best way to insert null on model->save()   ?

Now i control if $_POST['id_mezzi'] ==''    than $_POST['id_mezzi'] = NULL there is a better solution ?
Reply
#2

If you use the Entity class , you can add a set() function to do this. See http://codeigniter.com/user_guide/models...ness-logic
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 10-26-2021, 09:10 AM by mlurie.)

In your model, create the following method:

PHP Code:
protected function setDefaultIDMezzi(array $data) {
    if(!isset($data['data']['id_mezzi']))
        $data['data']['id_mezzi'] = NULL;
    return $data;


You must then configure your model to call this method before each insert (and update if desired) by including these protected variables:

PHP Code:
protected $beforeInsert = ['setDefaultIDMezzi'];
protected 
$beforeUpdate = ['setDefaultIDMezzi']; 

Please review the documentation to make sure you completely understand: https://codeigniter.com/user_guide/model...your-model.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB