Showing posts with label drupal 8 query. Show all posts
Showing posts with label drupal 8 query. Show all posts

Monday 4 November 2019

How to sort data by array of category ids in drupal 8?

Sometime we have a case of sort data with array of categories So we can't use direct 'orderBy' in drupal 8 query. We have to use 'addExpression' in query. Here we have an example for better clear this article.
Example:
In a way of query:

    $products = db_query("select gfd.entity_id from node__field_category as gfd inner join node_field_data as nfd on nfd.nid=gfd.entity_id ORDER BY FIELD(gfd.field_category_target_id, 675, 676, 677), nfd.title ASC");

    OR
    Other way of Query:


    $products = \Drupal::database()->select('node__field_category', 'gfd');
    $products->join('node_field_data', 'nfd', 'nfd.nid=gfd.entity_id');       
    $products->fields('gfd', array('entity_id'));       
    $products->addExpression('FIELD(gfd.field_category_target_id, 675, 676, 677)', 'order_field');
    $products->orderBy('order_field', 'ASC');   
    $product_ids = $products->execute()->fetchAll();
   
Description: Here we are using '$products->addExpression' for add special type of sorting so we will pass the field alias and field name then category id which you want for sort data and then 'order_field' is some kind of alias and we will pass in this way for ASC or DESC:
$products->orderBy('order_field', 'ASC');
So this is the simple way to sort data by array ASC/DESC in Drupal 8 query.


Note: How to sort data by array of category ids in drupal 8?

Friday 14 September 2018

What is right query structure for drupal 8?

Here we are defining our drupal 8 query structure with easy mode. You can find/search here INSERT, SELECT, UPDATE, DELETE.
We will explain in easy way with an examples, Please have a look..
That is our table `testing_form` structure..

id candidate_name candidate_mail candidate_number candidate_dob
1 John john@gmail.com 1236547890 2014-10-22
2 Mary mary@gmail.com 5698741230 2014-10-23
3 Karen karen@gmail.com 0123654789 2014-10-23

INSERT Query Structure:
According to php we will define INSERT Query as:

$insert_query = "INSERT INTO testing_form (candidate_name, candidate_mail, candidate_number, candidate_dob) VALUES ('sahil', 'sahil@gmail.com', '9874563210', '22-11-1990')";
mysqli_query($connection, $insert_query);

And now in case of drupal 8, our structure will be.

$fields = array(
    'candidate_name'   => 'sahil',
    'candidate_mail'   => 'sahil@gmail.com',
    'candidate_number' => '9874563210' ,
    'candidate_dob'    => '22-11-1990',           
);
db_insert('testing_form')
    ->fields($fields)
    ->execute();
   
Here 'testing_form' is table name and $fields variable is the data for fields of table.


SELECT Query Structure:
According to php we will define SELECT Query as:

$select_query = "SELECT * from testing_form where id = '2' and id != '3'";
mysqli_query($connection, $select_query);

And now in case of drupal 8, our structure will be.

$result = db_select('testing_form', 't')
    ->fields('t')
    ->condition('id', 2, '=')
    ->condition('id', 3, '!=')
    ->execute()
    ->fetchAll();
   
Here 'testing_form' is table name and our condition is all 2nd id candidate come but not come on 3rd id candidate.

We have some other method for SELECT query with connection in drupal 8..

$connection = \Drupal\Core\Database\Database::getConnection();
$results = $connection->query('select candidate_name, candidate_mail, candidate_number, candidate_dob')->fetchAll();


UPDATE Query Structure:
According to php we will define UPDATE Query as:

$update_query = "UPDATE `testing_form` set candidate_name='karen sood' where id='3'";
mysqli_query($connection, $update_query);

And now in case of drupal 8, our structure will be.

$update_query = \Drupal::database()->update('testing_form')
          ->fields(array('candidate_name' => 'karen sood', 'candidate_number' => '1236547890'))
          ->condition('id', '3')         
          ->execute();
   
Here 'testing_form' is table name and our condition is id number `2nd` of candidate data will update.

 
DELETE Query Structure:
According to php we will define DELETE Query as:

$delete_query = "DELETE from `testing_form` where id='3'";
mysqli_query($connection, $delete_query);

And now in case of drupal 8, our structure will be.

$delete_query = \Drupal::database()->delete('testing_form');
      ->condition('id', '3');   
      ->execute();
   
Here 'testing_form' is table name and our condition is id number `3rd` of candidate data will delete.


So these all are INSERT, SELECT, UPDATE, DELETE queries structure for Drupal 8. Next time we will come with `JOINS`, like how to use `joins` in drupal 8 query with many of conditions.



Note: If have any suggestions or issue regarding 'What is right query structure for drupal 8?' then you can ask by comments.  

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...