/** * @return null * @throws Exception */ public function execute() { $data = $this->entity->tabelizeRecord($this->record); foreach ($data as $table => $insert) { if ($this->tables && !in_array($table, $this->tables)) { continue; } if ($this->record->{$this->entity->getPrimaryKey()}) { /** * Primary key is already set, we need to update it. */ $insert[$this->entity->getPrimaryKey()] = $this->record->{$this->entity->getPrimaryKey()}; $this->insert($table, $insert); } else { /** * Primary key is not set yet, we need to set it now. */ $this->record->{$this->entity->getPrimaryKey()} = $this->insert($table, $insert); $this->record->setSaved(); } } return $this->record->{$this->entity->getPrimaryKey()}; }