Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid serializing compound objects? #20

Closed
franbenz opened this issue Jun 13, 2016 · 18 comments
Closed

How to avoid serializing compound objects? #20

franbenz opened this issue Jun 13, 2016 · 18 comments
Labels

Comments

@franbenz
Copy link

Hi Nil. Is there a way to avoid using the top-level included member?
Here is an example:

  1. The entity Invoice has a many to one relation to Customer;
  2. The entity Invoice_detail has a many to one relation to an Invoice;
    If I'm forced to use the included member, to avoid the doctrine closure issue, then every time I request an Invoice_detail to the jsonapi I would have to include in the request the Invoice and the Customer. Is this approach the right way to use the API?
@franbenz
Copy link
Author

Anyone else with the same issue?

@nilportugues
Copy link
Owner

@franbenz map it as a plain array.

But the standard uses included for a reason.

@franbenz
Copy link
Author

Thanks. Is there a doc about mappings?
About the standard, the spec mentions the optional use of Compound Documents "To reduce the number of HTTP requests".
But I don't think I should use a Compound Document for every request of Invoice_detail entities.

@nilportugues
Copy link
Owner

@franbenz A doc about mappings for this library you mean?

@franbenz
Copy link
Author

@nilportugues Yes, I can't find how to map a doctrine entity to a plain array in the yml mappings from this library.

@nilportugues
Copy link
Owner

There's no need to map arrays, the serializer will pick them up

@franbenz
Copy link
Author

@nilportugues
Hi Nil. When I use doctrine's HIDRATE_ARRAY and pass the result to the serializer, I get Notice: Undefined index: @type. Is there another way for the serializer to pick up the array as you suggested above?

Serialization:

$query = $manager->createQuery("SELECT o, c FROM AppBundle:Obras o
                    left join o.cliente c
                    where o.id = :idObra")->setParameter('idObra',$obra->getId());
$newObra = $query->getResult(Query::HYDRATE_ARRAY);
$jsonApiSerializer = $this->get('nil_portugues.serializer.json_api_serializer');
$serializedObject = $jsonApiSerializer->serialize($newObra);// Undefined index: @type

The $newObra array from doctrine looks like this:

*array* _(size=1)_
  0 => 
    *array* _(size=4)_
      'id' => int 68
      'nombre' => string '1234567890' _(length=10)_
      'activo' => boolean true
      'cliente' => 
        *array* _(size=3)_
          'id' => int 1
          'nombre' => string 'CLIENTE' _(length=7)_
          'activo' => boolean true

@nilportugues
Copy link
Owner

Mind updating to 1.4.0? I've applied some changes to the underlying lib that should fix this.

@franbenz
Copy link
Author

Hi @nilportugues . After the update to 1.4.0. the json-api package was updated. But I'm still getting "Undefined index: @type" when serializing the array generated by doctrine.

@nilportugues
Copy link
Owner

@franbenz thanks for the patience. Try this.

As it has @type... so it's definately an ArrayCollection from Doctrine, and not an array.

Write a mapper for it and let me know if it solves the problem.

@franbenz
Copy link
Author

The array I'm getting from Doctrine is not really an ArrayCollection, please look the var_dump from my previous comment. It does not include any class or type information.
This is a portion of the callstack:

1.  in vendor\nilportugues\json-api\src\Helpers\PropertyHelper.php at line 31 
2.  at ErrorHandler ->handleError ('8', 'Undefined index: @type', 'C:\xampp\htdocs\tapeApi\vendor\nilportugues\json-api\src\Helpers\PropertyHelper.php', '31', 
array('mappings' => array('AppBundle\Entity\Clientes' => object(Mapping), 'AppBundle\Entity\Items_Presupuesto' => object(Mapping), 'AppBundle\Entity\Obras' => object(Mapping), 'AppBundle\Entity\Presupuestos' => object(Mapping), 'AppBundle\Entity\User' => object(Mapping)), 
'value' => array('@map' => 'array', '@value' => array('id' => array('@scalar' => 'integer', '@value' => '94'), 'nombre' => array('@scalar' => 'string', '@value' => 'Obra de pruebaaa22'), 'activo' => array('@scalar' => 'boolean', '@value' => true), 'cliente' => array('@map' => 'array', '@value' => array('id' => array('@scalar' => 'integer', '@value' => '1'), 'nombre' => array('@scalar' => 'string', '@value' => 'CLIENTE'), 'activo' => array('@scalar' => 'boolean', '@value' => true))))))) 
in vendor\nilportugues\json-api\src\Helpers\PropertyHelper.php at line 31 
3. at PropertyHelper ::setResponseDataTypeAndId (
array('AppBundle\Entity\Clientes' => object(Mapping), 'AppBundle\Entity\Items_Presupuesto' => object(Mapping), 'AppBundle\Entity\Obras' => object(Mapping), 'AppBundle\Entity\Presupuestos' => object(Mapping), 'AppBundle\Entity\User' => object(Mapping)), 
array('@map' => 'array', '@value' => array('id' => array('@scalar' => 'integer', '@value' => '94'), 'nombre' => array('@scalar' => 'string', '@value' => 'Obra de pruebaaa22'), 'activo' => array('@scalar' => 'boolean', '@value' => true), 'cliente' => array('@map' => 'array', '@value' => array('id' => array('@scalar' => 'integer', '@value' => '1'), 'nombre' => array('@scalar' => 'string', '@value' => 'CLIENTE'), 'activo' => array('@scalar' => 'boolean', '@value' => true)))))) 
in vendor\nilportugues\json-api\src\JsonApiTransformer.php at line 97

@nilportugues
Copy link
Owner

@franbenz I can fix it. Give me second.

@nilportugues
Copy link
Owner

nilportugues commented Jun 23, 2016

@franbenz fetch new version with composer update nilportugues/json-api (Updates underlying library causing the error)

@franbenz
Copy link
Author

The undefined index notice is still occurring(empty function must receive a variable and not an expression). We've corrected it here, but now it happens in setResponseDataAttributes in vendor\nilportugues\json-api\src\Helpers\DataAttributesHelper.php at line 100. We don't know what should this function do when there is no type.

@nilportugues
Copy link
Owner

Mind sharing the object structure? Nestings, etc etc?

The code should not take you to DataAttributesHelper line 100 if it's not an object.

@franbenz
Copy link
Author

This is what the builtin serialize function from php returns for the array I'm passing to the serialize function from the json-api library. It's just an array:

string 'a:1:{i:0;a:4:{s:2:"id";i:100;s:6:"nombre";s:18:"Obra de pruebaaa22";s:6:"activo";b:1;s:7:"cliente";a:3:{s:2:"id";i:1;s:6:"nombre";s:7:"CLIENTE";s:6:"activo";b:1;}}}' (length=164)

This array was obtained from $array = $query->getResult(Query::HYDRATE_ARRAY);

@nilportugues
Copy link
Owner

@franbenz did you find a solution for these or I need to look into it?

@franbenz
Copy link
Author

franbenz commented Jul 2, 2016

For now we've stopped using these arrays serializations, though probably still a bug. There's just no hurry right now. Thanks Nil!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants