How to insert lots objects

Hi.

I developed action for receive JSON as object list (Airticket class) and save to the Pimcore DB.

Is it good code? may be another way to insert objects without foreach constrution, like one transaction and not many inserts?

	public function ticketaddAction(){
		if(isset($_POST['user']) && isset($_POST['password']) ) {
			$usr = $_POST['user']; $psw = $_POST['password'];
			$data = json_decode($_POST['data']);
			if(self::isApiAllow($usr,$psw)) { // проверяем пользователя и пароль
				foreach($data as $eid=>$d) { //may be another way to insert objects
					$owd = $d['ow_depart'];
					$owa = $d['ow_arrive'];
					$bkd = (strlen($d['bk_depart'])>0)?($d['bk_depart']/1):(0);
					$bka = (strlen($d['bk_arrive'])>0)?($d['bk_arrive']/1):(0);

					$pref = date($owd,'YYYY-mm-dd-hh-ii');
					$t = Object\Airticket::create(array(
						'eid'					=> $eid,
						'type_way'			=> $d['type_way'],
						'ow_ticket_type'		=> $d['ow_ticket_type'],
						'ow_origincode'		=> $d['ow_origincode'],
						'ow_destinationcode'	=> $d['ow_destinationcode'],
						'ow_depart'			=> new Pimcore\Date($owd),
						'ow_arrive'			=> new Pimcore\Date($owa),
						'ow_flightnumber'		=> $d['ow_flightnumber'],
						'ow_airlinecode'		=> $d['ow_airlinecode'],
						'ow_duration'			=> $d['ow_duration'],
						'ow_stopovers'		=> $d['ow_stopovers'],
						'ow_stopoverscodes'	=> $d['ow_stopoverscodes'],
						'bk_ticket_type'		=> $d['bk_ticket_type'],
						'bk_origincode'		=> $d['bk_origincode'],
						'bk_destinationcode'	=> $d['bk_destinationcode'],
						'bk_depart'			=> new Pimcore\Date($bkd),
						'bk_arrive'			=> new Pimcore\Date($bka),
						'bk_flightnumber'		=> $d['bk_flightnumber'],
						'bk_airlinecode'		=> $d['bk_airlinecode'],
						'bk_duration'			=> $d['bk_duration'],
						'bk_stopovers'		=> $d['bk_stopovers'],
						'bk_stopoverscodes'	=> $d['bk_stopoverscodes'],
						'currencycode'		=> $d['currencycode'],
						'totaladultprice'		=> $d['totaladultprice'],
						'totalchildprice'		=> $d['totalchildprice'],
						'totalinfantprice'		=> $d['totalinfantprice'],
						'flightdescription'		=> $d['flightdescription'],
						'flightnote'			=> $d['flightnote'],
						'quantity'				=> $d['quantity'],
						'state'				=> $d['state'],
						'nettoadultprice'		=> $d['nettoadultprice'],
						'nettochildprice'		=> $d['nettochildprice'],
						'nettoinfantprice'		=> $d['nettoinfantprice'],
						'isApiAllow'			=> $d['isApiAllow'],
						'supplier'				=> $d['supplier'] 
					));
					$t->setPublished(true);
					$t->setParentId(68935); // folder ID
					$t->setKey(\Pimcore\File::getValidFilename("api-$pref-$usr-".date("U")));
					$t->save();
				}
			}
		}
	}

Thanks for any ideas.

yeah no, not really. You could run insert queries directly into the database, but that might cause issues with cache and complex data types like relations or classification store, bricks or field collections.

1 Like

I agree with the post above. The best course of action here would be to create a script and a cronjob, and make your code run in the background.

If it’s interactive (create all objects after some sort of upload), then you face the problem of a possible timeout. :slight_smile:

Anyways, PHP API approach always trumps direct SQL injection.