1
votes

i am submitting the location from textbox and putting values in comma seprated form like ngp,akola,kanpur. Now i want if i have 3 values in location textbox then it insert 3 rows in location table one for ngp,second for akola and third for kanpur. but it also takes last inserted id of user table and post all values in location table.

my location table have fields id,name,user_id.

in user_id column i need the last inserted id of user table and in name column i need to insert all values in table..

please help me to do this..below is my code.

function add() {             
        if (!empty($this->request->data)) {

            $this->User->set($this->data['User']);
            $isValidated = $this->User->validates();
            if ($isValidated) {
                // Generating UUID   
                if (!empty($this->request->data['User']['company_name'])) {
                    $this->request->data['User']['is_business'] = 1; 
                    }
                if ($this->User->save($this->request->data, array('validate' => false))) {
                    $lastId = $this->User->id;
                    $location = implode(',',$this->request->data['User']['location']);
                    for(i=1;i<=$location;i++){
                $this->Location->save($location);   
                }                 
                }    

            }
        }
    }
2

2 Answers

0
votes
$lastId = $this->User->id;
$location = explode(',',$this->request->data['User']['location']);
foreach($location as $value){
    $data = array();
    $data["name"] = $value;
    $data["user_id"] = $lastId;
    $this->Location->save($data,false);
    $this->Location->id = false;
}
0
votes

Model::saveAll(array $data = null, array $options = array())

$this->request->data['Location'] = explode(',',$this->request->data['User']['location']);

if ($this->User->saveAll($this->request->data, array('validate' => false))){  // ...}