public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tags = array(array('name' => 'return', 'description' => 'array')); $docblock = new DocBlockGenerator('Initialize indexes'); $docblock->setTags($tags); $resultIndexList = array(); $indexList = $table->getIndex(); foreach ($indexList as $index) { $resIndex = $index->toArray(); $resIndex['column_list'] = array(); switch ($index->getType()) { case AbstractIndex::TYPE_PRIMARY: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_PRIMARY', ValueGenerator::TYPE_CONSTANT); break; case AbstractIndex::TYPE_KEY: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_KEY', ValueGenerator::TYPE_CONSTANT); break; case AbstractIndex::TYPE_UNIQUE: $resIndex['type'] = new ValueGenerator('AbstractModel::INDEX_UNIQUE', ValueGenerator::TYPE_CONSTANT); break; } foreach ($resIndex['columns'] as $col) { $resIndex['column_list'][] = $col['column_name']; } unset($resIndex['columns']); $resultIndexList[$index->getName()] = $resIndex; } $property = new PropertyGenerator('indexList', $resultIndexList, PropertyGenerator::FLAG_PROTECTED); $body = preg_replace("#^(\\s*)protected #", "\\1", $property->generate()) . "\n"; $method = new MethodGenerator(); $method->setName('initIndexList'); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setFinal(true); $method->setDocBlock($docblock); $method->setBody(<<<EOS {$body} \$this->indexList = \$indexList; \$this->setupIndexList(); EOS ); $file->getClass()->addMethodFromGenerator($method); }
public function testOtherTypesThrowExceptionOnGenerate() { $codeGenProperty = new PropertyGenerator('someVal', new \stdClass()); $this->setExpectedException('Zend\\Code\\Generator\\Exception\\RuntimeException', 'Type "stdClass" is unknown or cannot be used as property default value'); $codeGenProperty->generate(); }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $table = $part->getTable(); $tags = array(array('name' => 'var', 'description' => ' array связи')); $docblock = new \Zend\Code\Generator\DocBlockGenerator('Связи'); $docblock->setTags($tags); $linkList = $table->getLink(); /*if ($table->getColumn('parent_id')) { echo $table->getName(); print_r($linkList); die; }*/ $relation = array(); foreach ($linkList as $link) { $linkLocalColumnName = $link->getLocalColumn()->getName(); if ($link->getForeignTable() == $link->getLocalTable() && $link->getLocalColumn() == $link->getForeignColumn()) { continue; } $foreignAliasName = $link->getForeignEntity(); $rel = $link->toArray(); unset($rel['name']); switch ($link->getLinkType()) { case AbstractLink::LINK_TYPE_ONE_TO_ONE: $rel['type'] = new ValueGenerator('AbstractModel::ONE_TO_ONE', ValueGenerator::TYPE_CONSTANT); break; case AbstractLink::LINK_TYPE_ONE_TO_MANY: $rel['type'] = new ValueGenerator('AbstractModel::ONE_TO_MANY', ValueGenerator::TYPE_CONSTANT); break; case AbstractLink::LINK_TYPE_MANY_TO_ONE: $rel['type'] = new ValueGenerator('AbstractModel::MANY_TO_ONE', ValueGenerator::TYPE_CONSTANT); break; case AbstractLink::LINK_TYPE_MANY_TO_MANY: $rel['type'] = new ValueGenerator('AbstractModel::MANY_TO_MANY', ValueGenerator::TYPE_CONSTANT); break; } if ($link->getLocalColumn()->getName() != 'id' && !$link->getLinkTable() || $link->getLocalColumn()->getName() == 'id' && !$link->getLinkTable() && !$link->getLocalColumn()->isAutoincrement()) { $rel['required_link'] = !$link->getLocalColumn()->isNullable(); $rel['link_method'] = 'link' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase(); $rel['unlink_method'] = 'deleteLink' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase(); } else { if ($table->getName() == 'tag') { if ($link->getLocalEntityAlias() != $link->getLocalTable()->getName()) { $foreignAliasName .= '_as_' . $link->getLocalEntityAlias(); } } $rel['required_link'] = false; $rel['link_method'] = 'link' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase(); $rel['unlink_method'] = 'deleteLink' . $link->getLocalEntityAsCamelCase() . 'To' . $link->getForeignEntityAsCamelCase(); } $rel['local_entity'] = $link->getLocalEntity(); $rel['foreign_entity'] = $link->getForeignEntity(); $relation[$foreignAliasName] = $rel; $relation[$foreignAliasName]['foreign_model'] = '\\Model\\' . $link->getForeignTable()->getNameAsCamelCase() . 'Model'; } $property = new PropertyGenerator('relation', $relation, PropertyGenerator::FLAG_PROTECTED); //$property->setDocBlock($docblock); $method = new MethodGenerator(); $method->setName('initRelation'); $method->setFinal(true); $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); $body = preg_replace("#^(\\s*)protected #", "\\1", $property->generate()) . "\n"; $body .= "\$this->setRelation(\$relation); \n"; $body .= "\$this->setupRelation();"; $docblock = new DocBlockGenerator('Настройка связей'); $method->setDocBlock($docblock); $method->setBody($body); //$file->getClass()->addPropertyFromGenerator($property); $file->getClass()->addMethodFromGenerator($method); }