"SELECT invester_details.invester_name,invester_details.invester_contact,add_project.project_name,
add_project.project_id
FROM invester_details
JOIN investment ON invester_details.invester_id=investment.invester_id
JOIN add_project ON investment.project_id=add_project.project_id
WHERE CONCAT(invester_name,invester_contact,project_name)
LIKE '%$filtervalues%' AND add_project.project_id LIKE '$filtervalue'";
Related
I have currently a query with a bunch of filters, which makes sense to use the Criteria API, unfortunately I have this query that uses a Join which uses a string value instead of a relationship. This is an example of the query:
SELECT ua.id,
COALESCE(uf.status, f.status) AS status,
r.name,
ua.companyname,
ua.firstname,
ua.lastname,
ua.usergroup,
ua.email,
ua.country,
ua.continent
FROM useraccount ua
JOIN userrole ur on ua.id = ur.userid
JOIN role r on ur.roleid = r.id and r.eventgroupid = 1
JOIN feature f on f.name = 'Locked'
LEFT JOIN userfeature uf on uf.featureid = f.id AND uf.userid = ua.id;
As you can see the problem of the query is that I want to use COALESCE operation to get a UserFeature status if present, if not use the default status from the Feature table.
The feature table is just a simple one with id, name and the status, it is only related to UserFeature and UserFeature at the same time is related to the UserAccount.
As you might guess the CriteriaAPi doesn't allows a Join<> by a regular string value. I have tried to get my mind around to get how can I change the select statement to be more aligned with what CriteriaAPI offers, but I haven't found anything on this.
I'm using PostgreSQL and Hibernate 5.4.32 (by using the spring starter jpa)
In our controller logic, we have to create a few column aliases for a special class of object (Which is a compilation of many objects):
The trouble is, when trying to ransack, ransack will ignore the alias and try to go to the original table.column_name. SQL after ransack:
"SELECT CONCAT_WS('
',users.first_name,users.middle_name,users.last_name) AS conducted_by,
COUNT(NULLIF(is_complete = false,true)) as complete_count,
COUNT(staff_assessments.id) as employee_count,
staff_assessment_groups.name as name,
MIN(staff_assessments.created_at) as created_at,
staff_assessment_groups.id as staff_assessment_group_id,
staff_assessment_groups.effective_date as effective_date,
staff_assessment_groups.effective_date as review_date, 'N/A' as
store_names, true as is_complete, true as is_360_evaluation,
\"staff_assessments\".\"assigner_position_id\" FROM
\"staff_assessments\" INNER JOIN \"positions\" ON \"positions\".\"id\"
= \"staff_assessments\".\"assigner_position_id\" AND \"positions\".\"deleted_at\" IS NULL INNER JOIN \"employees\" ON
\"employees\".\"id\" = \"positions\".\"employee_id\" AND
\"employees\".\"deleted_at\" IS NULL INNER JOIN \"users\" ON
\"users\".\"id\" = \"employees\".\"user_id\" AND
\"users\".\"deleted_at\" IS NULL INNER JOIN
\"staff_assessment_groups\" ON \"staff_assessment_groups\".\"id\" =
\"staff_assessments\".\"staff_assessment_group_id\" INNER JOIN
\"survey_types\" ON \"survey_types\".\"id\" =
\"staff_assessments\".\"survey_type_id\" INNER JOIN
\"survey_type_categories_types\" ON \"survey_types\".\"id\" =
\"survey_type_categories_types\".\"survey_type_id\" WHERE
\"staff_assessments\".\"position_id\" IN (12024,) AND
\"staff_assessments\".\"is_360_evaluation\" = 't' AND
\"survey_type_categories_types\".\"survey_type_category_id\" = 3 AND
\"staff_assessments\".\"conducted_by\" IN ('Bart Simpson') GROUP BY
users.id, \"staff_assessments\".\"assigner_position_id\",
staff_assessment_groups.id ORDER BY
staff_assessment_groups.effective_date DESC LIMIT 20 OFFSET 0"
(Note the bold conducted_by--that was the original ransack search, but it is using staff_assessments.name instead of the aliased 'name' above)
So here is my question--is there way to tell ransack to use the aliased field? Or is there a way to simply create the records as an Active Relation
(Tried putting the objects to an array, but I could no longer ransack it)
I have list with ids
List<int> listOfIds;
And I have a query with cars objects.
I would like to do something like this:
query = from e in query
join b listOfIdson e.b.Id equals b
select e;
but then I get error:
Specified method is not supported.
I know that its possible to do that by Join method which is avaiable on my query, but I have no idea how to do this. I can run following Join methods on my query:
query.Join()
query.JoinGroup()
Thats all
Thanks for help
Could you try with QueryOver ?
var results = session.QueryOver<Car>().AndRestrictionOn(x=> x.id).IsIn(listOfIds)
I made this query in raw SQL and need some help moving to Doctrine:
SELECT
re . *, t.tipo, m.patente, o.nombre, de.revisado
FROM
sdriving_registros_emisores re
LEFT JOIN
sdriving_turno t ON re.idturno = t.idturno
LEFT JOIN
sdriving_maquina_emisor me ON me.idmaquinaemisor = re.maquinaemisorid
LEFT JOIN
sdriving_maquina m ON me.idmaquina = m.idmaquina
LEFT JOIN
sdriving_operador o ON re.idoperador = o.idoperador
LEFT JOIN
sdriving_detalle_emisores de ON de.idregistros = re.idregistros
WHERE
o.idempresa = 1
Also I wish to know what's the best method in perfomance: executing raw query or using Doctrine Query or something else
I need to perform this query:
SELECT * FROM (SELECT * FROM product WHERE car = 'large' ORDER BY onSale DESC) AS product_ordered GROUP BY type
In Symfony2 using the entity manager.
My basic query builder would be :
$query = $em->getRepository('AutomotiveBundle:Car')
->createQueryBuilder('p')
->where('pr.car = ?1')
->andWhere('pr.status = 1')
->orderBy('pr.onSale', 'DESC')
->setParameter(1, $product->getName())
->groupBy('p.type')
->getQuery();
But I cannot work out how to add in a subquery to this.
Ive tried making a separate query and joining it like:
->andWhere($query->expr()->in('pr.car = ?1',$query2->getQuery()));
But I get:
Call to undefined method Doctrine\ORM\Query::expr()
One trick is to build two queries and then use getDQL() to feed the first query into the second query.
For example, this query returns a distinct list of game ids:
$qbGameId = $em->createQueryBuilder();
$qbGameId->addSelect('distinct gameGameId.id');
$qbGameId->from('ZaysoCoreBundle:Event','gameGameId');
$qbGameId->leftJoin('gameGameId.teams','gameTeamGameId');
if ($date1) $qbGameId->andWhere($qbGameId->expr()->gte('gameGameId.date',$date1));
if ($date2) $qbGameId->andWhere($qbGameId->expr()->lte('gameGameId.date',$date2));
Now use the dql to get additional information about the games themselves:
$qbGames = $em->createQueryBuilder();
$qbGames->addSelect('game');
$qbGames->addSelect('gameTeam');
$qbGames->addSelect('team');
$qbGames->addSelect('field');
$qbGames->addSelect('gamePerson');
$qbGames->addSelect('person');
$qbGames->from('ZaysoCoreBundle:Event','game');
$qbGames->leftJoin('game.teams', 'gameTeam');
$qbGames->leftJoin('game.persons', 'gamePerson');
$qbGames->leftJoin('game.field', 'field');
$qbGames->leftJoin('gameTeam.team', 'team');
$qbGames->leftJoin('gamePerson.person', 'person');
// Here is where we feed in the dql
$qbGames->andWhere($qbGames->expr()->in('game.id',$qbGameId->getDQL()));
Kind of a long example but i didn't want to edit out stuff and maybe break it.
You can use DBAL for performing any sql query.
$conn = $this->get('database_connection');//create a connection with your DB
$sql="SELECT * FROM (SELECT * FROM product WHERE car =? ORDER BY onSale DESC) AS product_ordered GROUP BY type"; //Your sql Query
$stmt = $conn->prepare($sql); // Prepare your sql
$stmt->bindValue(1, 'large'); // bind your values ,if you have to bind another value, you need to write $stmt->bindValue(2, 'anothervalue'); but your order is important so on..
$stmt->execute(); //execute your sql
$result=$stmt->fetchAll(); // fetch your result
happy coding