2018-02-07 17:20
浏览 74

Doctrine2查询生成器 - 关联数据无法正确保湿

I've got a somewhat complex query I'm writing on an X-Cart project (utilizes doctrine as the ORM). I have to query/pull records from several tables and ensure I'm not getting back irrelevant/empty results in the associated data. Here's what I'm running into:

I have several inner joins that should be only pulling records that have associated data. I've had the query spit out the raw SQL that it generates, and it works 100% when run directly against the MySQL database.

I believe the problem is occurring when hydrating the base models' associated data.

public function getAlbums() {
    // Query Parameters
    $params = $this->params;

    // Base Query
    // TO DO: Associated data is not being hydrated properly by the joins,
    $query = \XLite\Core\Database::getRepo('\XLite\Module\CodeSmith\Albums\Model\Album')
        ->linkInner('album.songs', 'song')
        ->linkInner('song.song_tracks', 'song_track')
        ->linkInner('song_track.album_track', 'track');

    // Init conditions array
    $conditions = [];

    // If school_type is present, check against song's school type
        $conditions[] = $query->expr()->eq('song.school_type', $params['school_type']);

    // If track type is present, check against album_track's track type
        $conditions[] = $query->expr()->eq('track.track_type', "'{$params['track_type']}'");

    // If album name is present, generate OR expression to check against first letter of album name
    // i.e. (first_letter = A OR first_letter = B OR first_letter = C)
        $orX = $query->expr()->orX();
        $name_conditions = array_map(function($letter) use ($query){
            return $query->expr()->eq($query->expr()->substring('', 1, 1), "'$letter'");
        }, explode(',', $params['album_name']));
        $conditions[] = $orX;

        // TO DO

        // TO DO

    // Aggregate conditions array with AND
        $conditions = call_user_func_array(array($query->expr(), 'andx'), $conditions);

    // Fetch Results
    $albums = $query->getQuery()->getResult();

    return $albums;

The associated data is simply coming back with ALL related data. I don't want ALL related entries, only ones that fit the search criteria. Is this an issue with Doctrine or is there something I'm not doing right?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题