0x3d
0x3d

Reputation: 470

Can't get User from table Article using Doctrine

I have class User, and relation with class Article:

/**
 * @ORM\OneToMany(targetEntity="Article", mappedBy="author", cascade={"persist"})   
 * @var ArrayCollection $articles
 */
protected $articles;

In class Article I have inversed relation:

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

I get articles using:

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

When I try to get User:

 $article->getAuthor();

var_dump return:

    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

Please help me to make correct relation ship and getUser using Article Entity.

I use Symfony2 with Doctrine.

Edit:

Here is Article entity: http://pastebin.com/ZBQPehGL Here is User entity: http://pastebin.com/McEYdZ9W

Profiler don't show this query:

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

Upvotes: 1

Views: 1589

Answers (1)

andesk
andesk

Reputation: 91

I believe this is the correct behavior of Doctrine Entity Proxies.

As your findOneBy() call does not join the author inside the database, Doctrine does not hydrate the related author object but provides a Proxy Object instead of the "real" Entity of the author to reduce database queries to those that are really needed.

Try to access an attribute of the author (like $article->getAuthor()->getName()) and var_dump the author object after that again. Now you should see the "real" athor Entity. This happens as accessing the data of the author will "trigger" the proxy class to load the data from the database automatically.

Further reading:

PS: Be aware that accessing the author the way I suggested will leed to an extra database select query. If you know you will always need author data of articles you should add a join to the query where you select the articles to reduce the amount of queries ;)

Upvotes: 2

Related Questions