/** * Reset the property's storage fields. * * @param mixed $val The value to set as field value. * @return PropertyField[] */ private function generateFields($val) { $this->fields = []; if ($this->l10n()) { $translator = TranslationConfig::instance(); foreach ($translator->availableLanguages() as $langCode) { $ident = sprintf('%1$s_%2$s', $this->ident(), $langCode); $field = new PropertyField(); $field->setData(['ident' => $ident, 'sqlType' => $this->sqlType(), 'sqlPdoType' => $this->sqlPdoType(), 'extra' => $this->sqlExtra(), 'val' => $this->fieldVal($langCode, $val), 'defaultVal' => null, 'allowNull' => $this->allowNull()]); $this->fields[$langCode] = $field; } } else { $field = new PropertyField(); $field->setData(['ident' => $this->ident(), 'sqlType' => $this->sqlType(), 'sqlPdoType' => $this->sqlPdoType(), 'extra' => $this->sqlExtra(), 'val' => $this->storageVal($val), 'defaultVal' => null, 'allowNull' => $this->allowNull()]); $this->fields[] = $field; } return $this->fields; }
/** * Sync the new primary keys to related models. * * @param IdProperty $newProp The new ID property. * @param PropertyField $newField The new ID field. * @param IdProperty $oldProp The previous ID property. * @param PropertyField $oldField The previous ID field. * @throws InvalidArgumentException If the new property does not implement the proper mode. * @return self */ protected function syncRelatedFields(IdProperty $newProp, PropertyField $newField, IdProperty $oldProp, PropertyField $oldField) { unset($newProp, $oldProp, $oldField); $cli = $this->climate(); if (!$this->quiet()) { $cli->br(); $cli->comment('Syncing new IDs to related tables.'); } $related = $cli->arguments->get('related_model'); if (!$related) { $cli->br(); $input = $cli->confirm('Are there any model(s) related to the target?'); if (!$input->confirmed()) { return $this; } $related = $this->argOrInput('related_model'); } $this->setRelatedModels($related); $target = $this->targetModel(); $table = $target->source()->table(); foreach ($this->relatedModels() as $model) { $src = $model->source(); $tbl = $src->table(); $db = $src->db(); $db->query(strtr('LOCK TABLES `%relatedTable` AS a WRITE, `%sourceTable` AS b WRITE;', ['%relatedTable' => $tbl, '%sourceTable' => $table])); $sql = strtr('UPDATE `%relatedTable` AS a JOIN `%sourceTable` AS b ON a.`%prop` = b.`%oldKey` SET a.`%prop` = b.`%newKey`;', ['%relatedTable' => $tbl, '%prop' => $this->relatedProperties[$model->objType()], '%sourceTable' => $table, '%newKey' => $newField->ident(), '%oldKey' => $target->key()]); $db->query($sql); $db->query('UNLOCK TABLES;'); } return $this; }