I'm trying to alter a query in a view using mymodule_views_pre_execute and have used devel to find the sql query it is currently using, which is below:
SELECT node.nid AS nid FROM node node LEFT JOIN field_data_field_date
field_data_field_date ON node.nid = field_data_field_date.entity_id AND
(field_data_field_date.entity_type = :views_join_condition_0 AND
field_data_field_date.deleted = :views_join_condition_1)
WHERE ((
(DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > :node_date_filter) )AND
(( (node.status = :db_condition_placeholder_2) )))
LIMIT 10 OFFSET 0
I am then re-doing this using the following:
$query = db_select("node", "n");
$query->addField("n", "nid");
$query->leftJoin("{field_data_field_date}", "{field_data_field_date}",
"n.nid = field_data_field_date.entity_id AND field_data_field_date.entity_type = 'node'
AND field_data_field_date.deleted = '0'");
$query->where("(DATE_FORMAT(field_data_field_date.field_date_value, '%Y-%m-%d\T%H:%i') > NOW())");
$query->where("n.status = '1'");
I've had to replace :views_join_condition_0 with 'node', :views_join_condition_1 with '0' and :node_date_filter to NOW() although i'm not sure if this is the correct way? If I leave :views_join_condition_0, :views_join_condition_1 and :node_date_filter in though it doesn't work?!
Use hook_view_query_alter(&$view, &$query) instead.
Related
Guys I have such a problem.
I know how to write a good select statement but I have no idea how to turn it into a corresponding update.
Im still learning plsql
Here is my select
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok;
now I would like to wrap it up with an update and create something like this
update (
select * --count(*)
from POLISY_OT ot
join polisy p on p.poli_id = ot.ot_poli_id
join sou.rai_skl rs on rs.ot_id = ot.ot_id
where ot_under_promil = 0
and ot_skladka_rok <> ot_skladka_netto_rok
and ot_rodzaj_um = 'OP'
and ot_rodzaj = 'D'
and ot_produkt_id = 17
and p.poli_status in ('AK', 'CZ')
and rs.skl_roczna = ot.ot_skladka_rok)
set ot_skladka_rok = ot_skladka_netto_rok;
First, I really hope you aren't a student asking for homework help. That really bugs me.
On the assumption it's not, it's a little hard to tell exactly which columns belong to which tables, given that you didn't include the table aliases throughout.
I read this as that you wanted to update a column based on the value in another table, restricting the table updates to records that match a third table).
So I think you want something like this:
UPDATE polisy_ot ot
SET ot_skladka_rok =
(SELECT ot_skladka_netto_rok
FROM sou.rai_skl rs
WHERE rs.ot_id = ot.ot_id
AND rs.skl_roczna = ot.ot_skladka_rok)
WHERE ot_under_promil = 0
AND ot_skladka_rok <> ot_skladka_netto_rok
AND ot_rodzaj_um = 'OP'
AND ot_rodzaj = 'D'
AND ot_produkt_id = 17
AND EXISTS (SELECT NULL
FROM polisy p
WHERE p.poli_id = ot.ot_poli_id
AND p.poli_status IN ('AK', 'CZ'))
Good luck,
Stew
I want to use LIMIT 1 clausule on the subquery in SELECT in DQL query...How can I do that?
"SELECT partial p.{productId, productName, count, price, utwTs},
(
SELECT pp.fileName
FROM AdminBundle:ProductPhoto pp
WHERE pp.productId = p.productId
// LIMIT 1 <--- here
) AS photo_name,
u.avatarName
FROM AdminBundle\Entity\Product p
LEFT JOIN AdminBundle\Entity\Users u WITH u.id = p.userId
LEFT JOIN AdminBundle\Entity\Shop sh WITH sh.userId = u.id
LEFT JOIN AdminBundle\Entity\Stand s WITH p.standId = s.standId
WHERE p.active = 1 and p.productId > 0";
How can I do this? I can't use native sql, becuase pagination (knpbundle) won't work.
That's not possible for security reasons ...
see this -> http://www.doctrine-project.org/jira/browse/DDC-885
eventually you can still make you subquery into a query and limit results with ->setMaxResults( XX ) , et use it into your main query.
I'm trying to query the model from my Symfony2 project and I am not able to replace wildcards into my DQL query. Look;
$q2 =
'SELECT
p.codigo,
p.descripcion,
SUM(l.cantidad) as cantidad,
SUM(l.cantidad*l.pvp) as euros
FROM
MGFAppBundle:LineaVenta l
JOIN
MGFAppBundle:Producto p
WITH
l.producto = p.id
JOIN
l.venta v
WITH
l.venta = v.id
WHERE
l.producto IN (:array)
AND
v.farmacia = :farmacia
GROUP BY
p.codigo';
$query2 = $this->em->createQuery($q2)
->setParameter('farmacia', $farmacia)->setParameter('array', $array);
$porFarmacia = $query2->getResult();
// This does not return a single value, when it should return 2 lines.
echo $query2->getSQL();
// Returns:
// SELECT p0_.codigo AS codigo0, p0_.descripcion AS descripcion1, SUM(l1_.cantidad) AS sclr2, SUM(l1_.cantidad * l1_.pvp) AS sclr3 FROM LineaVenta l1_ INNER JOIN Producto p0_ ON (l1_.lineaventas_id = p0_.id) INNER JOIN Venta v2_ ON l1_.venta_id = v2_.id AND (l1_.venta_id = v2_.id) WHERE l1_.lineaventas_id IN (?) AND v2_.farmacia_id = ? GROUP BY p0_.codigo
So, question marks where parameters should be. Any hint? Thanks in advance.
SELECT Inventory_Stock.id, Inventory_Stock.quantity, SUM(InventoryUsage.quantity)
,Inventory_Stock.quantity - SUM(InventoryUsage.quantity) AS Stock
FROM Inventory_Stock LEFT JOIN InventoryUsage ON Inventory_Stock.id = InventoryUsage.InventoryStock_id
WHERE Inventory_Stock.id = 26 OR
Inventory_Stock.id = 27
GROUP BY Inventory_Stock.id
ORDER BY Stock Asc
How can I write the above code in Symfony2, I want to write it as raw query
Also I am using PageFanta for pagination.. so the result from the above query will go to pagination.
Relations are :
Product ( 1 - 1) InventoryStock
InventoryStock ( 1 - n ) InventoryUsage
If it isn't a problem get all records to array. And use ArrayAdapter:
$adapter = new ArrayAdapter($array);
$pagerfanta = new Pagerfanta($adapter);
If the count of the records is too big. Write your own adapter for pagerfanta
I have a problem with a drupal db_select.
Here is my code :
$query = db_select('node', 'n');
$query->addField('n', 'nid', 'nid');
$query->addField('cfs', 'entity_id', 'feature_support_id');
$query->addField('fpffs', 'entity_id', 'parent_feature_support_id');
$query->addField('cfsfc', 'feature_support_compared_target_id', 'feature_support_compared');
$query->addField('fpffsfc', 'feature_support_compared_target_id', 'parent_feature_support_compared');
//Get feature_support of the feature
$query->join('field_data_feature_support_feature', 'cfs', 'n.nid = cfs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'cfsfc', 'cfs.entity_id = cfsfc.entity_id');
//Get parent feature_support through feature
$query->join('field_data_feature_parent_feature', 'fp', 'n.nid = fp.entity_id');
$query->join('field_data_feature_support_feature', 'fpffs', 'fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id');
$query->join('field_data_feature_support_compared', 'fpffsfc', 'fpffs.entity_id = fpffsfc.entity_id');
$query->condition('n.nid', $node_revision->nid, '=');
$query->condition('cfsfc.feature_support_compared_target_id', 'fpffsfc.feature_support_compared_target_id', '=');
$result = $query->execute();
In DB my request should be
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id)
This request work when I try it in phpmyadmin, but instead in mysql log I have
SELECT n.nid AS nid, cfs.entity_id AS feature_support_id, fpffs.entity_id AS parent_feature_support_id, cfsfc.feature_support_compared_target_id AS feature_support_compared, fpffsfc.feature_support_compared_target_id AS parent_feature_support_compared
FROM node n
INNER JOIN field_data_feature_support_feature cfs ON n.nid = cfs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared cfsfc ON cfs.entity_id = cfsfc.entity_id
INNER JOIN field_data_feature_parent_feature fp ON n.nid = fp.entity_id
INNER JOIN field_data_feature_support_feature fpffs ON fp.feature_parent_feature_target_id = fpffs.feature_support_feature_target_id
INNER JOIN field_data_feature_support_compared fpffsfc ON fpffs.entity_id = fpffsfc.entity_id
WHERE (n.nid = '9') AND (cfsfc.feature_support_compared_target_id = 'fpffsfc.feature_support_compared_target_id')
See at the end, in the WHERE, there is single quote around 'fpffsfc.feature_support_compared_target_id' which should not be there.
It's obviously because the second argument of ->condition seems only accept variable. Anyone know how I can make a condition with two db fields with db_select?
Thanks for any help you can bring me.
Use $query->where($snippet, $args = array());
$query->where('cfsfc.feature_support_compared_target_id = fpffsfc.feature_support_compared_target_id');