ufk
ufk

Reputation: 32134

how to get result from createNativeQuery?

I'm building a zend framework 2 application with php 5.6

I'm using doctrine2 for the database related code. I created Yaml files for each table in the database.

I'm trying to call a query and return it's result.

I'm using the following code:

  $query=<<<EOS

QUERY...
EOS;

    $objectManager=$this->getObjectManager();

    $rsm = new ResultSetMapping();
    $rsm->addEntityResult( 'MyAlcoholist\Entity\DrinkFlavorInfo','u');
    $rsm->addFieldResult('u','drink_type_name','drinkTypeName');
    $rsm->addFieldResult('u','drink_brand_name','drinkBrandName');
    $rsm->addFieldResult('u','drimk_company_name','drinkCompanyName');
    $rsm->addFieldResult('u','drink_flavor_type_name','drinkFlavorTypeName');

    $query = $objectManager->createNativeQuery($query,$rsm);
    $query->setParameter(1, $id);
    $drinkFlavors = $query->getResult();
    die(var_export($drinkFlavors,1));

I created a class called DrinkFlavorInfo with getters and setters for the variables but since i configured doctrine to work with yaml then it's searching for a yaml file instead. in yaml configuration one of the properties is table_name and there is no table, i'm just trying to create a class that will hold the returned values. how can i do so?

Upvotes: 1

Views: 1587

Answers (1)

ufk
ufk

Reputation: 32134

ok so it appears that this is how i should have defined the columns and execute the query:

$objectManager=$this->getObjectManager();

    $rsm = new ResultSetMapping();
    $rsm->addScalarResult('drink_brand_name','drinkBrandName');
    $rsm->addScalarResult('drink_type_name','drinkTypeName');
    $rsm->addScalarResult('drink_flavor_type_name','drinkFlavorTypeName');
    $rsm->addScalarResult('drink_company_name','drinkCompanyName');
    $query = $objectManager->createNativeQuery($query,$rsm);
    $query->setParameter(1, $id);
    $drinkFlavors = $query->getArrayResult();
    die(var_export($drinkFlavors,1));

this works :)

Upvotes: 1

Related Questions