Is it possible to use Xpath or Xquery to query XML type fields in a database using Eloquent ORM ? I find in inconvenient to parse the XML every time and filter my results in memory. I would rather return my results from a query, is there a way to achieve that using Eloquent or I'm stuck with raw database statements?
Thank you
I reviewed the options for selecting items in Eloquent and there is not much good options to select based on XML values as you expect. You can use some wildcards and run calculations on Eloquent to filter needed data but none of these are very efficient.
Related
I have some big query with 8 joins and I want to go over each entity attributes in the query expression result for duplicate those entities.
I've tried to split the result attributes but I'm not sure how to do it.
Thanks
If I understand your question correctly, you may be looking for the OfType linq method?
I am writing a cypher query in neo4j that merge few seperate queries with'UNION' operator. Now, the final result need to be sorted according to a specific column and I also need to be able to use 'LIMIT' to the final result so that I can fetch based on pagination request.
How to achieve that? adding 'ORDER BY' or 'LIMIT' at the end doesn't seems to work. Can't find a way to wrap the result as temporary set(as in sql queries) either. Any suggestions?
Currently it is not possible to add a ORDER BY or SKIP/LIMIT clause to the global set of UNION.
This is a pending feature request, see https://github.com/neo4j/neo4j/issues/2725
Maybe you can share your query and we can find a way to do it without UNION ?
I am quite sure that DQL will be the way to go, but I am wondering if Doctrine, i am using Doctrine 2, has someway to return the row count. I won't be using the rows itself, I just want the count.
I'm new to Doctrine2 but it looks like you can simply do this:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
Source (Using Agregate Functions): http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#dql-select-examples
Allowed aggregate functions: http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#aggregate-functions
Here is another interesting point of view about using aggregated functions in DQL
http://doctrine-orm.readthedocs.org/en/latest/cookbook/aggregate-fields.html
Maybe you would avoid the creation of an specific query to obtain an aggregate value. In this case, aggregate fields are a good alternative.
I am creating a method that can create filter understood by NHibernate (by filter i mean a set of ICriteria object for example) from my abstract filter object.
public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria)
{
// T4 generated function
// lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) expression trees - hard to generate
// Is there a way to generate HQL/Linq query here istead?
}
then i want to do something like
session.CreateCriteria<Entity>().Add(myCriteria.ToNhCriteria())
to filter entities.
The problem is that using Expression. methods (Expression.Or etc) is quite tedious (the method is generated and i have multiple or statements that have to be joined into an expression somehow).
Is there a way to avoid using Expression.Or() and create ICrietrion / ICriteria using LINQ or HQL?
Hey, did you check out this question? It shows going from Linq to NHibernate to a MultiCriteria (and on the way transforms a linq query to an ICriteria)
No that is not possible. Why don't you use linq instead of criteria?
Linq is not the best solution unless you want to do filtering on collection-side not on datbase-side using WHERE clauses.
Ayende suggests that ICriteria API is well suited for dynamic filter creation, the problem i had with multiple ORs has been tackled by using Restrictions.Disjunction()... that simplified a lot
At the time I asked the question I just didn't realize such things exist in NHibernate :)
I thought that the purpose of using Linq2Nibernate was to return IQueryable and build up an expression tree. I am not sure if this is correct or not.
Why would anyone use Linq2Nibernate if they are not going to return IQueryable?
What else would it be used for?
I would love some more input on this topic
Linq For Nhibernate
I'm planning to use NHibernate.Linq to replace my HQL and criteria API queries with LINQ expressions. In other words, I'll generate the query in code (as a LINQ expression) and then pass it to NHibernate.Linq and NHib to convert it into a database query.
FYI there is an alpha version available.
I have planed to start using Linq2Nibernate but haven't got round to i yet.
My reason for wanting to user Linq2Nibernate is the nice syntax when constructing criterions and later querying them out.
Here is a nice simple example.
http://ayende.com/Blog/archive/2007/03/16/Linq-for-NHibernate.aspx
I am using Linq2Nhibernate with my repository pattern that returns IQueryable objects.
As you know, IQueryable is only a query definition - doesn't touch database yet.
Then local requirements are added to the query and finally the object or list is materialized.
Seems to work excellent and prevents unnecessary db queries for partial data at higher abstract layers.
What's Linq2NHibernate? As there are several projects which tried to implement a linq provider for nhibernate but all stopped before reaching completion.
Any linq provider needs to return IQueryable, or better an IEnumerable as that's how linq works. It's convenient to return an IQueryable as you then can re-use existing code to pad additional operators to an already created query (as ctx.Employee which might return IQueryable is already a query)