Code Monkey home page Code Monkey logo

Comments (9)

goetas avatar goetas commented on May 26, 2024 4

Solution here is simple:

  • if you are using the entity only for serialization:
    /**
	 * @var \Ramsey\Uuid\Uuid
	 *
	 * @ORM\Id
         * @Serializer\Type("string")
	 * @ORM\Column(type="uuid", unique=true)
	 * @ORM\GeneratedValue(strategy="CUSTOM")
	 * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
     */
    private $storeMonitoredProductId;

A more detailed explanation can be found on schmittjoh/serializer#846 (comment)

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024 1

This Doctrine plugin returns the UUID value from the database as a Ramsey\Uuid\Uuid object. Calling toString() on the object or echoing the object is the appropriate way to return the string version.

However, the Uuid object implements JsonSerializable, so when you pass it to json_encode(), it should return the string UUID and not an object representation. What are you using to encode it as JSON.

Please use shorter code examples. 😄

from uuid-doctrine.

din-gi avatar din-gi commented on May 26, 2024

I actually fixed the mistake which was on my side.. I have added multiple @ORM\Id indicators..

Now last question is how do I get only the string of the UUID and not all of that UUID data?

{
                "factory": {
                    "codec": {
                        "builder": {
                            "converter": {}
                        }
                    },
                    "nodeProvider": {
                        "nodeProviders": [
                            {},
                            {}
                        ]
                    },
                    "numberConverter": {},
                    "randomGenerator": {},
                    "timeGenerator": {
                        "nodeProvider": {
                            "nodeProviders": [
                                {},
                                {}
                            ]
                        },
                        "timeConverter": {},
                        "timeProvider": {}
                    },
                    "uuidBuilder": {
                        "converter": {}
                    }
                },
                "codec": {
                    "builder": {
                        "converter": {}
                    }
                },
                "fields": {
                    "time_low": "00532d60",
                    "time_mid": "6a9c",
                    "time_hi_and_version": "58b4",
                    "clock_seq_hi_and_reserved": "c9",
                    "clock_seq_low": "e7",
                    "node": "c566472cfc4c"
                },
                "converter": {}
            }

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024

This looks like a JSON represenation of the Uuid object. What is the line of code you are using that generates this? It’s not something I’ve seen before, so I’m not sure how you’re generating this.

from uuid-doctrine.

din-gi avatar din-gi commented on May 26, 2024

Yes.. as I said.. this was a JSON representation of the UUID object of this library.

I have added a var_dump to the repository code of mine..
here is my function in the repository:

$qb = $this->getEntityManager()->createQueryBuilder();
		$qb->select(
				'StoreMonitoredProducts.storeMonitoredProductId,
				StoreMonitoredProducts.sourceMonitoredProductId,
		 		StoreMonitoredProducts.apiAccessId,
		 		StoreMonitoredProducts.dropMarketplaceId,
		 		MarketplacesDrop.marketplaceName as dropMarketplaceName,
		 		StoreMonitoredProducts.dropSiteId,
		 		StoreMonitoredProducts.itemName,
		 		StoreMonitoredProducts.dropQuantity,
		 		StoreMonitoredProducts.dropPrice,
		 		StoreMonitoredProducts.breakEven,
		 		StoreMonitoredProducts.desiredProfit,
		 		StoreMonitoredProducts.lastUpdate,
		 		SourceMonitoredProducts.sourceMarketplaceId,
		 		MarketplacesSource.marketplaceName as sourceMarketplaceName,
		 		SourceMonitoredProducts.sourceSiteId,
		 		SourceMonitoredProducts.sourceQuantity,
		 		SourceMonitoredProducts.sourcePrice,
		 		SourceMonitoredProducts.lastScan,
				StoreMonitoredProducts.profit,
				StoreMonitoredProducts.views,
				StoreMonitoredProducts.watchers,
				StoreMonitoredProducts.sold,
				StoreMonitoredProducts.sellingRatio'
				)
			->from('CoreMarketplaceBundle\Entity\StoreMonitoredProducts', 'StoreMonitoredProducts')
			->leftJoin(
				'CoreMarketplaceBundle\Entity\SourceMonitoredProducts',
				'SourceMonitoredProducts',
				\Doctrine\ORM\Query\Expr\Join::LEFT_JOIN,
				'StoreMonitoredProducts.sourceMonitoredProductId = SourceMonitoredProducts.sourceMonitoredProductId'
			)
			->leftJoin(
				'CoreMarketplaceBundle\Entity\Marketplaces',
				'MarketplacesDrop',
				\Doctrine\ORM\Query\Expr\Join::INNER_JOIN,
				'StoreMonitoredProducts.dropMarketplaceId = MarketplacesDrop.marketplaceId'
			)
			->leftJoin(
				'CoreMarketplaceBundle\Entity\Marketplaces',
				'MarketplacesSource',
				\Doctrine\ORM\Query\Expr\Join::INNER_JOIN,
				'SourceMonitoredProducts.sourceMarketplaceId = MarketplacesSource.marketplaceId'
			)
			->orderBy('StoreMonitoredProducts.apiAccessId', 'ASC');

		if (isset($apiAccessId))
		{
			$qb->andWhere('StoreMonitoredProducts.apiAccessId = ?1');
			$qb->setParameter(1, $apiAccessId);
		}
		if (isset($marketplace))
		{
			$qb->andWhere('MarketplacesDrop.marketplaceName = ?2');
			$qb->setParameter(2, $marketplace);
		}
		if (isset($storeMonitoredProductId))
		{
			$storeMonitoredProductId = (int)$storeMonitoredProductId;
			$qb->andWhere('StoreMonitoredProducts.storeMonitoredProductId = ?3');
			$qb->setParameter(3, $marketplace);
		}
		if ($retrieveProductData)
		{
			// If we also want to retrieve product data
			$qb->addSelect('ProductData.productData');
			$qb->leftJoin(
					'CoreMarketplaceBundle\Entity\ProductData',
					'ProductData',
					\Doctrine\ORM\Query\Expr\Join::LEFT_JOIN,
					'StoreMonitoredProducts.productDataId = ProductData.productDataId'
			);
		}

		$query = $qb->getQuery();

		$results = $query->getResult();
		return $result;

So the result I receive from this function for example is the following:

array (size=2)
  0 => 
    array (size=23)
      'storeMonitoredProductId' => 
        object(Ramsey\Uuid\Uuid)[663]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'sourceMonitoredProductId' => 
        object(Ramsey\Uuid\Uuid)[665]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'apiAccessId' => 
        object(Ramsey\Uuid\Uuid)[669]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'dropMarketplaceId' => int 2
      'dropMarketplaceName' => string 'ebay' (length=4)
      'dropSiteId' => string '110185928902' (length=12)
      'itemName' => string 'TP Link' (length=7)
      'dropQuantity' => int 1
      'dropPrice' => string '20.00' (length=5)
      'breakEven' => string '21' (length=2)
      'desiredProfit' => string '25' (length=2)
      'lastUpdate' => null
      'sourceMarketplaceId' => int 1
      'sourceMarketplaceName' => string 'amazon' (length=6)
      'sourceSiteId' => string 'B01AK9TC0Y' (length=10)
      'sourceQuantity' => int 1
      'sourcePrice' => string '198.19' (length=6)
      'lastScan' => null
      'profit' => string '0.00' (length=4)
      'views' => int 0
      'watchers' => int 0
      'sold' => int 0
      'sellingRatio' => string '0.00' (length=4)
  1 => 
    array (size=23)
      'storeMonitoredProductId' => 
        object(Ramsey\Uuid\Uuid)[682]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'sourceMonitoredProductId' => 
        object(Ramsey\Uuid\Uuid)[672]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'apiAccessId' => 
        object(Ramsey\Uuid\Uuid)[513]
          protected 'codec' => 
            object(Ramsey\Uuid\Codec\StringCodec)[666]
              ...
          protected 'fields' => 
            array (size=6)
              ...
          protected 'converter' => 
            object(Ramsey\Uuid\Converter\Number\DegradedNumberConverter)[660]
              ...
      'dropMarketplaceId' => int 2
      'dropMarketplaceName' => string 'ebay' (length=4)
      'dropSiteId' => string '110186189158' (length=12)
      'itemName' => string 'Sandisk' (length=7)
      'dropQuantity' => int 1
      'dropPrice' => string '20.00' (length=5)
      'breakEven' => string '21' (length=2)
      'desiredProfit' => string '16' (length=2)
      'lastUpdate' => null
      'sourceMarketplaceId' => int 1
      'sourceMarketplaceName' => string 'amazon' (length=6)
      'sourceSiteId' => string 'B00VXMY262' (length=10)
      'sourceQuantity' => int 1
      'sourcePrice' => string '32.14' (length=5)
      'lastScan' => null
      'profit' => string '0.00' (length=4)
      'views' => int 0
      'watchers' => int 0
      'sold' => int 0
      'sellingRatio' => string '0.00' (length=4)

If I had a manual conversion to string, a line before the result, for example:

		$results[0]['storeMonitoredProductId'] = ($results[0]['storeMonitoredProductId'])->toString();

It will give me a string representation of the UUID.. that's what I want - I don't want to receive the full UUID object, is it possible?

Any idea?

from uuid-doctrine.

din-gi avatar din-gi commented on May 26, 2024

Hey Ramsey,

Sorry for the long examples.. I am working hard on my project and had no time due to my personal work.. no longer long examples..
Thanks for the help.

I am using JMS Serializer to serialize my objects. here's the code:

	public function generateGoodResponse($jsonResponse)
	{
		$context = new SerializationContext();
		$context->setSerializeNull(true);
		$serializer  = SerializerBuilder::create()
			->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())
			->build();
		$jsonContent = $serializer->serialize($jsonResponse, 'json', $context);

		$response = new Response($jsonContent);
		$response->headers->set('Content-Type', 'application/json');

		return $response;
	}

Also.. What is actually the difference between the library and the default guid of Doctrine? I still can't totally manage to understand. Thanks!

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024

It looks like the problem is with jms/serializer. It doesn't appear to respect PHP's JsonSerializable interface to properly convert the object as intended. Instead, it appears to iterate over the properties of the object and creates a JSON object based on those.

Here's an example:

composer.json:

{
    "require": {
        "ramsey/uuid": "^3.7",
        "jms/serializer": "^1.9"
    }
}

serialize-uuid.php:

<?php
require_once 'vendor/autoload.php';

$uuid = \Ramsey\Uuid\Uuid::uuid4();
$serializer = JMS\Serializer\SerializerBuilder::create()->build();

echo "Using jms/serializer:\n\n";
echo $serializer->serialize($uuid, 'json');
echo "\n\n";

echo "Using json_encode():\n\n";
echo json_encode($uuid);
echo "\n\n";

Running this script, we see the following output:

$ php serialize-uuid.php
Using jms/serializer:

{"factory":{"codec":{"builder":{"converter":{}}},"node_provider":{"node_providers":[{},{}]},"number_converter":{},"random_generator":{},"time_generator":{"node_provider":{"node_providers":[{},{}]},"time_converter":{},"time_provider":{}},"uuid_builder":{"converter":{}}},"codec":{"builder":{"converter":{}}},"fields":{"time_low":"4abe89df","time_mid":"76e6","time_hi_and_version":"452a","clock_seq_hi_and_reserved":"97","clock_seq_low":"69","node":"c93997076b53"},"converter":{}}

Using json_encode():

"4abe89df-76e6-452a-9769-c93997076b53"

from uuid-doctrine.

din-gi avatar din-gi commented on May 26, 2024

hmm..

Thanks.. I understood the problem,
And thanks for creating a feature request on JMS.

I will close the issue as this is not related to this library.
Have a lovely day.

from uuid-doctrine.

r3m4k3 avatar r3m4k3 commented on May 26, 2024

@goetas Thank you, that works.

from uuid-doctrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.