1

I am new to Symfony/Doctrine. I created 2 entities for managing some comments and documents attached to them: Here is comment entity, and here comment document entity. Now the question is when fetch data from db like this:

 $comment = $em->getRepository('PathToBundle:Comment')->findOneBy( array('ordernumber' => '123456') ); 

and, let's say I wan't to debug it so I

 print_r($comment); 

It print's out something like this:

 Path\ToBundle\Entity\Comment Object ( [id:Path\ToBundle\Entity\Comment:private] => 1 [ordernumber:Path\ToBundle\Entity\Comment:private] => 123456 [category:Path\ToBundle\Entity\Comment:private] => cat1 [comment:Path\ToBundle\Entity\Comment:private] => com1 [user:Path\ToBundle\Entity\Comment:private] => usr1 [version:Path\ToBundle\Entity\Comment:private] => 0 [documents:Path\ToBundle\Entity\Comment:private] => Doctrine\ORM\PersistentCollection Object ( [snapshot:Doctrine\ORM\PersistentCollection:private] => Array ( ) [owner:Doctrine\ORM\PersistentCollection:private] => Path\ToBundle\Entity\Comment Object *RECURSION* [association:Doctrine\ORM\PersistentCollection:private] => Array ( [fieldName] => documents [mappedBy] => comment [targetEntity] => Path\ToBundle\Entity\CommentDocument [cascade] => Array ( ) [orphanRemoval] => [fetch] => 2 [type] => 4 [inversedBy] => [isOwningSide] => [sourceEntity] => Path\ToBundle\Entity\Comment [isCascadeRemove] => [isCascadePersist] => [isCascadeRefresh] => [isCascadeMerge] => [isCascadeDetach] => ) 

And it's just beginning, it goes on and on until browser crashes. But if try to access single property like

 print_r($input->getComment()); 

It works ok.

So is this behavior normal, or I done something wrong? And how can I access associated documents table values?

2 Answers 2

1

It's normal. Note that your first print_r attempt is on a Doctrine object and the second one is on a string. Doctrine objects are many levels deep and contain a lot of information. Instead of using print_r try using Doctrine's Debug class, which lets you specify a maximum depth.

http://www.doctrine-project.org/api/common/2.4/class-Doctrine.Common.Util.Debug.html

\Doctrine\Common\Util\Debug::dump($comment, $maxDepth) 

If your entities are setup correctly, you should be able to access associated documents by

$comments->getDocuments(); 
Sign up to request clarification or add additional context in comments.

Comments

1

That behavior is pretty much expected. The Entity objects provided by Doctrine have a lot of functionality stuffed into them. Dumping the raw object will output a lot of data.

That said, you shouldn't be dumping the Entity (or any large Class) objects. If you need debug information, limit outputs to only what is relevant.

I also think you're misunderstanding the purpose of an ORM like Doctrine.

And how can I access associated documents table values?

While it's technically feasible to access raw db features like table information through Doctrine's Class Metadata Factory, the short answer is that you don't want to. Doctrine abstracts away the underlying db structure, attempting to peel away that abstraction negates the entire reason for using an ORM in the first place.

Focus on utilizing the tools Doctrine provides you; what's going on under the hood is less important, especially for a beginner.

(The flip side of that argument is, of course, knowledge of your platform's inner workings is essential for advanced techniques, such as extending base Doctrine functionality with custom code. That should not be attempted by a beginner, though. Chances are you can do what you need to with the existing tools.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.