Doctrine2:延迟加载失败,我必须重新初始化该类

I have three entities wallpost, albums and photos.

  1. wallpost has many to one relationship with albums.
  2. album has one to many relationship with photos.

When I post photos on my wall, the data goes into all three tables. One record in wallpost, one in album and multiple in photos.

Now when I fetch data using

$wall_post_obj->getAlbum()->getId()

It shows id but when I try to fetch any other info such as

$wall_post_obj->getAlbum()->getName()

It returns NULL.

This happens only when I delete one photo from album.

To get it work I have to re-initialize it using find query. like this: $em->find('\Entities\album',$id);


Extra information:

The $wall_post_obj comes from following query:

        $qb_1 = $em->createQueryBuilder();
        $q_1 = $qb_1->select('wp, fromUsr.id, 
                              fromUsr.professional_image, 
                              fromUsr.gender, 
                              fromUsr.firstname, 
                              fromUsr.lastname')
        ->from( '\Entities\wall_post', 'wp' )
        ->LeftJoin( 'wp.wall_postsFrom_user', 'fromUsr' )
        ->where( 'wp.id IN (:selected_wp_ids_r)' )
        ->setParameter( 'selected_wp_ids_r', $selected_wp_ids_r )
        ->orderBy( 'wp.last_activity_datetime', 'DESC' );

        // wallpost Comments join
        $q_1->addSelect( 'wall_comments, comment_user' );
        $q_1->LeftJoin( 'wp.wall_postsComment', 'wall_comments' );
        $q_1->LeftJoin( 'wall_comments.commentsIlook_user', 'comment_user' );

        // wallpost Likes join
        $q_1->addSelect( 'likes' );
        $q_1->LeftJoin( 'wp.wall_postsLikes', 'likes' );
        $q_1->LeftJoin( 'likes.likesLiked_by', 'likedByUser' );

        $q_1 = $q_1->getQuery()->getResult ();

The code was called in loop like below:

foreach( ...  )
{
     $wall_post_obj->getAlbum()
}

But accidentally $em->clear() was placed at the end of the loop.

$em->clear() clears the data from your current object. so $em->refresh() will not work.