public function write(WriterInterface $writer) { $writer->write('%s:', $this->getName())->indent()->writeCallback(function (WriterInterface $writer, Index $_this = null) { $writer->write('fields: [%s]', implode(', ', $_this->getColumnNames())); })->writeIf(($type = strtolower($this->parameters->get('indexType'))) !== 'index', 'type: ' . $type)->outdent(); return $this; }
public function write(WriterInterface $writer) { if ($this->referencedTable == null) { $writer->write('# There is another foreign key declaration.'); } else { $writer->write('%s:', 0 === strpos($this->parameters->get('name'), 'd:') ? substr($this->parameters->get('name'), 2) : $this->referencedTable->getModelName())->indent()->write('class: ' . $this->referencedTable->getModelName())->write('local: ' . $this->foreign->getColumnName())->write('foreign: ' . $this->local->getColumnName())->write('foreignAlias: ' . (($alias = $this->getForeignAlias()) ? $alias : ($this->isManyToOne() ? Inflector::pluralize($this->referencedTable->getModelName()) : $this->referencedTable->getModelName())))->write('onDelete: ' . strtolower($this->parameters->get('deleteRule')))->write('onUpdate: ' . strtolower($this->parameters->get('updateRule')))->outdent(); } return $this; }
public function writeVendor(WriterInterface $writer) { $writer->write('<vendor type="mysql">'); $writer->indent(); $writer->write('<parameter name="Engine" value="%s" />', $this->parameters->get('tableEngine')); $writer->write('<parameter name="Charset" value="%s" />', $this->parameters->get('defaultCharacterSetName')); $writer->outdent(); $writer->write('</vendor>'); return $this; }
public function write(WriterInterface $writer) { $writer->write('%s:', $this->getColumnName())->indent()->write('type: %s', $this->getDocument()->getFormatter()->getDatatypeConverter()->getType($this))->writeIf($this->isPrimary(), 'primary: true')->writeIf($this->isNotNull(), 'notnull: true')->writeIf($this->isAutoIncrement(), 'autoincrement: true')->writeCallback(function (WriterInterface $writer, Column $_this = null) { if ($values = $_this->getParameters()->get('datatypeExplicitParams')) { $writer->write('values: %s', strtr($values, array('(' => '[', ')' => ']'))); } })->writeIf($default = $this->getDefaultValue(), 'default: ' . $default)->writeCallback(function (WriterInterface $writer, Column $_this = null) { foreach ($_this->getNode()->xpath("value[@key='flags']/value") as $flag) { $writer->write(strtolower($flag) . ': true'); } })->outdent(); return $this; }
public function writeRelations(WriterInterface $writer) { $parentTable = $this->getParent()->getParent(); foreach ($this->foreigns as $foreign) { //if($foreign->getParent()->getParent()->getRawTableName() == $parentTable->getRawTableName()){ $writer->write('<foreign-key name="%s" foreignTable="%s" phpName="%s" refPhpName="%s" onDelete="%s" onUpdate="%s">', $foreign->parameters->get('name'), $foreign->getOwningTable()->getRawTableName(), $foreign->getOwningTable()->getModelName(), $foreign->getReferencedTable()->getModelName(), strtolower($foreign->parameters->get('deleteRule')) == 'no action' ? 'none' : strtolower($foreign->parameters->get('deleteRule')), strtolower($foreign->parameters->get('updateRule')) == 'no action' ? 'none' : strtolower($foreign->parameters->get('updateRule'))); $writer->indent(); $writer->write('<reference local="%s" foreign="%s" />', $foreign->getLocal()->getColumnName(), $foreign->getForeign()->getColumnName()); $writer->outdent(); $writer->write('</foreign-key>'); //} } return $this; }
public function writeRelations(WriterInterface $writer) { foreach ($this->foreignKeys as $foreign) { $writer->write('<foreign-key name="%s" foreignTable="%s" phpName="%s" refPhpName="%s" onDelete="%s" onUpdate="%s">', $foreign->parameters->get('name'), $foreign->getReferencedTable()->getRawTableName(), $foreign->getReferencedTable()->getModelName(), $foreign->getOwningTable()->getModelName(), strtolower($foreign->parameters->get('deleteRule')) == 'no action' ? 'none' : strtolower($foreign->parameters->get('deleteRule')), strtolower($foreign->parameters->get('updateRule')) == 'no action' ? 'none' : strtolower($foreign->parameters->get('updateRule'))); $writer->indent(); $locals = $foreign->getLocals(); $foreigns = $foreign->getForeigns(); for ($i = 0; $i < count($locals); $i++) { $writer->write('<reference local="%s" foreign="%s" />', $locals[$i]->getColumnName(), $foreigns[$i]->getColumnName()); } $writer->outdent(); $writer->write('</foreign-key>'); } return $this; }
public function writeReferences(WriterInterface $writer) { $writer->write('/**')->write(' * @var array')->write(' */')->writeCallback(function (WriterInterface $writer, Table $_this = null) { if (count($_this->getForeignKeys())) { $writer->write('protected $_referenceMap = array('); $writer->indent(); foreach ($_this->getForeignKeys() as $foreignKey) { $foreignKey->write($writer); } $writer->outdent(); $writer->write(');'); } else { $writer->write('protected $_referenceMap = array();'); } }); return $this; }
/** * Write model body code. * * @param \MwbExporter\Writer\WriterInterface $writer * @return \MwbExporter\Formatter\Node\Sequelize\Model\Table */ protected function writeBody(WriterInterface $writer) { $writer->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_JS))->write(''); } })->write("module.exports = function(sequelize, DataTypes) {")->indent()->write("return sequelize.define('%s', %s, %s);", $this->getModelName(), $this->asModel(), $this->asOptions())->outdent()->write("}"); return $this; }
public function write(WriterInterface $writer) { if ($this->isUnique()) { $type = "unique"; } else { if ($this->isIndex()) { $type = "index"; } else { return $this; } } $writer->write('<%s name="%s">', $type, $this->getName())->indent(); foreach ($this->columns as $refColumn) { $writer->write('<%s-column name="%s" />', $type, $refColumn->getColumnName()); } $writer->outdent()->write('</%s>', $type); return $this; }
/** * Write model body code. * * @param \MwbExporter\Writer\WriterInterface $writer * @return \MwbExporter\Formatter\Sencha\ExtJS4\Model\Table */ public function writeBody(WriterInterface $writer) { $writer->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_JS))->write(''); } })->write("Ext.define('%s', %s);", $this->getClassPrefix() . '.' . $this->getModelName(), $this->asModel()); return $this; }
public function write(WriterInterface $writer) { $writer->write('columns:')->indent()->writeCallback(function (WriterInterface $writer, Columns $_this = null) { foreach ($_this as $column) { $column->write($writer); } })->outdent(); return $this; }
public function writeGetterAndSetter(WriterInterface $writer) { if (!$this->isIgnored()) { $this->getDocument()->addLog(sprintf(' Writing setter/getter for column "%s"', $this->getColumnName())); $table = $this->getTable(); $converter = $this->getFormatter()->getDatatypeConverter(); $nativeType = $converter->getNativeType($converter->getMappedType($this)); $writer->write('/**')->write(' * Set the value of ' . $this->getColumnName() . '.')->write(' *')->write(' * @param ' . $nativeType . ' $' . $this->getColumnName())->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $this->getBeautifiedColumnName() . '($' . $this->getColumnName() . ')')->write('{')->indent()->write('$this->' . $this->getColumnName() . ' = $' . $this->getColumnName() . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get the value of ' . $this->getColumnName() . '.')->write(' *')->write(' * @return ' . $nativeType)->write(' */')->write('public function get' . $this->getBeautifiedColumnName() . '()')->write('{')->indent()->write('return $this->' . $this->getColumnName() . ';')->outdent()->write('}')->write(''); } return $this; }
/** * (non-PHPdoc) * @see \MwbExporter\Model\Schema::write() */ public function write(WriterInterface $writer) { $writer->open($this->getDocument()->translateFilename(null, $this))->write('<?xml version="1.0" encoding="UTF-8"?>')->writeCallback(function (WriterInterface $writer, Schema $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_XML)); } })->write('<database name="%s" defaultIdMethod="native">', $this->getName())->writeCallback(function (WriterInterface $writer, Schema $_this = null) { $_this->writeSchema($writer); })->write('</database>')->close(); return $this; }
public function writeTable(WriterInterface $writer) { if (!$this->isExternal()) { $writer->open($this->getTableFileName())->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_JS))->write(''); } })->write('%s.%s = Ext.extend(%s, %s);', $this->getClassPrefix(), $this->getModelName(), $this->getParentClass(), $this->asModel())->write('')->write('%1$s.%2$s = Ext.extend(%1$s.%2$s, %3$s);', $this->getClassPrefix(), $this->getModelName(), $this->asUI())->write('')->close(); return self::WRITE_OK; } return self::WRITE_EXTERNAL; }
public function writeTable(WriterInterface $writer) { if (!$this->isExternal()) { $writer->open($this->getTableFileName())->write('<?php')->write('')->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_PHP))->write(''); } })->write('class ' . $this->getTablePrefix() . $this->getModelName() . 'Controller extends ' . $this->getParentTable())->write('{')->indent()->write('')->outdent()->write('}')->write('')->close(); return self::WRITE_OK; } return self::WRITE_EXTERNAL; }
public function write(WriterInterface $writer) { $type = strtoupper($this->getFormatter()->getDatatypeConverter()->getType($this)); if ($type == 'DECIMAL') { $type = $type . '" size="' . $this->parameters->get('precision') . '" scale="' . $this->parameters->get('scale'); } if ($type == 'ENUM' or $type == 'SET') { $type = $type . '" sqlType="' . $type . $this->parameters->get('datatypeExplicitParams') . '" valueSet="' . substr($this->parameters->get('datatypeExplicitParams'), 1, -1); } $writer->write('<column name="%s" type="%s"%s%s%s%s%s%s />', $this->getColumnName(), $type, $this->isPrimary == 1 ? ' primaryKey="true"' : '', $this->parameters->get('length') > 0 ? ' size="' . $this->parameters->get('length') . '"' : '', $this->isNotNull() ? ' required="true"' : '', $this->isAutoIncrement() ? ' autoIncrement="true"' : '', ($defaultValue = $this->getDefaultValue()) && !in_array($defaultValue, array('CURRENT_TIMESTAMP')) ? ' defaultValue="' . $defaultValue . '"' : '', $defaultValue ? ' defaultExpr="' . $defaultValue . '"' : ''); return $this; }
/** * (non-PHPdoc) * @see \MwbExporter\Model\Schema::write() */ public function write(WriterInterface $writer) { $data = $this->asYAML(); $indent = $this->getConfig()->get(Formatter::CFG_INDENTATION); $yaml = new YAML($data, array('indent' => $indent, 'inline' => true, 'inline_size' => $this->getInlineSize($data) + ($this->getArrayLevel($data) - 1) * $indent)); $writer->open($this->getDocument()->translateFilename(null, $this))->writeCallback(function (WriterInterface $writer, Schema $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_YAML))->write(''); } })->write($yaml)->close(); return $this; }
public function writeGetterAndSetter(WriterInterface $writer) { if (!$this->isIgnored()) { $this->getDocument()->addLog(sprintf(' Writing setter/getter for column "%s"', $this->getColumnName())); $table = $this->getTable(); $converter = $this->getFormatter()->getDatatypeConverter(); $nativeType = $converter->getNativeType($converter->getMappedType($this)); $shouldTypehintProperties = $this->getConfig()->get(Formatter::CFG_PROPERTY_TYPEHINT); $typehint = $shouldTypehintProperties && class_exists($nativeType) ? "{$nativeType} " : ''; $writer->write('/**')->write(' * Set the value of ' . $this->getColumnName() . '.')->write(' *')->write(' * @param ' . $nativeType . ' $' . $this->getColumnName())->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $this->getBeautifiedColumnName() . '(' . $typehint . '$' . $this->getColumnName() . ')')->write('{')->indent()->write('$this->' . $this->getColumnName() . ' = $' . $this->getColumnName() . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get the value of ' . $this->getColumnName() . '.')->write(' *')->write(' * @return ' . $nativeType)->write(' */')->write('public function get' . $this->getBeautifiedColumnName() . '()')->write('{')->indent()->write('return $this->' . $this->getColumnName() . ';')->outdent()->write('}')->write(''); } return $this; }
public function writeTable(WriterInterface $writer) { switch (true) { case $this->isExternal(): return self::WRITE_EXTERNAL; case $this->getConfig()->get(Formatter::CFG_SKIP_M2M_TABLES) && $this->isManyToMany(): return self::WRITE_M2M; default: $this->getDocument()->addLog(sprintf('Writing table "%s"', $this->getModelName())); $writer->open($this->getTableFileName())->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_YAML))->write(''); } })->write($this->asYAML())->close(); return self::WRITE_OK; } }
public function writeTable(WriterInterface $writer) { if (!$this->isExternal()) { $writer->open($this->getTableFileName())->writeCallback(function (WriterInterface $writer, Table $_this = null) { if ($_this->getConfig()->get(Formatter::CFG_ADD_COMMENT)) { $writer->write($_this->getFormatter()->getComment(Comment::FORMAT_YAML))->write(''); } })->write('%s:', $this->getModelName())->indent()->writeIf($actAs = trim($this->getActAsBehaviour()), $actAs)->write('tableName: ' . ($this->getConfig()->get(Formatter::CFG_EXTEND_TABLENAME_WITH_SCHEMA) ? $this->getSchema()->getName() . '.' : '') . $this->getRawTableName())->writeCallback(function (WriterInterface $writer, Table $_this = null) { $_this->getColumns()->write($writer); })->writeCallback(function (WriterInterface $writer, Table $_this = null) { $externalRelation = $_this->getExternalRelations(); if (count($_this->getTableRelations()) || $externalRelation) { $writer->write('relations:'); $writer->indent(); foreach ($_this->getTableRelations() as $relation) { $relation->write($writer); } if ($externalRelation) { $writer->write($externalRelation); } $writer->outdent(); } })->writeCallback(function (WriterInterface $writer, Table $_this = null) { if (count($_this->getTableIndices())) { $writer->write('indexes:'); $writer->indent(); foreach ($_this->getTableIndices() as $index) { $index->write($writer); } $writer->outdent(); } })->write('options:')->indent()->write('charset: ' . (($charset = $this->parameters->get('defaultCharacterSetName')) ? $charset : 'utf8'))->writeIf($engine = $this->parameters->get('tableEngine'), 'type: ' . $engine)->outdent()->outdent()->close(); return self::WRITE_OK; } return self::WRITE_EXTERNAL; }
public function writeRelationsGetterAndSetter(WriterInterface $writer) { $table = $this->getTable(); // one to many references foreach ($this->foreigns as $foreign) { if ($foreign->getForeign()->getTable()->isManyToMany()) { // do not create entities for many2many tables continue; } if ($foreign->isUnidirectional()) { // do not output mapping in foreign table when the unidirectional option is set continue; } if ($foreign->isManyToOne()) { // is ManyToOne $related = $this->getRelatedName($foreign); $related_text = $this->getRelatedName($foreign, false); if ($v = $foreign->getForeign()->parseComment('field-inversed')) { //v($v); $propName = $v; $varName = Inflector::singularize($v); $funactionName = ucfirst(Inflector::singularize($propName)); $funactionGetName = ucfirst($v); } else { $funactionName = $this->columnNameBeautifier($foreign->getOwningTable()->getModelName()) . $related; $funactionGetName = $this->columnNameBeautifier(Inflector::pluralize($foreign->getOwningTable()->getModelName())) . $related; $propName = lcfirst(Inflector::pluralize($foreign->getOwningTable()->getModelName())) . $related; $varName = lcfirst($foreign->getOwningTable()->getModelName()); } if ($v = $foreign->getForeign()->parseComment('field-mapped')) { $funactionSetName = ucfirst($v); } else { $funactionSetName = ucfirst($table->getModelName()); } $typeEntity = $foreign->getOwningTable()->getNamespace(); $nullType = 'null|'; $defaultValue = ' = null'; // ->write('if ($'.$varName.') {') // ->indent() // ->writeIf(!$unidirectional, '$'.$varName.'->set'.$setterFunactionName.'($this);') // ->outdent() // ->write('}') // ->write('$this->'.$propName.' = $'.$varName.';') // ->write('') // ->write('return $this;') $writer->write('/**')->write(' * Add ' . trim($varName . ' ' . $related_text) . ' entity to collection (one to many).')->write(' *')->write(' * @param ' . $nullType . $typeEntity . ' $' . $varName)->write(' *')->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function add' . $funactionName . '(' . $typeEntity . ' $' . $varName . $defaultValue . ')')->write('{')->indent()->write('if ($' . $varName . ') {')->indent()->write('$' . $varName . '->set' . $funactionSetName . '($this);')->write('$this->' . $propName . '[] = $' . $varName . ';')->outdent()->write('}')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * remove ' . trim($varName . ' ' . $related_text) . ' entity from collection (one to many).')->write(' *')->write(' * @param ' . $nullType . $typeEntity . ' $' . $varName)->write(' *')->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function remove' . $funactionName . '(' . $typeEntity . ' $' . $varName . $defaultValue . ')')->write('{')->indent()->write('if ($' . $varName . ') {')->indent()->write('$this->' . $propName . '->removeElement($' . $varName . ');')->outdent()->write('}')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . trim($propName . ' ' . $related_text) . ' collection (one to many).')->write(' *')->write(' * @return ' . $table->getCollectionInterface() . '|' . $typeEntity . '[]')->write(' */')->write('public function get' . $funactionGetName . '()')->write('{')->indent()->write('return $this->' . $propName . ';')->outdent()->write('}'); } else { // OneToOne if ($filedNameInversed = $foreign->getForeign()->parseComment('field-inversed')) { $funactionName = ucfirst(Inflector::singularize($filedNameInversed)); $propName = $varName = $filedNameInversed; } else { $funactionName = ucfirst($this->columnNameBeautifier($foreign->getOwningTable()->getModelName())); $propName = $varName = lcfirst($foreign->getOwningTable()->getModelName()); } $typeEntity = $foreign->getOwningTable()->getNamespace(); $writer->write('/**')->write(' * Set ' . $varName . ' entity (one to one).')->write(' *')->write(' * @param ' . $typeEntity . ' $' . $propName)->write(' *')->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $funactionName . '(' . $typeEntity . ' $' . $varName . ')')->write('{')->indent()->write('$this->' . $propName . ' = $' . $varName . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . $varName . ' entity (one to one).')->write(' *')->write(' * @return ' . $typeEntity)->write(' */')->write('public function get' . $funactionName . '()')->write('{')->indent()->write('return $this->' . $propName . ';')->outdent()->write('}'); } $writer->write(''); } // many to one references if (null !== $this->local) { $unidirectional = $this->local->isUnidirectional(); $nullType = ''; if (!$this->isNotNull()) { $nullType = 'null|'; } if ($this->local->isManyToOne()) { // is ManyToOne $related = $this->getManyToManyRelatedName($this->local->getReferencedTable()->getRawTableName(), $this->local->getForeign()->getColumnName()); $related_text = $this->getManyToManyRelatedName($this->local->getReferencedTable()->getRawTableName(), $this->local->getForeign()->getColumnName(), false); if ($filedNameMapped = $this->local->getForeign()->parseComment('field-mapped')) { $funactionName = ucfirst(Inflector::singularize($filedNameMapped)); $propName = $varName = $filedNameMapped; } else { $funactionName = $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()) . $related; $propName = $varName = lcfirst($this->local->getReferencedTable()->getModelName()) . $related; } if (!$this->isNotNull()) { if (!$this->isNotNull()) { $nullType = 'null|'; } } $defaultValue = ' = null'; $typeEntity = $this->local->getReferencedTable()->getNamespace(); $writer->write('/**')->write(' * Set ' . trim($varName . ' ' . $related_text) . ' entity (many to one).')->write(' *')->write(' * @param ' . $nullType . $typeEntity . ' $' . $varName)->write(' *')->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $funactionName . '(' . $typeEntity . ' $' . $varName . $defaultValue . ')')->write('{')->indent()->write('$this->' . $propName . ' = $' . $varName . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . trim($varName . ' ' . $related_text) . ' entity (many to one).')->write(' *')->write(' * @return ' . $nullType . $typeEntity)->write(' */')->write('public function get' . $funactionName . '()')->write('{')->indent()->write('return $this->' . $propName . ';')->outdent()->write('}')->write(''); } else { // OneToOne $typeEntity = $this->local->getReferencedTable()->getNamespace(); $modelName = $this->local->getReferencedTable()->getModelName(); if ($v = $this->local->getForeign()->parseComment('field-mapped')) { $propName = $varName = $v; $funactionName = $this->columnNameBeautifier(Inflector::singularize($v)); } else { $propName = $varName = lcfirst($this->local->getReferencedTable()->getModelName()); $funactionName = $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()); } if ($v = $this->local->getForeign()->parseComment('field-inversed')) { $setterFunactionName = $this->columnNameBeautifier($v); } else { $setterFunactionName = $this->columnNameBeautifier($this->local->getOwningTable()->getModelName()); } $writer->write('/**')->write(' * Set ' . $propName . ' entity (one to one).')->write(' *')->write(' * @param ' . $nullType . $typeEntity . ' $' . $varName)->write(' *')->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $funactionName . '(' . $typeEntity . ' $' . $varName . ' = null)')->write('{')->indent()->write('if ($' . $varName . ') {')->indent()->writeIf(!$unidirectional, '$' . $varName . '->set' . $setterFunactionName . '($this);')->outdent()->write('}')->write('$this->' . $propName . ' = $' . $varName . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . $propName . ' entity (one to one).')->write(' *')->write(' * @return ' . $nullType . $typeEntity)->write(' */')->write('public function get' . $funactionName . '()')->write('{')->indent()->write('return $this->' . $propName . ';')->outdent()->write('}')->write(''); } } return $this; }
public function writeSerialization(WriterInterface $writer) { $writer->write('public function __sleep()')->write('{')->indent()->write('return array(%s);', implode(', ', array_map(function ($column) { return sprintf('\'%s\'', $column); }, $this->getColumns()->getColumnNames())))->outdent()->write('}'); return $this; }
public function write(WriterInterface $writer) { $writer->write('\'' . $this->getReferencedTable()->getRawTableName() . '\' => array(')->indent()->write('\'columns\' => \'' . $this->foreign->getColumnName() . '\',')->write('\'refTableClass\' => \'' . $this->getReferencedTable()->getRawTableName() . '\',')->write('\'refColumns\' => \'' . $this->local->getColumnName() . '\',')->outdent()->write('),'); return $this; }
public function write(WriterInterface $writer) { $writer->write('/**')->write(' * Relationship with ' . $this->getReferencedTable()->getModelName() . '.')->write(' */')->write('public function ' . Inflector::pluralize($this->getReferencedTable()->getRawTableName()) . '()')->write('{')->indent()->write('return $this->belongsTo(\'' . $this->getReferencedTable()->getNamespace() . '\\' . $this->getReferencedTable()->getModelName() . '\', \'' . $this->getLocal()->getColumnName() . '\');')->outdent()->write('}')->write(''); return $this; }
public function write(WriterInterface $writer) { $writer->write('$table->foreign(\'' . $this->getLocal()->getColumnName() . '\')->references(\'' . $this->getForeign()->getColumnName() . '\')->on(\'' . $this->getReferencedTable()->getRawTableName() . '\');'); return $this; }
/** * Write model body code. * * @param \MwbExporter\Writer\WriterInterface $writer * @return \MwbExporter\Formatter\Sencha\ExtJS4\Model\Table */ public function writeBody(WriterInterface $writer) { $writer->write("Ext.define('%s', %s);", $this->getClassPrefix() . '.' . $this->getModelName(), $this->asModel()); return $this; }
/** * Write getArrayCopy method. * * @see \Zend\Stdlib\Hydrator\ArraySerializable::hydrate() * @param \MwbExporter\Writer\WriterInterface $writer * @return \MwbExporter\Formatter\Doctrine2\AnnotationZF2InputFilter\Model\Table */ public function writeGetArrayCopy(WriterInterface $writer) { $columns = $this->getColumns(); $relations = $this->getTableRelations(); $writer->write('/**')->write(' * Return a array with all fields and data.')->write(' * Default the relations will be ignored.')->write(' * ')->write(' * @param array $fields')->write(' * @return array')->write(' */')->write('public function getArrayCopy(array $fields = array())')->write('{')->indent()->write('$dataFields = array(%s);', implode(', ', array_map(function ($column) { return sprintf('\'%s\'', $column); }, $columns->getColumnNames())))->write('$relationFields = array(%s);', implode(', ', array_map(function ($relation) { return sprintf('\'%s\'', lcfirst($relation->getReferencedTable()->getModelName())); }, $relations)))->write('$copiedFields = array();')->write('foreach ($relationFields as $relationField) {')->indent()->write('$map = null;')->write('if (array_key_exists($relationField, $fields)) {')->indent()->write('$map = $fields[$relationField];')->write('$fields[] = $relationField;')->write('unset($fields[$relationField]);')->outdent()->write('}')->write('if (!in_array($relationField, $fields)) {')->indent()->write('continue;')->outdent()->write('}')->write('$getter = sprintf(\'get%s\', ucfirst(str_replace(\' \', \'\', ucwords(str_replace(\'_\', \' \', $relationField)))));')->write('$relationEntity = $this->{$getter}();')->write('$copiedFields[$relationField] = (!is_null($map))')->indent()->write('? $relationEntity->getArrayCopy($map)')->write(': $relationEntity->getArrayCopy();')->outdent()->write('$fields = array_diff($fields, array($relationField));')->outdent()->write('}')->write('foreach ($dataFields as $dataField) {')->indent()->write('if (!in_array($dataField, $fields) && !empty($fields)) {')->indent()->write('continue;')->outdent()->write('}')->write('$getter = sprintf(\'get%s\', ucfirst(str_replace(\' \', \'\', ucwords(str_replace(\'_\', \' \', $dataField)))));')->write('$copiedFields[$dataField] = $this->{$getter}();')->outdent()->write('}')->write('')->write('return $copiedFields;')->outdent()->write('}')->write(''); return $this; }
public function writeFillable(WriterInterface $writer) { /* * FIXME: identify which columns are FK and not add to the array fillable */ $writer->write('/**')->write(' * The attributes that are mass assignable.')->write(' * ')->write(' * @var array')->write(' */')->writeCallback(function (WriterInterface $writer, Table $_this = null) { if (count($_this->getColumns())) { $content = ''; $columns = $_this->getColumns(); foreach ($columns as $column) { $content .= '\'' . $column->getColumnName() . '\','; } $writer->write('protected $fillable = [' . substr($content, 0, -1) . '];'); } }); return $this; }
public function writeRelationsGetterAndSetter(WriterInterface $writer) { $table = $this->getTable(); // one to many references foreach ($this->foreigns as $foreign) { if ($foreign->getForeign()->getTable()->isManyToMany()) { // do not create entities for many2many tables continue; } if ($foreign->parseComment('unidirectional') === 'true') { // do not output mapping in foreign table when the unidirectional option is set continue; } if ($foreign->isManyToOne()) { // is ManyToOne $related = $this->getRelatedName($foreign); $related_text = $this->getRelatedName($foreign, false); $writer->write('/**')->write(' * Add ' . trim($foreign->getOwningTable()->getModelName() . ' ' . $related_text) . ' entity to collection (one to many).')->write(' *')->write(' * @param ' . $foreign->getOwningTable()->getNamespace() . ' $' . lcfirst($foreign->getOwningTable()->getModelName()))->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function add' . $this->columnNameBeautifier($foreign->getOwningTable()->getModelName()) . $related . '(' . $foreign->getOwningTable()->getModelName() . ' $' . lcfirst($foreign->getOwningTable()->getModelName()) . ')')->write('{')->indent()->write('$this->' . lcfirst(Inflector::pluralize($foreign->getOwningTable()->getModelName())) . $related . '[] = $' . lcfirst($foreign->getOwningTable()->getModelName()) . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . trim($foreign->getOwningTable()->getModelName() . ' ' . $related_text) . ' entity collection (one to many).')->write(' *')->write(' * @return ' . $table->getCollectionInterface())->write(' */')->write('public function get' . $this->columnNameBeautifier(Inflector::pluralize($foreign->getOwningTable()->getModelName())) . $related . '()')->write('{')->indent()->write('return $this->' . lcfirst(Inflector::pluralize($foreign->getOwningTable()->getModelName())) . $related . ';')->outdent()->write('}'); } else { // OneToOne $writer->write('/**')->write(' * Set ' . $foreign->getOwningTable()->getModelName() . ' entity (one to one).')->write(' *')->write(' * @param ' . $foreign->getOwningTable()->getNamespace() . ' $' . lcfirst($foreign->getOwningTable()->getModelName()))->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $this->columnNameBeautifier($foreign->getOwningTable()->getModelName()) . '(' . $foreign->getOwningTable()->getModelName() . ' $' . lcfirst($foreign->getOwningTable()->getModelName()) . ')')->write('{')->indent()->write('$this->' . lcfirst($foreign->getOwningTable()->getModelName()) . ' = $' . lcfirst($foreign->getOwningTable()->getModelName()) . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . $foreign->getOwningTable()->getModelName() . ' entity (one to one).')->write(' *')->write(' * @return ' . $foreign->getOwningTable()->getNamespace())->write(' */')->write('public function get' . $this->columnNameBeautifier($foreign->getOwningTable()->getModelName()) . '()')->write('{')->indent()->write('return $this->' . lcfirst($foreign->getOwningTable()->getModelName()) . ';')->outdent()->write('}'); } $writer->write(''); } // many to one references if (null !== $this->local) { $unidirectional = $this->local->parseComment('unidirectional') === 'true'; if ($this->local->isManyToOne()) { // is ManyToOne $related = $this->getManyToManyRelatedName($this->local->getReferencedTable()->getRawTableName(), $this->local->getForeign()->getColumnName()); $related_text = $this->getManyToManyRelatedName($this->local->getReferencedTable()->getRawTableName(), $this->local->getForeign()->getColumnName(), false); $writer->write('/**')->write(' * Set ' . trim($this->local->getReferencedTable()->getModelName() . ' ' . $related_text) . ' entity (many to one).')->write(' *')->write(' * @param ' . $this->local->getReferencedTable()->getNamespace() . ' $' . lcfirst($this->local->getReferencedTable()->getModelName()))->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()) . $related . '(' . $this->local->getReferencedTable()->getModelName() . ' $' . lcfirst($this->local->getReferencedTable()->getModelName()) . ' = null)')->write('{')->indent()->write('$this->' . lcfirst($this->local->getReferencedTable()->getModelName()) . $related . ' = $' . lcfirst($this->local->getReferencedTable()->getModelName()) . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . trim($this->local->getReferencedTable()->getModelName() . ' ' . $related_text) . ' entity (many to one).')->write(' *')->write(' * @return ' . $this->local->getReferencedTable()->getNamespace())->write(' */')->write('public function get' . $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()) . $related . '()')->write('{')->indent()->write('return $this->' . lcfirst($this->local->getReferencedTable()->getModelName()) . $related . ';')->outdent()->write('}')->write(''); } else { // OneToOne $writer->write('/**')->write(' * Set ' . $this->local->getReferencedTable()->getModelName() . ' entity (one to one).')->write(' *')->write(' * @param ' . $this->local->getReferencedTable()->getNamespace() . ' $' . lcfirst($this->local->getReferencedTable()->getModelName()))->write(' * @return ' . $table->getNamespace())->write(' */')->write('public function set' . $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()) . '(' . $this->local->getReferencedTable()->getModelName() . ' $' . lcfirst($this->local->getReferencedTable()->getModelName()) . ' = null)')->write('{')->indent()->writeIf(!$unidirectional, '$' . lcfirst($this->local->getReferencedTable()->getModelName()) . '->set' . $this->columnNameBeautifier($this->local->getOwningTable()->getModelName()) . '($this);')->write('$this->' . lcfirst($this->local->getReferencedTable()->getModelName()) . ' = $' . lcfirst($this->local->getReferencedTable()->getModelName()) . ';')->write('')->write('return $this;')->outdent()->write('}')->write('')->write('/**')->write(' * Get ' . $this->local->getReferencedTable()->getModelName() . ' entity (one to one).')->write(' *')->write(' * @return ' . $this->local->getReferencedTable()->getNamespace())->write(' */')->write('public function get' . $this->columnNameBeautifier($this->local->getReferencedTable()->getModelName()) . '()')->write('{')->indent()->write('return $this->' . lcfirst($this->local->getReferencedTable()->getModelName()) . ';')->outdent()->write('}')->write(''); } } return $this; }
public function writeDown(WriterInterface $writer) { $writer->write('/**')->write(' * Reverse the migrations.')->write(' *')->write(' * @return void')->write(' */')->write('public function down()')->write('{')->indent()->write('Schema::drop(\'' . $this->getRawTableName() . '\');')->outdent()->write('}')->write(''); return $this; }