I need a simple column for a table.
By example a table "project", with column id, name and year.
If I do:
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id =?',1);
$pro = $q->execute();
json_encode($pro->toArray());
The answer is all column like
{"id":1,"name":"Project name","year":2013}
but I need only one column. I expect:
{"id":1}
It is with DQL because with native SQL work fine.
The ORM is build automaticaly with a Visual Paradigm.
This is because Doctrine hydrate the response with all the object information, so all columns.
You need to use a different hydration method, there are many one, but let's focus on 5 of them:
HYDRATE_RECORD, the default one
HYDRATE_ARRAY
HYDRATE_NONE
HYDRATE_SCALAR
HYDRATE_ARRAY_SHALLOW
You need the HYDRATE_ARRAY_SHALLOW hydration method. Here's why.
HYDRATE_RECORD
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id = ?',1);
$pro = $q->execute(array(), Doctrine_Core::HYDRATE_RECORD);
var_dump(json_encode($pro->toArray()));
This will hydrate the result using object, and also hydrate relations (if you use a leftJoin inside your query). Since it returns object, we need to call toArray() to be able to send a propre json:
[{"id":1,"name":"Project name","year":2013}]"
HYDRATE_ARRAY
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id = ?',1);
$pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
var_dump(json_encode($pro));
This will hydrate result as an array an automatically add the primary key:
[{"id":"1","pro_id":"1"}]"
HYDRATE_NONE
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id = ?',1);
$pro = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
var_dump(json_encode($pro));
This won't hydrate result, and return just values:
[["1"]]"
HYDRATE_SCALAR
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id = ?',1);
$pro = $q->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
var_dump(json_encode($pro));
This will hydrate result from the select but with key index as the column name with the table alias:
[{"a_pro_id":"1"}]"
HYDRATE_ARRAY_SHALLOW
$q = Doctrine_Query::create()
->select('a.pro_id')
->from('fndr_proyecto a')
->where('a.pro_id = ?',1);
$pro = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY_SHALLOW);
var_dump(json_encode($pro));
This will hydrate result from the select but with key index as the column name without the table alias:
"[{"pro_id":"1"}]"
I'm not sure what version of Doctrine j0k was using. It provided some answers, but I did have trouble finding Doctrine_Query and Doctrine_Core classes. I am using Doctrine 2.3.4. The following worked for me.
public static function getAllEventIDs($em) {
return parent::getAllFromColumn('\path\to\Entity\entityName', 'id', $em);
}
public static function getAllFromColumn($tableName, $columnName, $em) {
$q = $em->createQueryBuilder('t')
->select("t.$columnName")
->from($tableName, 't');
$q = $q->getQuery();
$result = $q->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);
return $result;
}
This did however return a array of arrays. ie, the id of the first event was is
$result[0]['id'];
As of Doctrine 2.10, you can use Scalar Column Hydration:
$query = $em->createQuery('SELECT a.id FROM CmsUser u');
$ids = $query->getResult(Query::HYDRATE_SCALAR_COLUMN);
or
$ids = $query->getSingleColumnResult();
and this results in a flat array
[412, 959, 1234]
Related
How to limits results by main entity, not with childrens?
If I do:
$queryBuilder = $em->createQueryBuilder();
$queryBuilder->select('n, c');
$queryBuilder->from('AppBundle:News', 'n');
$queryBuilder->leftJoin('n.comments', 'c');
$queryBuilder->setMaxResults(3);
$results = $queryBuilder->getQuery()->getResult();
And first News has for example 3 comments, then results return me only one record News. If I don't have comments or if I remove leftJoin, then this is working well.
You are using the wrong variable for your results.
You are using $queryBuilder to prepare your statement:
$queryBuilder = $em->createQueryBuilder();
But taking $qb to get your result;
$results = $qb->getQuery()->getResult();
Doctrine can behave unexpectedly with joins, multiple wheres and pagination.
The recommended way is to use the Paginator, and not directly call getResults.
The paginator handles the grouping of the results to the main entity.
Edit: to supply some sample code:
<?php
use Doctrine\ORM\Tools\Pagination\Paginator;
$dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
$query = $entityManager->createQuery($dql)
->setFirstResult(0)
->setMaxResults(100);
$paginator = new Paginator($query, $fetchJoinCollection = true);
$c = count($paginator);
foreach ($paginator as $post) {
echo $post->getHeadline() . "\n";
}
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/pagination.html
I want join the abteilung table which is in another database with other access data.
Here is the right database for abteilung:
$manager = $this->getDoctrine()->getManager('olddb')->getRepository('ChrisOldUserBundle:BpDepartment');
And this is the old query i want to change:
$result = $this->getDoctrine()->getRepository('KfzBuchungBundle:Rent')
->createQueryBuilder('r')
->addSelect('abteilung')
->addSelect('auto')
->join('r.auto','auto')
->join('r.abteilung','abteilung')
->where('r.mieteStart >= :date_from')
->andWhere('r.mieteEnde <= :date_to')
->setParameter('date_from', $date_from)
->setParameter('date_to', $date_to)
->orderBy('r.mieteStart', 'ASC')
->distinct()
->getQuery()->getArrayResult();
I tried with:
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Chris\KfzBuchungBundle\Entity\Rent', 'bp');
$rsm->addEntityResult('Chris\Bundle\OldUserBundle\Entity\BpDepartment', 'bp_dpt');
$rsm->addFieldResult('bp','id','id');
$query = $this->getDoctrine()->getManager()->createNativeQuery('SELECT * FROM bp_department bp_dpt', $rsm);
$result = $query->getResult();
But same shit, i have no idea.
So here is my query:
public function fetchAd($adID){
$row = $this->tableGateway->select(function(Select $select) use ($adID){
$select->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');
$select->where(array('ads.adID' => $adID));
});
return $row->current();
}
So what I'm doing I'm querying the ad table and join the adDetails table in order for me to get the details for a certain AD, the problem is that the entity AD which belongs to the model that I'm doing the query, it doesn't have the columns names(variables) from the adDetails table; so it will only return the columns from the AD table, because the entity doesn't have those fields in the exchangeArray()
I have tried to extend the AD entity to use the AdDetails entity but it returns now to object array but with the fields as null, because it can;t populate them.
So, how should I do this, in order for me to have all the columns available in the model for the tables that will join?
I'm planning to join other tables as well.
Ok I solved the problem, the thing is that it will return an array, and it won't use the ORM style, but it does the job, for now relations are not supported in ZF2 like in ZF1;
use Zend\Db\Sql\Sql,
Zend\Db\Sql\Where;
$sql = new Sql($this->tableGateway->getAdapter());
$select = $sql->select();
$select->from($this->tableGateway->table)
->join('adDetails','adDetails.adID = ads.adID',array('*'),'inner');
$where = new Where();
$where->equalTo('ads.adID', $adID) ;
$select->where($where);
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
return $result->current();
public function fetchJoin()
{
$select = new \Zend\Db\Sql\Select;
$select->from('tablea a');
$select->columns(array('*'));
$select->join('tableb b', "b.id = a.b_id", array('field1'), 'left');
// to display query string remove comment in next line
//echo $select->getSqlString();
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
How do I filter based on a joined table's columns in Propel?
Like:
$results = FooQuery::create()->joinBar()->filterByBarSurname('surname');
You have to use use method as described in the doc:
$results = FooQuery::create()
->useBarQuery()
->filterBySurname('surname')
->endUse()
->find();
// example Query generated for a MySQL database
$query = 'SELECT foo.* from foo
INNER JOIN bar ON foo.BAR_ID = bar.ID
WHERE bar.SURNAME = :p1'; // :p1 => 'surname'
If you have to use join(), I don't think you can use filterByXXX method but the old where:
$results = FooQuery::create()
->join('Foo.Bar')
->where('Bar.surname = ?', 'surname')
->find();
I have a previously writen query in other Doctrine project using Query Builder.
$qb->select('c', 'm')
->from('Chapter c')
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
How can I replicate this in my inherited class from Doctrine_table in symfony?
I am using symfony 1.4
In your ChapterTable class, something like the following:
public function getChapters($max)
{
$qry = self::getInstance()
->createQuery("c")
->leftJoin('c.Book m')
->orderBy('c.chapterno DESC')
->addOrderBy('m.id ASC')
->groupBy('m.id')
->where('c.chapterno = (SELECT MAX(v.chapterno) FROM Chapter v WHERE v.mid = m.id)')
->limit($max);
return $qry->execute();
}
Then you can call this with eg:
$myMaxValue = 25;
$results = Doctrine::getTable("Chapter")->getChapters($myMaxValue);
Here is a nice solution to organize your queries: