symfony2 query builder with multiple databases (doctrine) - php

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.

Related

How can I generate this query in doctrine?

How can I generate this query in doctrine or query builder?
SELECT EndDate from helios.fsa_audits order by StartDate desc limit 1;
Any idea or Advice about how to make it?
Seeing your entity here, in your controller you can try this:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder()
->select(array('a'))
->from(FsaAudits::class, 'a')
->orderBy("a.StartDate","DESC")
->setMaxResults(1);
$resultset = $qb->getQuery()->getResult();
if (count($resultset) <= 0) {
$fsaobj = $resultset[0];
echo $fsaobj->getEndDate();
}
See Working with query builder

Convert sql to doctrine orm in aend framework

I have a query which looks like below:
SELECT *
FROM mydb.users
left join mydb.job on
users.id = job.userid;
Now I am using doctrine orm in querying database but I am newbie here.
What I have done so far is below but it doesn't work as expected.
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'c'))
->from('Admin\Entity\User', 'a')
->leftJoin('a.id', 'c');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
you do not need the from clause and array from clause select
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('u');
$qb->select('u', 'j')
->leftJoin('u.jobs', 'j')
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
it is mandatory that your users entity contains a "one to many" jobs for the attribute.
trick to display the SQL output :
var_dump($qb->getQuery()->getSQL());

Symfony Doctrine findBy and then map

Basically I want to execute this mysql query with doctrine:
select distinct user_id from work_hour where project_id = ?;
But I don't know how I can do this with pretty Doctrine code. Is it possible to make it look like the following pseudo code or do I have to use the query builder?:
$project = new Project();
...
$entities = $em->getRepository('AppBundle:WorkHour')
->findByProject($project)->selectUser()->distinct();
Where $entities is an array of User objects
WorkHour and Project have a ManyToOne relation
WorkHour and User have a ManyToOne relation
You'll need to use a QueryBuilder for that, but that would still be quite a "pretty Doctrine code" and would still look quite like your pseudo-code.
Something like this should work:
$queryBuilder = $em->createQueryBuilder();
$query = queryBuilder
->select('u')
->distinct()
->from('AppBundle:User', 'u')
->join('AppBundle:WorkHour', 'wh')
->where('u.workHour = wh')
->andWhere('wh.project = :project')
->getQuery();
$query->setParameter(':project', $project);
$entities = $query->getResult();
public function findByProject($project)
{
$qb = $this->getEntityManager()->createQueryBuilder('User');
$qb
->select('User')
->from('Path\Bundle\Entity\User', 'User')
->join('Path\Bundle\Entity\WorkHour', 'wh',
'WITH', 'User.workHour = wh')
->where('wh.project = :project'))
->distinct()
->setParameter('project', $project)
;
$query = $qb->getQuery();
return $query->getResult();
}
If you have a complicated query you should do it in QueryBuilder, it'll be more efficient.
http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html
If you have complicated queries you shouldn't do it directly into the controller, it shouldn't know this logic, you have to do it in the repository and call it from there

Using ResultSetMapping in Doctrine

I have a query like this:
$query = "select run_record_detail.id from
(select max(id) as run_record_id from run_record where delete_flag=0 group by test_run_id) rr
inner join
run_record_detail
on rr.run_record_id = rr.run_record_id
where delete_flag=0 and test_case_id IN (:ids)";
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addEntityResult('\Test\Entity\RunRecordDetail', 'run_record_detail');
$rsm->addFieldResult('run_record_detail', 'id', 'id');
$query = $this->getEntityManager()->createNativeQuery($query, $rsm);
$query->setParameter('ids', $testCaseIdArray);
$result = $query->getResult();
I am trying to use ResultsetMapping to bind run_record_detail to RunRecordDetail object. I want RunRecordDetail object in the output. But I am getting only its id(as I have selected ). Please help me getting this object.
I am following Doctrine doc http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html

Clone eloquent models for different queries

Sorry for modifying this question (to all those who have answered here). The question was in fact wrong.
$query = User::find(1)->books(); //books() is "return $this->hasMany('Book');"
$query_for_counting_history_books = clone $query;
$query_for_counting_geography_books = clone $query;
$number_of_history_books = $query_for_counting_history_books->where('type', '=', 'history')->count();
$number_of_geography_books = $query_for_counting_geography_books->where('type', '=', 'geography')->count();
$books = $query->paginate(10);
I want $books to be queried as:
SELECT * FROM books WHERE user_id=1 limit 10;
But its output is:
SELECT * FROM books WHERE user_id=1 and type="history" and type="geography"
limit 10;
Am I missing something about Clone here?
What should be done for this solution?
Not sure why your clone() is not working - but try this:
$query = User::where('confirmed', '=', '1');
$query_for_counting_male = $query->replicate();
$query_for_counting_female = $query->replicate();
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_replicate
Since $query is instance of HasMany, So the cloning should be done as:
$query_for_counting_history_books = clone $query->getQuery();
$query_for_counting_geography_books = clone $query->getQuery();
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Relations/HasOneOrMany.html#method_getQuery

Categories