function save() { if (!(int) $this->product_id) { throw new Exception('Trying to insert a mapping with no product ID'); } $schema = new VF_Schema; $schema->setConfig($this->getConfig()); $levels = $schema->getLevels(); $select = $this->getReadAdapter()->select() ->from($schema->mappingsTable(), array('id')); foreach ($this->vehicle->toValueArray() as $level => $id) { $select->where($this->inflect($level) . '_id = ?', $id); } $select->where('entity_id = ?', $this->product_id); $id = (int) $select->query()->fetchColumn(); if (0 !== $id) { return $id; } $columns = ''; $values = ''; foreach ($levels as $level) { $columns .= '`' . $this->inflect($level) . '_id`,'; $values .= $this->inflect($this->vehicle->getLevel($level)->getId()); $values .= ','; } $query = sprintf( ' INSERT INTO `'.$schema->mappingsTable().'` ( ' . $columns . ' `entity_id` ) VALUES ( ' . $values . ' %d ) ', (int) $this->product_id ); $r = $this->query($query); return $this->getReadAdapter()->lastInsertId(); }