简体   繁体   中英

Zf2 HydratingResultSet and Multiple Entities

Just was wondering if ZF2's hydrating resultset can hydrate multiple entities. Consider the snippet below:

$sql = new Sql($this->adapter);
    $sqlObject = $sql->select()
        ->from([
            'ART' => 'acl_roles'
        ])
        ->join([
                'ARTT' => 'acl_role_types',
            ],
            'ART.type_id = ARTT.id',
            [
                'ARTT.id'         => 'id',
                'ARTT.identifier' => 'identifier',
                'ARTT.name'       => 'name',
                'ARTT.status'     => 'status',
                'ARTT.dateAdded'  => 'date_added',
            ],
            Select::JOIN_INNER
        )
        ->where([
            'ART.identifier' => $identifier,
        ])
        ->columns([
            'ART.id'          => 'id',
            'ART.type_id'     => 'type_id',
            'ART.identifier'  => 'identifier',
            'ART.name'        => 'name',
            'ART.status'      => 'status',
            'ART.description' => 'description',
            'ART.dateAdded'   => 'date_added',
        ]);

Now if the query was on a single entity, I could do something like:

$stmt = $sql->prepareStatementForSqlObject($sqlObject);
$resultset = $stmt->execute();
if ($resultset instanceof ResultInterface && $resultset->isQueryResult()) {
    $hydratingResultSet = new HydratingResultSet(new ArraySerializable, new EntityClass);
    $hydratingResultSet->initialize($resultset);
    return $hydratingResultSet->current();
}

However in my case I need the hydrating result set to be able to build and return multiple entities (namely AclRoleEntity and AclRoleTypeEntity). Is this something that is possible? If yes how (considering the result set being a flat array of combination of both entities). If no are there better alternatives to achieve this without using Doctrine/Propel?

Thanks

It's totally possible, you're just going to need a configured (possibly custom) Hydrator.

Your hydrator will need to know the logic to inject your parameters into your objects from a flat array, and how to reduce your object models back to a flat array on extraction.

You're probably looking at a few Hydrator Strategies or a hydrator naming strategy and potentially a combination of both.

With the correct hydrator, you can achieve what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM