Permalink
Browse files

Add RawType that does not prepare the values and just passes them str…

…aight through.
  • Loading branch information...
1 parent f7fc3fe commit a1e69fe47c22af7bb79277c009de56bd3b1dde94 @jwage jwage committed Jun 15, 2012
@@ -194,6 +194,11 @@
public $strategy = 'pushAll'; // pushAll, set
}
/** @Annotation */
+final class Raw extends AbstractField
+{
+ public $type = 'raw';
+}
+/** @Annotation */
final class EmbedOne extends AbstractField
{
public $type = 'one';
@@ -0,0 +1,53 @@
+<?php
+/*
+ * 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 MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\ODM\MongoDB\Mapping\Types;
+
+/**
+ * Raw data type.
+ *
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @link www.doctrine-project.org
+ * @since 1.0
+ * @author Jonathan H. Wage <jonwage@gmail.com>
+ * @author Roman Borschel <roman@code-factory.org>
+ * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
+ */
+class RawType extends Type
+{
+ public function convertToDatabaseValue($value)
+ {
+ return $value;
+ }
+
+ public function convertToPHPValue($value)
+ {
+ return $value;
+ }
+
+ public function closureToMongo()
+ {
+ return '$return = $value;';
+ }
+
+ public function closureToPHP()
+ {
+ return '$return = $value;';
+ }
+}
@@ -54,7 +54,8 @@
'hash' => 'Doctrine\ODM\MongoDB\Mapping\Types\HashType',
'collection' => 'Doctrine\ODM\MongoDB\Mapping\Types\CollectionType',
'increment' => 'Doctrine\ODM\MongoDB\Mapping\Types\IncrementType',
- 'object_id' => 'Doctrine\ODM\MongoDB\Mapping\Types\ObjectIdType'
+ 'object_id' => 'Doctrine\ODM\MongoDB\Mapping\Types\ObjectIdType',
+ 'raw' => 'Doctrine\ODM\MongoDB\Mapping\Types\RawType'
);
/**
@@ -0,0 +1,47 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Functional;
+
+use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
+
+class RawTypeTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+{
+ /**
+ * @dataProvider getTestRawTypeData
+ */
+ public function testRawType($value)
+ {
+ $test = new RawType();
+ $test->raw = $value;
+
+ $this->dm->persist($test);
+ $this->dm->flush();
+
+ $result = $this->dm->getDocumentCollection(get_class($test))->findOne(array('_id' => new \MongoId($test->id)));
+ $this->assertEquals($value, $result['raw']);
+ }
+
+ public function getTestRawTypeData()
+ {
+ return array(
+ array('test'),
+ array(1),
+ array(0),
+ array(array('test' => 'test')),
+ array(new \MongoDate()),
+ array(true),
+ array(array('date' => new \MongoDate())),
+ array(new \MongoId())
+ );
+ }
+}
+
+/** @ODM\Document */
+class RawType
+{
+ /** @ODM\Id */
+ public $id;
+
+ /** @ODM\Raw */
+ public $raw;
+}
@@ -0,0 +1,66 @@
+<?php
+
+namespace Doctrine\ODM\MongoDB\Tests\Persisters;
+
+use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
+use Doctrine\ODM\MongoDB\Tests\BaseTest;
+
+class DereferenceTest extends BaseTest
+{
+ private $pb;
+
+ public function setUp()
+ {
+ parent::setUp();
+ $this->pb = $this->dm->getUnitOfWork()->getPersistenceBuilder();
+ }
+
+ public function tearDown()
+ {
+ unset($this->pb);
+ parent::tearDown();
+ }
+
+ public function testDereferenceManyWithSetStrategyDoesNotUnsetFirst()
+ {
+ $post = new Post();
+ $post->title = 'test';
+ $this->dm->persist($post);
+ $this->dm->flush();
+ $this->dm->clear();
+
+ $post = $this->dm->find(get_class($post), $post->id);
+
+ $comment = new Comment();
+ $comment->subject = 'test';
+
+ $post->comments = array();
+ $post->comments[] = $comment;
+
+ $this->dm->flush();
+
+ }
+}
+
+/** @ODM\Document */
+class Post
+{
+ /** @ODM\Id */
+ public $id;
+
+ /** @ODM\String */
+ public $title;
+
+ /** @ODM\EmbedMany(targetDocument="Comment", strategy="set") */
+ public $comments = array();
+}
+
+/** @ODM\EmbeddedDocument */
+class Comment
+{
+ /** @ODM\Id */
+ public $id;
+
+ /** @ODM\String */
+ public $subject;
+}

0 comments on commit a1e69fe

Please sign in to comment.