Example #1
0
/* Unique Indexes */
$authorUsernameUnique = new Unique('author_password_unique_idx');
$authorUsernameUnique->setColumns([['name' => 'username', 'size' => '8']]);
/* Behaviors */
$timestampableBehavior = new TimestampableBehavior();
$timestampableBehavior->setName('timestampable');
$sluggableBehavior = new SluggableBehavior();
$sluggableBehavior->setName('sluggable');
/* Tables */
$table1 = new Table('blog_post');
$table1->setDescription('The list of posts');
$table1->setNamespace('Blog');
$table1->setPackage('Acme.Blog');
$table1->addColumns([$column11, $column12, $column13, $column14, $column15, $column16, $column17]);
$table1->addForeignKeys([$fkAuthorPost, $fkCategoryPost]);
$table1->addBehavior($timestampableBehavior);
$table1->addBehavior($sluggableBehavior);
$table2 = new Table('blog_author');
$table2->setDescription('The list of authors');
$table2->setNamespace('Blog');
$table2->setPackage('Acme.Blog');
$table2->addColumns([$column21, $column22, $column23]);
$table2->addUnique($authorUsernameUnique);
$table3 = new Table('blog_category');
$table3->setDescription('The list of categories');
$table3->setNamespace('Blog');
$table3->setPackage('Acme.Blog');
$table3->addColumns([$column31, $column32]);
$table4 = new Table('blog_tag');
$table4->setDescription('The list of tags');
$table4->setNamespace('Blog');
 public function testHasBehaviors()
 {
     $behavior1 = $this->getBehaviorMock('Foo');
     $behavior2 = $this->getBehaviorMock('Bar');
     $behavior3 = $this->getBehaviorMock('Baz');
     $table = new Table();
     $table->addBehavior($behavior1);
     $table->addBehavior($behavior2);
     $table->addBehavior($behavior3);
     $this->assertCount(3, $table->getBehaviors());
     $this->assertTrue($table->hasBehavior('Foo'));
     $this->assertTrue($table->hasBehavior('Bar'));
     $this->assertTrue($table->hasBehavior('Baz'));
     $this->assertFalse($table->hasBehavior('Bab'));
     $this->assertSame($behavior1, $table->getBehavior('Foo'));
     $this->assertSame($behavior2, $table->getBehavior('Bar'));
     $this->assertSame($behavior3, $table->getBehavior('Baz'));
 }
Example #3
0
 public function startElement($parser, $name, $attributes)
 {
     $parentTag = $this->peekCurrentSchemaTag();
     if (false === $parentTag) {
         switch ($name) {
             case 'database':
                 if ($this->isExternalSchema()) {
                     $this->currentPackage = isset($attributes['package']) ? $attributes['package'] : null;
                     if (null === $this->currentPackage) {
                         $this->currentPackage = $this->defaultPackage;
                     }
                 } else {
                     $this->currDB = $this->schema->addDatabase($attributes);
                 }
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('database' === $parentTag) {
         switch ($name) {
             case 'external-schema':
                 $xmlFile = isset($attributes['filename']) ? $attributes['filename'] : null;
                 // 'referenceOnly' attribute is valid in the main schema XML file only,
                 // and it's ignored in the nested external-schemas
                 if (!$this->isExternalSchema()) {
                     $isForRefOnly = isset($attributes['referenceOnly']) ? $attributes['referenceOnly'] : null;
                     $this->isForReferenceOnly = null !== $isForRefOnly ? 'true' === strtolower($isForRefOnly) : true;
                     // defaults to TRUE
                 }
                 if ('/' !== $xmlFile[0]) {
                     $xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile);
                     if (!file_exists($xmlFile)) {
                         throw new SchemaException(sprintf('Unknown include external "%s"', $xmlFile));
                     }
                 }
                 $this->parseFile($xmlFile);
                 break;
             case 'domain':
                 $this->currDB->addDomain($attributes);
                 break;
             case 'table':
                 if (!isset($attributes['schema']) && $this->currDB->getSchema() && $this->currDB->getPlatform()->supportsSchemas() && false === strpos($attributes['name'], $this->currDB->getPlatform()->getSchemaDelimiter())) {
                     $attributes['schema'] = $this->currDB->getSchema();
                 }
                 $this->currTable = $this->currDB->addTable($attributes);
                 if ($this->isExternalSchema()) {
                     $this->currTable->setForReferenceOnly($this->isForReferenceOnly);
                     $this->currTable->setPackage($this->currentPackage);
                 }
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currDB->addVendorInfo($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currDB->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('table' === $parentTag) {
         switch ($name) {
             case 'column':
                 $this->currColumn = $this->currTable->addColumn($attributes);
                 break;
             case 'foreign-key':
                 $this->currFK = $this->currTable->addForeignKey($attributes);
                 break;
             case 'index':
                 $this->currIndex = new Index();
                 $this->currIndex->setTable($this->currTable);
                 $this->currIndex->loadMapping($attributes);
                 break;
             case 'unique':
                 $this->currUnique = new Unique();
                 $this->currUnique->setTable($this->currTable);
                 $this->currUnique->loadMapping($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currTable->addVendorInfo($attributes);
                 break;
             case 'id-method-parameter':
                 $this->currTable->addIdMethodParameter($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currTable->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('column' === $parentTag) {
         switch ($name) {
             case 'inheritance':
                 $this->currColumn->addInheritance($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currColumn->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('foreign-key' === $parentTag) {
         switch ($name) {
             case 'reference':
                 $this->currFK->addReference($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('index' === $parentTag) {
         switch ($name) {
             case 'index-column':
                 $this->currIndex->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currIndex->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('unique' === $parentTag) {
         switch ($name) {
             case 'unique-column':
                 $this->currUnique->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ($parentTag == 'behavior') {
         switch ($name) {
             case 'parameter':
                 $this->currBehavior->addParameter($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('vendor' === $parentTag) {
         switch ($name) {
             case 'parameter':
                 $this->currVendorObject->setParameter($attributes['name'], $attributes['value']);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } else {
         // it must be an invalid tag
         $this->_throwInvalidTagException($parser, $name);
     }
     $this->pushCurrentSchemaTag($name);
 }