1
0
mirror of synced 2025-01-17 22:11:41 +03:00

xml driver

This commit is contained in:
Fabio B. Silva 2012-03-07 22:03:24 -03:00
parent 6e93186db4
commit 531eb68d56
5 changed files with 297 additions and 2 deletions

View File

@ -85,6 +85,58 @@ class XmlDriver extends AbstractFileDriver
}
}
// Evaluate native named queries
if (isset($xmlRoot->{'named-native-queries'})) {
foreach ($xmlRoot->{'named-native-queries'}->{'named-native-query'} as $nativeQueryElement) {
$metadata->addNamedNativeQuery(array(
'name' => isset($nativeQueryElement['name']) ? (string)$nativeQueryElement['name'] : null,
'query' => isset($nativeQueryElement->query) ? (string)$nativeQueryElement->query : null,
'resultClass' => isset($nativeQueryElement['result-class']) ? (string)$nativeQueryElement['result-class'] : null,
'resultSetMapping' => isset($nativeQueryElement['result-set-mapping']) ? (string)$nativeQueryElement['result-set-mapping'] : null,
));
}
}
// Evaluate sql result set mapping
if (isset($xmlRoot->{'sql-result-set-mappings'})) {
foreach ($xmlRoot->{'sql-result-set-mappings'}->{'sql-result-set-mapping'} as $rsmElement) {
$entities = array();
$columns = array();
foreach ($rsmElement as $entityElement) {
//<entity-result/>
if (isset($entityElement['entity-class'])) {
$entityResult = array(
'fields' => array(),
'entityClass' => (string)$entityElement['entity-class'],
'discriminatorColumn' => isset($entityElement['discriminator-column']) ? (string)$entityElement['discriminator-column'] : null,
);
foreach ($entityElement as $fieldElement) {
$entityResult['fields'][] = array(
'name' => isset($fieldElement['name']) ? (string)$fieldElement['name'] : null,
'column' => isset($fieldElement['column']) ? (string)$fieldElement['column'] : null,
);
}
$entities[] = $entityResult;
}
//<column-result/>
if (isset($entityElement['name'])) {
$columns[] = array(
'name' => (string)$entityElement['name'],
);
}
}
$metadata->addSqlResultSetMapping(array(
'name' => (string)$rsmElement['name'],
'entities' => $entities,
'columns' => $columns
));
}
}
/* not implemented specially anyway. use table = schema.table
if (isset($xmlRoot['schema'])) {
$metadata->table['schema'] = (string)$xmlRoot['schema'];

View File

@ -495,7 +495,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testNamedNativeQuery()
{
if (!$this instanceof AnnotationDriverTest) {
if (!$this instanceof AnnotationDriverTest && !$this instanceof XmlMappingDriverTest) {
$this->markTestIncomplete();
}
@ -551,7 +551,7 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
*/
public function testSqlResultSetMapping()
{
if (!$this instanceof AnnotationDriverTest) {
if (!$this instanceof AnnotationDriverTest && !$this instanceof XmlMappingDriverTest) {
$this->markTestIncomplete();
}

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\CMS\CmsAddress" table="cms_users">
<named-native-queries>
<named-native-query name="find-all" result-set-mapping="mapping-find-all">
<query>SELECT id, country, city FROM cms_addresses</query>
</named-native-query>
<named-native-query name="find-by-id" result-class="CmsAddress">
<query>SELECT * FROM cms_addresses WHERE id = ?</query>
</named-native-query>
<named-native-query name="count" result-set-mapping="mapping-count">
<query>SELECT COUNT(*) AS count FROM cms_addresses</query>
</named-native-query>
</named-native-queries>
<sql-result-set-mappings>
<sql-result-set-mapping name="mapping-find-all">
<entity-result entity-class="CmsAddress">
<field-result name="id" column="id"/>
<field-result name="city" column="city"/>
<field-result name="country" column="country"/>
</entity-result>
</sql-result-set-mapping>
<sql-result-set-mapping name="mapping-without-fields">
<entity-result entity-class="CmsAddress"/>
</sql-result-set-mapping>
<sql-result-set-mapping name="mapping-count">
<column-result name="count"/>
</sql-result-set-mapping>
</sql-result-set-mappings>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="country" column="country" type="string" length="50"/>
<field name="city" column="city" type="string" length="50"/>
<field name="zip" column="zip" type="string" length="50"/>
<one-to-one field="address" target-entity="CmsAddress" inversed-by="user" orphan-removal="true">
<cascade>
<cascade-persist/>
</cascade>
<join-column name="address_id" referenced-column-name="id" />
</one-to-one>
<one-to-one field="email" target-entity="CmsUser" inversed-by="address">
<join-column referenced-column-name="id" nullable="true" />
</one-to-one>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,121 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\CMS\CmsUser" table="cms_users">
<named-queries>
<named-query name="all" query="SELECT u FROM __CLASS__ u"/>
</named-queries>
<named-native-queries>
<named-native-query name="fetchIdAndUsernameWithResultClass" result-class="CmsUser">
<query>SELECT id, username FROM cms_users WHERE username = ?</query>
</named-native-query>
<named-native-query name="fetchAllColumns" result-class="CmsUser">
<query>SELECT * FROM cms_users WHERE username = ?</query>
</named-native-query>
<named-native-query name="fetchJoinedAddress" result-set-mapping="mappingJoinedAddress">
<query>SELECT u.id, u.name, u.status, a.id AS a_id, a.country, a.zip, a.city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?</query>
</named-native-query>
<named-native-query name="fetchJoinedPhonenumber" result-set-mapping="mappingJoinedPhonenumber">
<query>SELECT id, name, status, phonenumber AS number FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username = ?</query>
</named-native-query>
<named-native-query name="fetchUserPhonenumberCount" result-set-mapping="mappingUserPhonenumberCount">
<query>SELECT id, name, status, COUNT(phonenumber) AS numphones FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username IN (?) GROUP BY id, name, status, username ORDER BY username</query>
</named-native-query>
</named-native-queries>
<sql-result-set-mappings>
<sql-result-set-mapping name="mappingJoinedAddress">
<entity-result entity-class="__CLASS__">
<field-result name="id"/>
<field-result name="name"/>
<field-result name="status"/>
<field-result name="address.zip"/>
<field-result name="address.city"/>
<field-result name="address.country"/>
<field-result name="address.id" column="a_id"/>
</entity-result>
</sql-result-set-mapping>
<sql-result-set-mapping name="mappingJoinedPhonenumber">
<entity-result entity-class="CmsUser">
<field-result name="id"/>
<field-result name="name"/>
<field-result name="status"/>
<field-result name="phonenumbers.phonenumber" column="number"/>
</entity-result>
</sql-result-set-mapping>
<sql-result-set-mapping name="mappingUserPhonenumberCount">
<entity-result entity-class="CmsUser">
<field-result name="id"/>
<field-result name="name"/>
<field-result name="status"/>
</entity-result>
<column-result name="numphones"/>
</sql-result-set-mapping>
</sql-result-set-mappings>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="status" column="status" type="string" length="50" unique="true"/>
<field name="username" column="username" type="string" length="255" unique="true"/>
<field name="name" column="name" type="string" length="255"/>
<one-to-many field="phonenumbers" target-entity="CmsPhonenumber" mapped-by="user" orphan-removal="true">
<cascade>
<cascade-persist/>
<cascade-merge/>
</cascade>
</one-to-many>
<one-to-many field="articles" target-entity="CmsArticle" mapped-by="user">
<cascade>
<cascade-detach/>
</cascade>
</one-to-many>
<one-to-one field="address" target-entity="CmsAddress" inversed-by="user" orphan-removal="true">
<cascade>
<cascade-persist/>
</cascade>
<join-column name="address_id" referenced-column-name="id" />
</one-to-one>
<one-to-one field="email" target-entity="CmsEmail" inversed-by="user" orphan-removal="true">
<cascade>
<cascade-persist/>
</cascade>
<join-column referenced-column-name="id" nullable="true" />
</one-to-one>
<many-to-many field="groups" target-entity="CmsGroup">
<cascade>
<cascade-persist/>
<cascade-detach/>
<cascade-merge/>
</cascade>
<join-table name="cms_users_groups">
<join-columns>
<join-column name="user_id" referenced-column-name="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="group_id" referenced-column-name="id"/>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Doctrine\Tests\Models\Company\CompanyPerson" table="company_persons" inheritance-type="JOINED">
<named-native-queries>
<named-native-query name="fetchAllWithResultClass" result-class="__CLASS__">
<query>SELECT id, name, discr FROM company_persons ORDER BY name</query>
</named-native-query>
</named-native-queries>
<named-native-queries>
<named-native-query name="fetchAllWithSqlResultSetMapping" result-set-mapping="mappingFetchAll">
<query>SELECT id, name, discr AS discriminator FROM company_persons ORDER BY name</query>
</named-native-query>
</named-native-queries>
<sql-result-set-mappings>
<sql-result-set-mapping name="mappingFetchAll">
<entity-result entity-class="__CLASS__" discriminator-column="discriminator">
<field-result name="id"/>
<field-result name="name"/>
</entity-result>
</sql-result-set-mapping>
</sql-result-set-mappings>
<discriminator-map >
<discriminator-mapping value="person" class="CompanyPerson" />
<discriminator-mapping value="manager" class="CompanyManager" />
<discriminator-mapping value="employee" class="CompanyEmployee" />
</discriminator-map>
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="name" column="name"/>
<one-to-one field="spouse" target-entity="CompanyPerson">
<join-column name="spouse_id" referenced-column-name="id"/>
</one-to-one>
<many-to-many field="friends" target-entity="CompanyPerson">
<join-table name="company_persons_friends">
<join-columns>
<join-column name="person_id" referenced-column-name="id"/>
</join-columns>
<inverse-join-columns>
<join-column name="friend_id" referenced-column-name="id"/>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>