Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.4] Added support for build-time behaviors in the XML schema (refs #…

  • Loading branch information...
commit 3bd67875397d9aff8e3c03152f92439617f41e5a 1 parent 5d90052
@fzaninotto fzaninotto authored
View
98 generator/classes/propel/engine/database/model/Behavior.php
@@ -0,0 +1,98 @@
+<?php
+/*
+ * $Id: Behavior.php 989 2008-03-11 14:29:30Z heltem $
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information please see
+ * <http://propel.phpdb.org>.
+ */
+
+include_once 'propel/engine/database/model/Index.php';
+
+/**
+ * Information about behaviors of a table.
+ *
+ * @author François Zaninotto
+ * @version $Revision: 989 $
+ * @package propel.engine.database.model
+ */
+class Behavior extends XMLElement {
+
+ protected $table;
+ protected $name;
+ protected $parameters = array();
+
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function setTable(Table $table)
+ {
+ $this->table = $table;
+ }
+
+ public function getTable()
+ {
+ return $this->table;
+ }
+
+ public function addParameter($attribute)
+ {
+ $attribute = array_change_key_case($attribute, CASE_LOWER);
+ $this->parameters[$attribute['name']] = $attribute['value'];
+ }
+
+ public function getParameters()
+ {
+ return $this->parameters;
+ }
+
+ public function getParameter($name)
+ {
+ return $this->parameters[$name];
+ }
+
+ /**
+ * Sets up the Behavior object based on the attributes that were passed to loadFromXML().
+ * @see parent::loadFromXML()
+ */
+ protected function setupObject()
+ {
+ $this->name = $this->getAttribute("name");
+ }
+
+ /**
+ * @see parent::appendXml(DOMNode)
+ */
+ public function appendXml(DOMNode $node)
+ {
+ $doc = ($node instanceof DOMDocument) ? $node : $node->ownerDocument;
+
+ $bNode = $node->appendChild($doc->createElement('behavior'));
+ $bNode->setAttribute('name', $this->getName());
+
+ foreach ($this->parameters as $name => $value) {
+ $parameterNode = $bNode->appendChild($doc->createElement('parameter'));
+ $parameterNode->setAttribute('name', $name);
+ $parameterNode->setAttribute('value', $value);
+ }
+ }
+}
View
39 generator/classes/propel/engine/database/model/Table.php
@@ -29,6 +29,8 @@
include_once 'propel/engine/database/model/ForeignKey.php';
include_once 'propel/engine/database/model/IdMethodParameter.php';
include_once 'propel/engine/database/model/Validator.php';
+include_once 'propel/engine/database/model/Behavior.php';
+
/**
* Data about a table used in an application.
@@ -747,6 +749,43 @@ public function addUnique($unqdata)
}
}
+
+ /**
+ * Adds a new Behavior to the table
+ */
+ public function addBehavior($bdata)
+ {
+ if ($bdata instanceof Behavior) {
+ $behavior = $bdata;
+ $behavior->setTable($this);
+ $this->behaviors[$behavior->getName()] = $behavior;
+ return $behavior;
+ } else {
+ $behavior = new Behavior($this);
+ $behavior->loadFromXML($bdata);
+ return $this->addBehavior($behavior);
+ }
+ }
+
+ /**
+ * Get the table behaviors
+ * @return Array of Behavior objects
+ */
+ public function getBehaviors()
+ {
+ return $this->behaviors;
+ }
+
+ /**
+ * Get one table behavior by name
+ * @param string $name the brhavior name
+ * @return Behavior a behavior object
+ */
+ public function getBehavior($name)
+ {
+ return $this->behaviors[$name];
+ }
+
/**
* Get the name of the Table
*/
View
9 generator/classes/propel/engine/database/model/XMLElement.php
@@ -155,4 +155,13 @@ public function toString()
$xmlstr = $doc->saveXML();
return trim(preg_replace('/<\?xml.*?\?>/', '', $xmlstr));
}
+
+ /**
+ * Magic string method
+ * @see toString()
+ */
+ public function __toString()
+ {
+ return $this->toString();
+ }
}
View
23 generator/classes/propel/engine/database/transform/XmlToAppData.php
@@ -52,6 +52,7 @@ class XmlToAppData extends AbstractHandler {
private $currIndex;
private $currUnique;
private $currValidator;
+ private $currBehavior;
private $currVendorObject;
private $isForReferenceOnly;
@@ -232,14 +233,18 @@ public function startElement($name, $attributes) {
$this->currVendorObject = $this->currTable->addVendorInfo($attributes);
break;
- case "validator":
+ case "validator":
$this->currValidator = $this->currTable->addValidator($attributes);
- break;
+ break;
- case "id-method-parameter":
+ case "id-method-parameter":
$this->currTable->addIdMethodParameter($attributes);
break;
-
+
+ case "behavior":
+ $this->currBehavior = $this->currTable->addBehavior($attributes);
+ break;
+
default:
$this->_throwInvalidTagException($name);
}
@@ -303,6 +308,16 @@ public function startElement($name, $attributes) {
default:
$this->_throwInvalidTagException($name);
}
+ } elseif ($parentTag == "behavior") {
+
+ switch($name) {
+ case "parameter":
+ $this->currBehavior->addParameter($attributes);
+ break;
+
+ default:
+ $this->_throwInvalidTagException($name);
+ }
} elseif ($parentTag == "validator") {
switch($name) {
case "rule":
View
7 generator/resources/dtd/database.dtd
@@ -42,7 +42,7 @@ PHP class or method name.
note: the interface="true", requires that useManagers=true in the
properties file.
-->
-<!ELEMENT table (column+,(foreign-key|index|unique|id-method-parameter|validator|vendor)*)>
+<!ELEMENT table (column+,(foreign-key|index|unique|id-method-parameter|validator|behavior|vendor)*)>
<!ATTLIST table
name CDATA #REQUIRED
phpName CDATA #IMPLIED
@@ -128,6 +128,11 @@ PHP class or method name.
name CDATA #IMPLIED
>
+<!ELEMENT behavior (parameter+)>
+<!ATTLIST index
+ name CDATA #IMPLIED
+>
+
<!ELEMENT index-column (vendor*)>
<!ATTLIST index-column
name CDATA #REQUIRED
View
8 generator/resources/xsd/database.xsd
@@ -283,6 +283,13 @@
<xs:attribute name="foreign" type="column_name" use="required"/>
</xs:complexType>
+ <xs:complexType name="behavior">
+ <xs:choice maxOccurs="unbounded">
+ <xs:element name="parameter" type="parameter" minOccurs="0" maxOccurs="unbounded"/>
+ </xs:choice>
+ <xs:attribute name="name" type="xs:string" use="optional"/>
+ </xs:complexType>
+
<xs:complexType name="column">
<xs:choice maxOccurs="unbounded">
<xs:element name="inheritance" type="inheritance" minOccurs="0" maxOccurs="unbounded"/>
@@ -344,6 +351,7 @@
<xs:element name="unique" type="unique" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="id-method-parameter" type="id-method-parameter" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="validator" type="validator" minOccurs="0" maxOccurs="unbounded"/>
+ <xs:element name="behavior" type="behavior" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="vendor" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="name" type="table_name" use="required"/>
View
12 generator/resources/xsl/database.xsl
@@ -116,6 +116,7 @@
<xsl:apply-templates select='id-method-parameter'/>
<xsl:apply-templates select='validator'/>
<xsl:apply-templates select='vendor'/>
+ <xsl:apply-templates select='behavior'/>
</table>
</xsl:template>
@@ -157,6 +158,17 @@
</xsl:template>
<!--
+ Just copy the behavior node with attributes and add the param
+ -->
+ <xsl:template match='behavior'>
+ <behavior>
+ <xsl:apply-templates select='@*'/>
+ <xsl:apply-templates select='parameter'/>
+ </behavior>
+ </xsl:template>
+
+
+ <!--
Just copy the unique-column node with attributes and add the vendor node
-->
<xsl:template match='unique-column'>
View
13 test/fixtures/bookstore/behavior-schema.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
+<database name="bookstore-behavior" defaultIdMethod="native" package="behavior">
+
+ <table name="b_user" phpName="User">
+ <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
+ <column name="first_name" type="VARCHAR" size="100" primaryString="true" />
+ <column name="created_at" type="TIMESTAMP" />
+ <behavior name="timestampable">
+ <parameter name="create_column" value="created_at" />
+ </behavior>
+ </table>
+
+</database>
View
53 test/testsuite/generator/engine/database/model/BehaviorTest.php
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * $Id: BehaviorTest.php 1133 2009-09-16 13:35:12Z francois $
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information please see
+ * <http://propel.phpdb.org>.
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+include_once 'propel/engine/database/transform/XmlToAppData.php';
+include_once 'propel/engine/platform/MysqlPlatform.php';
+
+/**
+ * Tests for Behavior class
+ *
+ * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a>
+ * @version $Revision: 1133 $
+ * @package generator.engine.database.model
+ */
+class BehaviorTest extends PHPUnit_Framework_TestCase {
+
+ private $xmlToAppData;
+ private $appData;
+
+ /**
+ * test if the tables get the package name from the properties file
+ *
+ */
+ public function testXmlToAppData() {
+ $this->xmlToAppData = new XmlToAppData(new MysqlPlatform(), "defaultpackage", null);
+ $this->appData = $this->xmlToAppData->parseFile('fixtures/bookstore/behavior-schema.xml');
+ $table = $this->appData->getDatabase("bookstore-behavior")->getTable('b_user');
+ $behaviors = $table->getBehaviors();
+ $this->assertEquals(count($behaviors), 1, 'XmlToAppData ads as many behaviors as there are behaviors tags');
+ $behavior = $table->getBehavior('timestampable');
+ $this->assertEquals($behavior->getTable()->getName(), 'b_user', 'XmlToAppData sets the behavior table correctly');
+ $this->assertEquals($behavior->getParameters(), array('create_column' => 'created_at'), 'XmlToAppData sets the behavior parameters correctly');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.