$shops = $this->em->getRepository('models\Shop')->findAll();
Gives my an array with entities but I need the entity as array.
How do I convert an entity to an array?
Doctrine allows you to specify a hydration mode when executing queries, which let's you change the data type of the results returned. In this case, you need Query::HYDRATE_ARRAY. It does not let you specify this on the default findAll() method, found on the repositories. You will need to write your own DQL for it.
If you need a collection of entites as arrays:
$query = $em->createQuery('SELECT u FROM User u');
$entites = $query->execute(array(), Query::HYDRATE_ARRAY);
// If you don't have parameters in the query, you can use the getResult() shortcut
$query = $em->createQuery('SELECT u FROM User u');
$entities = $query->getResult(Query::HYDRATE_ARRAY);
If you need a single entity as an array, eg. for a specific ID:
$query = $em->createQuery('SELECT u FROM User u WHERE u.id = ?1');
$query->setParameter(1, $id);
$entity = $query->getSingleResult(Query::HYDRATE_ARRAY);
These methods are defined on Query, and AbstractQuery.
I had the same issue.return get_object_vars($this) is not a good solution because it also converts internal doctrine object/properties too.After some research i found this class: EntitySerializer which creates clean array or JSON from your entities and removes unnecessary items.The documentation is located here.For example i used the following code:
$patientProfile = $this->em->getRepository('Entities\Patientprofile')->findOneByuserid('2222222');
$entitySerializer=new Bgy\Doctrine\EntitySerializer($this->em);
$patientProfile=$entitySerializer->toArray($patientProfile);
Related
I am fetching data from the mysql database with doctrine:
$array = $this->em->getRepository(Documents::class)->findAll();
This is the output:
For my case I want to fetch an array directly, so I created a function:
$array = $this->em->getRepository(Documents::class)->getArray();
repository:
public function getArray()
{
return $this->getEntityManager()
->getRepository(Documents::class)
->createQueryBuilder('e')
->select('e')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
The array is created, but some fields are missing:
How can I also fetch pages and products? And I would like my data to be shown as a date +"timestamp": "02.12.2019"
Forgot about core class that will require another setup
Just use getArrayResult() function instead of getResult(). It returns an array of all data
$query = $em->createQuery("SELECT test FROM namespaceTestBundle:Test test");
$tests = $query->getArrayResult();
Query#getResult(): Retrieves a collection of objects. The result is
either a plain collection of objects (pure) or an array where the
objects are nested in the result rows (mixed).
Query#getArrayResult(): Retrieves an array graph (a nested array) that
is largely interchangeable with the object graph generated by
Query#getResult() for read-only purposes.
I Just tested that returns all result of data as an array nested:
Second soluton in other answer will work as well but they works different ways:
Also see this answer https://stackoverflow.com/a/17499629/12232340 And repository
According to this EntityRepository class, findAll don't take multiple arguments.
You need to do a "fetch-join" by adding it to the select:
public function getArray()
{
return $this->getEntityManager()
->getRepository(Documents::class)
->createQueryBuilder('e')
->select('e', 'p')
->leftJoin('e.products', 'p')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
More info: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/dql-doctrine-query-language.html#joins
I have problem with Doctrine query in my Symfony2 project.
That my code:
public function getLender($lender) {
$lender = str_replace("_", " ", $lender);
$this->qb->select('ld, ld.entry, ll.name, ll.logo')
->from('PageMainBundle:LoanDescription', 'ld')
->leftJoin(
'PageMainBundle:loanLender',
'll',
\Doctrine\ORM\Query\Expr\Join::WITH,
'ld.lender = ll.lenderId'
)
->where('ll.name=?1')
->setParameter(1, $lender);
return $this->qb->getQuery()->getResult();
}
When in select section i choose columns it works very well - returns values of columns. unforunelly when I try something like that:
$this->qb->select('ld')
I don't get pure values but sometkhing strange.
How can I get values of all db columns?
This "strange" thing is most probably an LoanDescription collection of object (entity) instances. So to get value of entry field you need to call $entity->getEntry() on this entity object (assuming that you have such method defined in your entity)
OR
You can use getArrayResult instead of getResult and you should get array with valies
I am trying to use native query in doctrine and for now created something really simple:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('ObjectA', 'a');
$rsm->addFieldResult('a', 'id', 'id');
$query = $em->createNativeQuery('SELECT * FROM table a', $rsm);
What I try using this code, I am getting an error that ObjectA is not a valid entity or mapped super class. Which is totally true.
My question is: Is there any way to mad result of a native query to any arbitrary class (not Entity), but still user Doctrine's tools to do it.
Note: I am trying to avoid usage of lower level PDO.
Thank you.
Nothing like that, neither in the doc nor in the source code Doctrine\ORM\Query\ResultSetMapping (and it happens that some features are not documented).
I'd go with using scalar results and mapping the query result back to the object. Something like this:
$rsm = new ResultSetMapping();
$rsm->addScalarResult('a', 'a');
$rsm->addScalarResult('b', 'b');
$query = $em->createNativeQuery('SELECT a, b FROM table LIMIT 1', $rsm);
$result = $query->getSingleResult();
$a = new ObjectA();
$a->setA($result['a']);
// or
$a = new ObjectA($result); // with mapping passed to the constructor
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?
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 :)
I'm working with Entity objects from Doctrine queries and i end up with a very big array, with all information from all entities related. This ends up being a huge data tree... how can i limit this? Avoid listing all data from all relationships?
You can always remove not needed associations (this is a best practice for speeding up Doctrine). Or you can select only fields that you need in your presentation layer (as read-only data):
public function getAll()
{
$qb = $this->createQueryBuilder('u'); // Where are in User custom repository
return $qb
->select(array('u.id', 'u.first', 'u.last'))
->getQuery()
->getResult();
}
If you still need to work with objects (or for complex queries that needs plain SQL) a possibility is filling only needed properties (and eventually, associations/nested collections) of your domain object.
An example, more on native SQL:
public function getAll()
{
$mapping = new \Doctrine\ORM\Query\ResultSetMapping();
$mapping->addEntityResult('Acme\HelloBundle\User', 'e');
$mapping->addFieldResult('e', 'id', 'id');
$mapping->addFieldResult('e', 'first', 'first');
$mapping->addFieldResult('e', 'last', 'last');
$sql = "SELECT id, first, last FROM user ";
$result = $this->_em->createNativeQuery($sql, $mapping)->getResult();
// Or hust return $result itself (array)
return new \Doctrine\Common\Collections\ArrayCollection($result);
}
Of course the disadvance (?) is use of native SQL. I don't believe that ResultSetMapping can be used with DQL.
EDIT: take a look at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/best-practices.html