2012-07-05 5 views
1

을 사용하여 테이블 조에서 사용자를 가져올 수 없습니다내가 클래스 조에 클래스 사용자와의 관계가 교리

/** 
* @ORM\ManyToOne(targetEntity="User", inversedBy="articles") 
* @ORM\JoinColumn(name="author_id", referencedColumnName="id") 
*/ 
protected $author; 

을 나는 기사를 사용하여 얻을 :

$article = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Article") 
      ->findOneBy(array(
       'title' => $title, 
      )); 

나는 사용자가 얻을 때 :

$article->getAuthor(); 

위해서 var_dump 복귀 :

object(Proxies\IftodiDesignBundleEntityUserProxy)[305] 
    private '_entityPersister' => 
     object(Doctrine\ORM\Persisters\BasicEntityPersister)[309] 
     protected '_class' => 
      object(Doctrine\ORM\Mapping\ClassMetadata)[113] 
      public 'reflFields' => 
       array (size=8) 
       ... 
      private '_prototype' => null 
      public 'name' => string 'Iftodi\DesignBundle\Entity\User' (length=31) 
      public 'namespace' => string 'Iftodi\DesignBundle\Entity' (length=26) 
      public 'rootEntityName' => string 'Iftodi\DesignBundle\Entity\User' (length=31) 
      public 'customRepositoryClassName' => null 
      public 'isMappedSuperclass' => boolean false 
      public 'parentClasses' => 
       array (size=0) 
       ... 
      public 'subClasses' => 
       array (size=0) 
       ... 
      public 'namedQueries' => 
       array (size=0) 
       ... 
      public 'identifier' => 
       array (size=1) 
       ... 
      public 'inheritanceType' => int 1 
      public 'generatorType' => int 4 
      public 'fieldMappings' => 
       array (size=7) 
       ... 
      public 'fieldNames' => 
       array (size=7) 
       ... 
      public 'columnNames' => 
       array (size=7) 
       ... 
      public 'discriminatorValue' => null 
      public 'discriminatorMap' => 
       array (size=0) 
       ... 
      public 'discriminatorColumn' => null 
      public 'table' => 
       array (size=1) 
       ... 
      public 'lifecycleCallbacks' => 
       array (size=0) 
       ... 
      public 'associationMappings' => 
       array (size=1) 
       ... 
      public 'isIdentifierComposite' => boolean false 
      public 'containsForeignIdentifier' => boolean false 
      public 'idGenerator' => 
       object(Doctrine\ORM\Id\IdentityGenerator)[101] 
       ... 
      public 'sequenceGeneratorDefinition' => null 
      public 'tableGeneratorDefinition' => null 
      public 'changeTrackingPolicy' => int 1 
      public 'isVersioned' => null 
      public 'versionField' => null 
      public 'reflClass' => 
       object(ReflectionClass)[112] 
       ... 
      public 'isReadOnly' => boolean false 
     protected '_conn' => 
      object(Doctrine\DBAL\Connection)[120] 
      protected '_conn' => 
       object(Doctrine\DBAL\Driver\PDOConnection)[356] 
       ... 
      protected '_config' => 
       object(Doctrine\DBAL\Configuration)[127] 
       ... 
      protected '_eventManager' => 
       object(Doctrine\Common\EventManager)[124] 
       ... 
      protected '_expr' => 
       object(Doctrine\DBAL\Query\Expression\ExpressionBuilder)[119] 
       ... 
      private '_isConnected' => boolean true 
      private '_transactionNestingLevel' => int 0 
      private '_transactionIsolationLevel' => int 2 
      private '_nestTransactionsWithSavepoints' => null 
      private '_params' => 
       array (size=7) 
       ... 
      protected '_platform' => 
       object(Doctrine\DBAL\Platforms\MySqlPlatform)[118] 
       ... 
      protected '_schemaManager' => null 
      protected '_driver' => 
       object(Doctrine\DBAL\Driver\PDOMySql\Driver)[121] 
       ... 
      private '_isRollbackOnly' => boolean false 
     protected '_platform' => 
      object(Doctrine\DBAL\Platforms\MySqlPlatform)[118] 
      protected 'doctrineTypeMapping' => null 
      protected 'doctrineTypeComments' => null 
     protected '_em' => 
      object(Doctrine\ORM\EntityManager)[117] 
      private 'config' => 
       object(Doctrine\ORM\Configuration)[128] 
       ... 
      private 'conn' => 
       object(Doctrine\DBAL\Connection)[120] 
       ... 
      private 'metadataFactory' => 
       object(Doctrine\ORM\Mapping\ClassMetadataFactory)[116] 
       ... 
      private 'repositories' => 
       array (size=3) 
       ... 
      private 'unitOfWork' => 
       object(Doctrine\ORM\UnitOfWork)[115] 
       ... 
      private 'eventManager' => 
       object(Doctrine\Common\EventManager)[124] 
       ... 
      private 'hydrators' => 
       array (size=0) 
       ... 
      private 'proxyFactory' => 
       object(Doctrine\ORM\Proxy\ProxyFactory)[114] 
       ... 
      private 'expressionBuilder' => null 
      private 'closed' => boolean false 
     protected '_queuedInserts' => 
      array (size=0) 
      empty 
     protected '_rsm' => null 
     protected '_columnTypes' => 
      array (size=0) 
      empty 
     private '_insertSql' => null 
     protected '_selectColumnListSql' => null 
     protected '_selectJoinSql' => null 
     protected '_sqlAliasCounter' => int 0 
     protected '_sqlTableAliases' => 
      array (size=0) 
      empty 
    private '_identifier' => 
     array (size=1) 
     'id' => string '1' (length=1) 
    public '__isInitialized__' => boolean false 
    protected 'id' => null 
    protected 'username' => null 
    protected 'name' => null 
    protected 'email' => null 
    protected 'password' => null 
    protected 'salt' => null 
    protected 'role' => null 
    protected 'articles' => null 

나 제 엔티티를 사용하여 올바른 관계 선박 및 인 getUser 수 있도록 도와주세요.

나는 Symfony2를 Doctrine과 함께 사용한다.

편집 : 여기

는 제 엔티티 : http://pastebin.com/ZBQPehGL 다음은 사용자 엔티티 :

$articles = $this->getDoctrine() 
      ->getRepository("IftodiDesignBundle:Article") 
      ->findAll(); 
+0

데이터베이스에 수신 할 항목이 있는지 확인 했습니까? 프로파일 러에서 어떤 쿼리가 실행되는지 확인 했습니까? 클래스 ORM 정의와 특정 값에 대한 getter를 포함시킬 수 있습니까? – Sgoettschkes

답변

2

나는 이것이 올바른 행동이라고 생각 : http://pastebin.com/McEYdZ9W

프로파일이 쿼리를 표시하지 않습니다는 Doctrine Entity Proxies.

findOneBy() 호출이 데이터베이스 내부의 작성자와 결합하지 않기 때문에 Doctrine은 관련 작성자 오브젝트를 수화하지 않지만 작성자의 "실제"엔터티 대신 프록시 객체를 제공하여 데이터베이스 쿼리를 정말로 필요합니다.

작성자의 속성에 액세스 (예 : $ article-> getAuthor() -> getName())하고 그 후에 var 객체를 다시 var_dump하십시오. 이제 "실제"athor 엔터티를 볼 수 있습니다. 이는 작성자의 데이터에 액세스하면 프록시 클래스를 트리거하여 데이터베이스의 데이터를 자동으로로드하므로 발생합니다.

추가 읽기 :

추신 : 저자에게 내가 제안하는 방식에 액세스하는 별도의 데이터베이스 선택 쿼리에 LEED된다는 점에 유의해야합니다. 기사의 작성자 데이터가 항상 필요하다는 것을 알고 있다면 쿼리를 줄이기 위해 기사를 선택하는 쿼리에 조인을 추가해야합니다.)

관련 문제