Doctrine 2 Add custom field using DQL is possible? - doctrine

I can't solve this problem. I have a very large code for filters using a QueryBuilder instance
$qb = new QueryBuilder();
$qb->select('item)->from->("BundleExample:Item");
$qb->andWhere("item.idProvince = {$idProvinde");
if($price)
$qb->andWhere("price betwenn..");
Ok , there is too much lines.
Now i need to add a virtual column (distance) or overwrite a current field value.
$qb->select('item, COS(..) as distance')
As i understand , i need to create a ResultSetMapping , but i have to re-do all the filtering process and this is very annoying.
Any ideas?
thanks

You don't have to use native queries nor RSM, as Doctrine is capable of returning mixed/hybrid result sets: Pure and Mixed Results.

Related

Forcing Partition Query Using Laravel

Hi I am currently writing a query where I would like to force the partition that the query uses. I know this code is working in MySQL but I don't know how I would write this using laravel.
This is the MySQL code I would use to force the partition
PARTITION (p46)
I have tried writing the following in Laravel but it doesn't seem to be working
->raw('PARTITION (p46)')
I'm not sure if raw is even a method you can use in laravel which is why it probably doesn't work.
I am using this partition enforcement instead of using the below code, it makes my queries much faster.
->where('venue_id', 46)
My table is partition using HASH(venue_id) and I have tried using the MySQL code and that is working perfectly.
Thank you in advance if you have a solution for this, I haven't been able to find anywhere which explains how to do this.
Here is my full laravel query to give you some context:
$bodyVisitors->selectRaw('SUM(visitors_new) AS new, SUM(visitors_total) AS total')
->raw('PARTITION (p' . $venue_filter . ')')
->where('day_epoch', '>=', $body_start)
->where('day_epoch', '<', $body_end)
->get();
The ->raw() is being ignored is there anyway to make this work? I am fine if the answer is no, I will just stop using laravel to write those queries.
I also suffer the partition problem for whole day...
The way I solve is
select the max timestamp and max ID for T1 then left join it to the T2 on T1.timestamp and ID
Hope that my suggestion would give a some help...
I am still writing the eloquent script almost done :'(
Try like this in your Eloquent Model:
$table = $this->getConnection()->getTablePrefix() . $this->getTable();
$table .= ' PARTITION ({your partition})';
$this->newQuery()->from($table)->where()->get();
that will be making a query like this:
select * from table PARTITION (xxxxxx) where
Try adding this scope
{
$query->from($partition);
}
and $partition is a name of your partition. Try $partition = 'p46' in your case

Hibernate 4 : setting a default value if query returns null

I have a use case where i am mapping two tables to the same object.
In this object i have a string called source and I want to be able to set the table name or the database name to this variable.
Any ideas on how to achieve this?
I have thought about iterating over my list and manually setting it but this has the potential to waste a fair chunk of time.
I appreciate this is somewhat of an odd request so this may be the only way but am hoping for a solution that maps the source variable when hibernate is mapping everything else.
if i had understood correctly your issue , then your solution might be the MappedSuperClass , in which you must have an abstract class , which will have the common fields of the two tables and then you will extend that to the two entities you want , which will point to two different tables.
Check this link
You could try to achieve this with Load listener or Interceptors. In the listener/interceptor you can check what the data source is and populate the source field accordingly.
In the end i ended up using a formula to map my variable to a select statement which was sufficient for what i needed.

How to stop Doctrine from returning a primary key for every query

I am kind of annoyed at Doctrine for returning primary keys in each and every query even though I don't want it to. Is there anyway to stop this ? coz I don't really want those damn primary keys along with my doctrine query results.
A query for instance that I have is:
$getAllDatesForUserQuery = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_ARRAY) ;
In this situation, it retrieves all the datenames as it should, but also happily returns the primary key column value. I DON"T WANT IT.
Is it me? or is it Doctrine ?
In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:
$q = $this->createQuery('s')
->select('s.datename')
->where('s.userid = ?',3)
->setHydrationMode(Doctrine::HYDRATE_SINGLE_SCALAR);
You should find that the query will return a simple one-dimensional array containing only the value(s) you wanted.

Nhibernate Update timestamp

Is there a way to do
"UPDATE Item SET start_date = CURRENT_TIMESTAMP" ?
in Nhibernate without using hql/sql.
I am trying to avoid hql/sql because the rest of my code is in criteria. I want to do something like :
var item = session.get<Item>(id)
item.start_date = current_timestamp
There are two ways and sql is correct one.
Either you will
load all entities, change, update and commit, or
write sql query and let dbms handle most of the work
I am trying to avoid hql/sql because the rest of my code is in criteria
That is not a valid argument. Criteria is an API intended for relational search, and it does not support mass updates.
Different tasks, different APIs.
In this case, you can use either HQL or SQL, as the syntax is the same. I recommend the former, because you'll be using your entity/property names instead of table/column ones.

Linq stored procedure with dynamic results

So I'm extremely new to Linq in .Net 3.5 and have a question. I use to use a custom class that would handle the following results from a store procedure:
Set 1: ID Name Age
Set 2: ID Address City
Set 3: ID Product Price
With my custom class, I would have received back from the database a single DataSet with 3 DataTables inside of it with columns based on what was returned from the DB.
My question is how to I achive this with LINQ? I'm going to need to hit the database 1 time and return multiple sets with different types of data in it.
Also, how would I use LINQ to return a dynamic amount of sets depending on the parameters (could get 1 set back, could get N amount back)?
I've looked at this article, but didn't find anything explaining multiple sets (just a single set that could be dynamic or a single scalar value and a single set).
Any articles/comments will help.
Thanks
I believe this is what you're looking for
Linq to SQL Stored Procedures with Multiple Results - IMultipleResults
I'm not very familiar with LINQ myself but here is MSDN's site on LINQ Samples that might be able to help you out.
EDIT: I apologize, I somehow missed the title where you mentioned you wanted help using LINQ with Stored Procedures, my below answer does not address that at all and unfortunately I haven't had the need to use sprocs with LINQ so I'm unsure if my below answer will help.
LINQ to SQL is able hydrate multiple sets of data into a object graph while hitting the database once. However, I don't think LINQ is going to achieve what you ultimately want -- which as far as I can tell is a completely dynamic set of data that is defined outside of the query itself. Perhaps I am misunderstanding the question, maybe it would help if you provide some sample code that your existing application is using?
Here is a quick example of how I could hydrate a anonymous type with a single database call, maybe it will help:
var query = from p in db.Products
select new
{
Product = p,
NumberOfOrders = p.Orders.Count(),
LastOrderDate = p.Orders.OrderByDescending().Take(1).Select(o => o.OrderDate),
Orders = p.Orders
};

Resources