Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

proper way to load mongodb hash associated array mapping when not using annotations with weird accessors [closed]

Tags:

php

mongodb

I am doing this to map non annotation mapping of my document. But it is not catching it up. I know it is old code, but does someone know how to map it correctly. Thanks!

associated PR = https://github.com/Payum/PaypalExpressCheckoutNvp/pull/12/files#diff-fcfa75e424ccb89d62449aba21f9db31R49

And also related to this: https://groups.google.com/forum/#!topic/doctrine-user/MdIoOMWA7F4 https://github.com/doctrine/mongodb-odm/issues/421 https://github.com/doctrine/mongodb-odm/issues/453

<?php

abstract class MongoTest extends BaseMongoTest
{
/**
 * {@inheritDoc}
 */
protected function getMetadataDriverImpl()
{
    $rootDir = realpath(__DIR__.'/../../../../../../../../../');
    if (false === $rootDir || false === is_dir($rootDir.'/src/Payum')) {
        throw new \RuntimeException('Cannot guess Payum root dir.');
    }

$driver = new MappingDriverChain;
    $xmlDriver = new XmlDriver(
        new SymfonyFileLocator(
            array(
                $rootDir.'/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping'
                => 'Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Document',
                $rootDir.'/examples/Payum/Paypal/ExpressCheckout/Nvp/Examples/Resources/mapping'
                => 'Payum\Paypal\ExpressCheckout\Nvp\Examples\Document'
            ),
            '.mongodb.xml'
        ),
        '.mongodb.xml'
    );
    $driver->addDriver($xmlDriver, 'Payum\Paypal\ExpressCheckout\Nvp\Examples\Document');
    $driver->addDriver($xmlDriver, 'Payum\Paypal\ExpressCheckout\Nvp\Bridge\Doctrine\Document');


    return $driver;
}

I get errors of 2 tests failing because there is no persistence of the values properties of PaymentDetail Document under examples folder.

Here is the mapping of the PaymentDetails

https://github.com/cordoval/PaypalExpressCheckoutNvp/blob/mongo-tests/src/Payum/Paypal/ExpressCheckout/Nvp/Bridge/Doctrine/Resources/mapping/PaymentDetails.mongodb.xml?pr=%2FPayum%2FPaypalExpressCheckoutNvp%2Fpull%2F12

and the mapping for the superclass

https://github.com/cordoval/PaypalExpressCheckoutNvp/blob/mongo-tests/examples/Payum/Paypal/ExpressCheckout/Nvp/Examples/Resources/mapping/PaymentDetails.mongodb.xml?pr=%2FPayum%2FPaypalExpressCheckoutNvp%2Fpull%2F12

It seems the issue is because of weird setter/getter of BaseModel which is extended by PaymentDetails.

protected $paymentrequest_nnn_amt = array();

    public function getPaymentrequestAmt($n = null)
    {
        return $this->get('paymentrequest_nnn_amt', $n);
    }

    public function setPaymentrequestAmt($n, $value)
    {
        $this->set('paymentrequest_nnn_amt', $value, $n);
    }

that above is from an intermediate base class, and here below is from the base class

/**
 * @param string $property
 * @param bool   $n
 * @param bool   $m
 *
 * @return mixed
 */
protected function get($property, $n = false, $m = false)
{
    $currentValue = $this->$property;
    if (false !== $n && false !== $m) {
        if (null === $n && null === $m) {
            return $currentValue;
        }
        if (array_key_exists($n, $currentValue) && array_key_exists($m,$currentValue[$n]){
            return $currentValue[$n][$m];
        }
    }
    if (null === $n) {
        return $currentValue;
    }
    if (array_key_exists($n, $currentValue)) {
        return $currentValue[$n];
    }
}
like image 523
cordoval Avatar asked Oct 08 '13 20:10

cordoval


1 Answers

I found out and fixed the problem. There were several issue I've run into:

  • First, After using ORM for long time I was confused that name is a field in mongo and fieldName is a property name (issue).
  • Second, If the fieldName does not match any property it is just silently skipped (where ORM throws exception). So it was hard to find out why the property is not saved. (issue).

After fixing the mapping everything works fine.

like image 144
Maksim Kotlyar Avatar answered Nov 08 '22 22:11

Maksim Kotlyar