Getting junk data on running findAll or similar find methods for document containing embedded document
Closed this issue · 1 comments
Hi, I am using embedded document to achieve multi-level database structure. I am managed to set data in database correctly. But when I am fetching these data using findAll or findByOne or similar type of method, It is providing me with lot's of junk data. Part of these junks is pasted below.
[origin:Symfony\Component\Stopwatch\StopwatchEvent:private] => 1500196415142.2:Symfony\Component\Stopwatch\StopwatchEvent:private] => event_listener:Symfony\Component\Stopwatch\StopwatchEvent:private] => Array())[Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber] => Symfony\Component\Stopwatch\StopwatchEvent Object([periods:Symfony\Component\Stopwatch\StopwatchEvent:private] => Array ([0] => Symfony\Component\Stopwatch\StopwatchPeriod Object([start:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 31[end:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 31[memory:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 2097152)) [origin:Symfony\Component\Stopwatch\StopwatchEvent:private] => 1500196415142.2[category:Symfony\Component\Stopwatch\StopwatchEvent:private] => event_listener[started:Symfony\Component\Stopwatch\StopwatchEvent:private] => Array())[Symfony\Component\HttpKernel\EventListener\LocaleListener] => Symfony\Component\Stopwatch\StopwatchEvent Object([periods:Symfony\Component\Stopwatch\StopwatchEvent:private] => Array([0] => Symfony\Component\Stopwatch\StopwatchPeriod Object([start:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 31 [end:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 31[memory:Symfony\Component\Stopwatch\StopwatchPeriod:private] => 2097152))[origin:Symfony\Component\Stopwatch\StopwatchEvent:private] => 1500196415142.2
Below is document I am using to set and get data and also showing data available in mongodb through ubuntu terminal.
/**
* @MongoDB\Document
*/
class City
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $name;
/**
* @MongoDB\EmbedMany(targetDocument="Region")
*/
protected $regions = array();
/**
* @MongoDB\EmbedMany(targetDocument="Suburb")
*/
protected $suburbs = array();
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getRegions()
{
return $this->regions;
}
/**
* @param mixed $regions
*/
public function addRegions(Region $regions)
{
$this->regions[] = $regions;
}
/**
* @return mixed
*/
public function getSuburbs()
{
return $this->suburbs;
}
/**
* @param mixed $suburbs
*/
public function addSuburbs(Suburb $suburbs)
{
$this->suburbs[] = $suburbs;
}
}
/**
* @MongoDB\EmbeddedDocument()
*/
class Region
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $name;
/* Getters and Setters */
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
}
/**
* @MongoDB\EmbeddedDocument()
*/
class Suburb
{
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
*/
protected $name;
/**
* @MongoDB\Field(type="int")
*/
protected $postcode;
/* Getters and Setters */
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getPostcode()
{
return $this->postcode;
}
/**
* @param mixed $postcode
*/
public function setPostcode($postcode)
{
$this->postcode = $postcode;
}
}
Fetching in controller:
/**
* @Route("/getloc")
*/
public function getLocationAction()
{
$cities = $this->get('doctrine_mongodb')
->getRepository('AppBundle:City')
->findAll();
print_r($cities);
return new Response("");
}
I am able to properly set data in database and below is data I have inserted and getting through terminal.
{ "_id" : ObjectId("59708b6ad6faef0c7061486a"), "name" : "Capetown", "regions" : [ { "_id" : ObjectId("59708b6ad6faef0c7061486b"), "name" : "Region1" }, { "_id" : ObjectId("59708b6ad6faef0c7061486c"), "name" : "Region2" } ], "suburbs" : [ { "_id" : ObjectId("59708b6ad6faef0c7061486d"), "name" : "Suburb1" }, { "_id" : ObjectId("59708b6ad6faef0c7061486e"), "name" : "Suburb2" }, { "_id" : ObjectId("59708b6ad6faef0c7061486f"), "name" : "Suburb3" } ] }
Hi! I don't think the junk data you're refering to comes from the database itself (as that's clearly pictured by a dump from db in the post), it's rather from the print_r
you're using. Most probably it comes from collections holding reference to owning document manager, best way to "inspect" what's in the entity is using var_dump
with a limit on nested properties or utiziling Debug::dump
from doctrine/common.
Also I'm closing this issue as this is a duplicate from SO: https://stackoverflow.com/questions/45127002/fetching-embedded-data-getting-lots-of-extra-data