I have multilingual project and I use symfony2+doctrine.
If I try to display list of objects in language different than the default one, doctrine generates one query to get all data for all objects and then generates one query for every single object, witch is unnecessary.
The code in the repository code looks like:
$repository = $this->_em->getRepository($this->_entityName);
$keywordController = new \Cms\Bundles\KeywordBundle\Controller\ObjectKeywordController();
$queryBuilder = $repository->createQueryBuilder('e');
$queryBuilder->andWhere('e.status=:status');
$queryBuilder->setParameter('status', 'public');
$queryBuilder->andWhere('e.displayTranslation = :displayTranslation');
$queryBuilder->setParameter('displayTranslation', 'true');
$queryBuilder->orderBy('e.publishedDate', 'DESC');
$query = $queryBuilder->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
if ($locale) {
$query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
}
$paginator = new Paginator($query);
$total_items = count($paginator);
$pages_count = (int) ceil($total_items / $page_row_count);
// now get one page's items:
$paginator
->getQuery()
->setFirstResult($page_row_count * ($current_page - 1)) // set the offset
->setMaxResults($page_row_count); // set the limit
I and get the following queries:
SELECT a0_.id AS id_0, t1_.content AS title_1, t2_.content AS sub_title_2, t3_.content AS announce_3, t4_.content AS author_4, t5_.content AS content_5, t6_.content AS source_6, a0_.creation_date AS creation_date_7, a0_.update_date AS update_date_8, a0_.published_date AS published_date_9, a0_.schedule_disable_date AS schedule_disable_date_10, a0_.schedule_publish_date AS schedule_publish_date_11, t7_.content AS seo_title_12, t8_.content AS seo_description_13, t9_.content AS keywords_as_text_14, t10_.content AS main_pic_description_15, t11_.content AS main_pic_title_16, t12_.content AS slug_17, t13_.content AS status_18, t14_.content AS display_translation_19, a0_.comments_enabled AS comments_enabled_20, t15_.content AS comments_count_21, a0_.main_pic AS main_pic_22, a0_.poll_id AS poll_id_23, a0_.attachment_id AS attachment_id_24, a0_.gallery_id AS gallery_id_25, a0_.created_by AS created_by_26, a0_.main_category_id AS main_category_id_27 FROM articles a0_ LEFT JOIN article_translations t1_ ON t1_.locale = 'bg' AND t1_.field = 'title' AND t1_.object_id = a0_.id LEFT JOIN article_translations t2_ ON t2_.locale = 'bg' AND t2_.field = 'subTitle' AND t2_.object_id = a0_.id LEFT JOIN article_translations t3_ ON t3_.locale = 'bg' AND t3_.field = 'announce' AND t3_.object_id = a0_.id LEFT JOIN article_translations t4_ ON t4_.locale = 'bg' AND t4_.field = 'author' AND t4_.object_id = a0_.id LEFT JOIN article_translations t5_ ON t5_.locale = 'bg' AND t5_.field = 'content' AND t5_.object_id = a0_.id LEFT JOIN article_translations t6_ ON t6_.locale = 'bg' AND t6_.field = 'source' AND t6_.object_id = a0_.id LEFT JOIN article_translations t7_ ON t7_.locale = 'bg' AND t7_.field = 'seoTitle' AND t7_.object_id = a0_.id LEFT JOIN article_translations t8_ ON t8_.locale = 'bg' AND t8_.field = 'seoDescription' AND t8_.object_id = a0_.id LEFT JOIN article_translations t9_ ON t9_.locale = 'bg' AND t9_.field = 'keywordsAsText' AND t9_.object_id = a0_.id LEFT JOIN article_translations t10_ ON t10_.locale = 'bg' AND t10_.field = 'mainPicDescription' AND t10_.object_id = a0_.id LEFT JOIN article_translations t11_ ON t11_.locale = 'bg' AND t11_.field = 'mainPicTitle' AND t11_.object_id = a0_.id LEFT JOIN article_translations t12_ ON t12_.locale = 'bg' AND t12_.field = 'slug' AND t12_.object_id = a0_.id LEFT JOIN article_translations t13_ ON t13_.locale = 'bg' AND t13_.field = 'status' AND t13_.object_id = a0_.id LEFT JOIN article_translations t14_ ON t14_.locale = 'bg' AND t14_.field = 'displayTranslation' AND t14_.object_id = a0_.id LEFT JOIN article_translations t15_ ON t15_.locale = 'bg' AND t15_.field = 'commentsCount' AND t15_.object_id = a0_.id WHERE t13_.content = 'public' AND t14_.content = 'true' AND a0_.id IN ('176448', '176447', '176446', '176444', '176442', '176441', '176440', '176437', '176436', '176435', '176434', '176424') ORDER BY a0_.published_date DESC;
and the for every object a query like:
SELECT t0.id AS id_1, t0.locale AS locale_2, t0.field AS field_3, t0.content AS content_4, t0.object_id AS object_id_5 FROM article_translations t0 WHERE t0.object_id = ?
How to avoid this extra queries and why doctrine don't use the data from the first one?
Note: All extra queries are generated in function postLoad in the TranslatableListener class. There is a parameter $skipOnLoad, which is always false, and it looks like this is somehow the problem, but why this happens I have no idea:)
Related
i want to do this in doctrine symonfy2
update producto p
left join producto_compra pc ON p.id = pc.id_producto
set p.cantidad = (p.cantidad - pc.cantidad)
where pc.id_compra = '56';
and i tried with this, and it doesnt work
$em = $this->getEntityManager();
$query = $em->createQuery('UPDATE ORM\Entity\Producto p '
. 'LEFT JOIN ORM\Entity\ProductoCompra pc WITH p.id = pc.idProducto '
. 'SET p.cantidad = (p.cantidad - pc.cantidad) '
. 'WHERE pc.idCompra = :fc ')
->setParameter('fc', $facturacompra);
similar question look at here, i see that join is not supported in update or delete queries.
I have a query that perform normally with MySQL :
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
I try to transform it in DQL for run it in Symfony :
$query = $this->_em->createQuery('
SELECT u.*,ranking.*
FROM UserBundle:User u
JOIN UserBundle:Ranking ranking
WITH ranking.user_id = u.id
JOIN (
SELECT x.user_id, MAX(x.id) AS default_id
FROM UserBundle:Ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id'
);
$results = $query->getResult();
I have this error :
[Semantical Error] line 0, col 113 near '(SELECT x.user_id,': Error: Class '(' is not defined.
Do you have any idea please ? Thanks!
Use native query
$rsm = new ResultSetMapping();
$sql = "
SELECT *
FROM td_user u
JOIN td_ranking ranking ON ranking.user_id = u.id
JOIN (
SELECT x.user_id,
MAX(x.id) AS default_id
FROM td_ranking x
GROUP BY x.user_id
) y
ON y.user_id = ranking.user_id
AND y.default_id = ranking.id
";
$result = $this->getEntityManager()->createNativeQuery($sql, $rsm)->getResult();
I have used the below query to calculate the onhand quantity from InventTrans table but that onhand does not match with the AvailPhysical and/or PhysicalInvent of InventSum table. I have tried matching with other quantities as well but onhand is not matching. Please guide me what am i missing here
SELECT T1.ITEMID AS ITEMID
,T1.QTY
,T1.INVENTDIMID AS INVENTDIMID
,DATEDIFF(dd, T3.TRANSDATE, getdate()) AS Age
,t19.price as RetailPrice
,t1.CurrencyCode AS CurrencyKey
,T1.DATAAREAID AS DATAAREAID
,T1.PARTITION AS PARTITION
FROM INVENTTRANS T1
LEFT JOIN INVENTTRANSORIGIN T2 ON (
T1.INVENTTRANSORIGIN = T2.RECID
AND (T1.DATAAREAID = T2.DATAAREAID)
AND (T1.PARTITION = T2.PARTITION)
)
INNER JOIN INVENTTRANSPOSTING T3 ON (
(
(
(
T1.VOUCHERPHYSICAL = T3.VOUCHER
AND (T1.DATAAREAID = T3.DATAAREAID)
AND (T1.PARTITION = T3.PARTITION)
)
)
AND (
T1.DATEPHYSICAL = T3.TRANSDATE
AND (T1.DATAAREAID = T3.DATAAREAID)
AND (T1.PARTITION = T3.PARTITION)
)
)
AND (
T1.INVENTTRANSORIGIN = T3.INVENTTRANSORIGIN
AND (T2.DATAAREAID = T3.DATAAREAID)
AND (T2.PARTITION = T3.PARTITION)
)
) and t3.INVENTTRANSPOSTINGTYPE in (0)
LEFT JOIN INVENTDIM t4 ON (
t4.inventdimid = t1.inventdimid
AND t4.PARTITION = t1.PARTITION
AND t4.dataareaid = t1.dataareaid
)
LEFT OUTER JOIN INVENTTABLEMODULE T19 ON T19.ItemID = T1.ItemID AND T19.DataAreaID = T1.DataAreaID and T19.Partition = T1.Partition AND T19.MODULETYPE=0
LEFT OUTER JOIN INVENTTABLE T20 ON T1.ITEMID = T20.ITEMID AND T1.DATAAREAID = T20.DATAAREAID
where T20.ITEMTYPE <> 2
Any help would be greatly appreciated.
I have stuck in this issue from the last two days.
Please help.
I want to assign the following query .
$qr = "( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
to
one view's field value.
I have used the following code.
function general_views_query_alter(&$view, &$query) {
$qr = " ( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
$query->add_where(1, "node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value", $qr);
}
But, it is returning the following where query.
where node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value = '( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = \'node\' AND subconfigcode.deleted = \'0\') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = \'1\') AND (node.type IN (\'offering\')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0) ')
I want to get query without the single quote assigned for sub query and remove extra slashed added to each value.
Please help.
Answer :
Direct query is not applicable for assignment purpose in the add_where condition.
For that, I have generated the query object using.
function general_views_query_alter(&$view, &$query) {
$subQuery = db_select('node', 'node'); $subQuery->leftJoin('field_data_field_select_parent_configuratio', 'select_parent_configuratio ', 'node.nid = select_parent_configuratio .entity_id');
$subQuery->condition('select_parent_configuratio.entity_type', 'node', '=');
$subQuery->condition('select_parent_configuratio.deleted', '0', '=');
$subQuery->leftJoin('node', 'select_parent_configuratio_node', 'select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid');
$subQuery->leftJoin('field_data_field_subconfigcode', 'subconfigcode', 'select_parent_configuratio_node.nid = subconfigcode.entity_id');
$subQuery->condition('subconfigcode.entity_type', 'node', '=');
$subQuery->condition('subconfigcode.deleted', '0', '=');
$subQuery->where("select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid");
$subQuery->condition('node.status', "1", '=');
$subQuery->condition('node.type', array('offering'), 'IN');
$subQuery->orderBy('configcode');
$subQuery->addField('subconfigcode', 'field_subconfigcode_value', 'configcode');
//$subQuery->range(0, 1);
$query->add_where($group,'node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value',$subQuery,'in');
}
Using this, I am able to generate it the query and assignment to value of the variable.
I need to get posts by custom category id, sort by custom field value and with another custom field if this field exists. I think I need to use custom selection query. Look at my query: The problem is that this query returns the same post 5 times... In admin panel I made 20 posts per page. Maybe someone have their own solution ? Thanks.
$wp_query->request = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);
$customSelect = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 10,
'taxonomy' => 'category',
'order' => 'DESC',
'meta_key' => 'your custom field',
'orderby' => 'meta_value'
This is probably way off what your trying to achieve, the only bit I can't get my head round is how to test if meta key exists using a variable or something.
Anyways good luck
Why you use "$wp_query->request", this variable is for retrieving last query result, not to set new one...
just try
$my_custom_sql = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($my_custom_sql, OBJECT);
Try this-
get_results(
"
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = 3
ORDER BY post_date ASC
"
); ?>