2014-06-27 130 views
1

我是Symfony/Doctrine的新手。 我创建了2个实体来管理附加到它们的一些注释和文档: 这里是comment entity,这里comment document entity。 现在的问题是从数据库这样获取数据时:Symfony2学说关联结果

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

,并让我们说我wan't调试它,所以我

print_r($comment); 

它打印的出这样的事:

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] => 
        ) 

它刚刚开始,它一直持续到浏览器崩溃。但如果尝试访问单个属性,如

print_r($input->getComment()); 

它工作正常。

那么这种行为是否正常,或者我做错了什么?我怎样才能访问关联的文档表值?

回答

1

这很正常。请注意,您的第一个print_r尝试位于Doctrine对象上,第二个位于字符串上。学说对象有很多层次,包含很多信息。而不是使用print_r尝试使用Doctrine的Debug类,它允许您指定最大深度。

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

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

如果你的实体是设置正确,你应该能够通过

$comments->getDocuments(); 
访问相关文件
1

这种行为非常期待。由Doctrine提供的实体对象具有填充到它们中的功能的批号。转储原始对象将输出大量数据。

也就是说,您不应该倾销实体(或任何大类)对象。如果您需要调试信息,请将输出限制为仅与相关的内容相关。

我也认为你误解了像教条这样的ORM的目的。

如何访问关联的文档表值?

尽管通过Doctrine的类元数据工厂访问原始数据库功能(如表格信息)在技术上是可行的,但简短的答案是您不想。 Doctrine抽象出底层的数据库结构,试图剥离抽象层,首先否定了使用ORM的全部理由。

专注于利用工具Doctrine为您提供;引擎盖下发生的事情并不重要,特别是对初学者来说。当然,对于高级技术来说,你的平台的内部工作知识是必不可少的,比如用自定义代码扩展基本的学说功能,尽管这不应该由初学者尝试。你是否可以用现有的工具做你所需要的。)