7

It's likely that when you are loading individual nodes, you are loading the entity type manager, then for each node retrieve the storage, do a query to get the node data, and build the node. With the multiple process, some of the DB queries are likely combined, and the processing will be in a batch rather than having to run through the code multiple times.


7

The created date is stored in the node_field_data table as a UNIX timestamp in a cloumn called created. Here's the code you would use to fetch the nodes that has been created X days ago, using a direct select query. $day = 86400; // One day in seconds. $days = 3; // The amount of days ago to fetch from. $nids = \Drupal::database()->select('...


5

As described in Insert Queries, the object returned from \Drupal::database()->insert() doesn't have an condition() method. If the purpose of the code is creating a new database row if it doesn't exist, or update it if it already exists, the code should use the object returned from \Drupal::database()->merge(). See Merge Queries. For example, the ...


4

Answering my own question based on comments here and my own experimentation. In Drupal 8 the creation of an entity with base fields is simpler than it was in Drupal 7. Here are the steps I followed to create an entity in Drupal 8 with fields in the single table. I'll talk about my situation: a Dog entity. There's some good general docs on Drupal.org about ...


4

You can use 'entity' field query $query = \Drupal::entityQuery('node'); $query->condition('status', 1); $query->condition('type', 'article'); $query->condition('field_some_field.entity:node.title', 'Referenced node title');


4

Logically a truncate and a delete statement are very similar. They work a little different which allows truncate to be faster in many cases. For the specific code in question, it might be a little slower but it should perform the same purpose and the code should work fine. However, if your site lacks the ability to TRUNCATE then it also lacks the ability to ...


3

The entity query is a powerful and convenient tool to query for content and config entities that have certain properties/field values/field references. Yet, it has its limitations. Comparing two fields is not possible using the generic entity query and its condition method. For such a task, you may need to use the more low level dynamic (select) queries ...


3

You can use fetchAllKeyed try with: $connection = \Drupal::database(); $query = $connection->select('node_field_data', 'n'); $query->condition('n.type', 'employee'); $query->condition('n.status', 1); $query->fields('n', ['nid', 'title']); // use fetchAllKeyed $results = $query->execute()->fetchAllKeyed(0,1);


3

You can add external databases in your settings.php file: $databases['external']['default']= array( 'database'=>'', 'username'=>'', 'password'=>'', 'prefix'=>'', 'host'=>'', 'port'=>'3306', 'namespace'=>'Drupal\\Core\\Database\\Driver\\mysql', 'driver'=>'mysql', ); Then, in a custom module you can query for nodes, ...


3

One option is to use the new Devel Entity Updates module, which was built for this exact purpose: In #2976035: Entity type CRUD operations must use the last installed entity type and field storage definitions the ability to run drush entup was removed, see the related change record for more details. This project aims to restore this functionality as a ...


3

There's no single function that will extract the value for you, but field_info_field() will get you an array that contains the data: $info = field_info_field('field_foo'); if (isset($info['columns']['value']['length'])) { // Do something with $info['columns']['value']['length'] } It it's a core text field, value will always be the name of the column you ...


3

Those tables are tables for fields. Whenever you add a field to a content type Drupal creates two tables, one to store the field value and another one to store historical data of that field. Deleting those table will break your site. This just means that you have a lot of fields. Usually, in drupal 7 it is a watchdog table that contributes to the size of ...


3

If you go into your Drupal database you should find a table called 'node_field_data'. In this table there is a column labeled 'created'. That will be your Unix timestamp for when the node was created.


3

For input, saving to the DB, Database API takes care of it, automatically prevents SQL injection when using dynamic query. Since your code is using dynamic query, you're all good. If you were to use static query you have to make sure you use placeholders as the docs mention. For output, to prevent XSS, you'll want to use a template file, and print your ...


2

Here is an example of sortable table with pagination. $header = [ 'label' => [ 'data' => $this->t('ID'), 'specifier' => 'nid', ], 'replies' => [ 'data' => $this->t('Title'), 'specifier' => 'title', ], 'created' => [ 'data' => $this->t('Created'), 'specifier' => 'created', // Set ...


2

Although this question is quite old and no longer relevant for Drupal 8 as drush no longer has entity-updates, I believe people would still be landing on this page with Pending Entity Updates messages in the Status Report. I ran into the same scenario and wanted to address that issue. The message is shown when the definition of the entity in the database ...


2

Looking at the comments in settings.php, you may well be able to do something like this: $databases['default']['default'] = [ 'database' => 'db_name', 'username' => 'root', 'password' => 'root', 'host' => 'localhost', 'port' => '3306', 'driver' => 'mysql', 'prefix' => [ 'default' => 'shared_', 'custom_table' =&...


2

If you can connect to mysql over ssh using another client (like the mysql command line client or something like SequelPro) then Drupal should also work with the same settings. I don't have experience with heidiSQL, but I suggest just copying the settings that worked in heidiSQL into your Drupal settings file and see how it goes. One concern: This might make ...


2

To be able to exploit your result, you should fetch your result with one of the function of the Statement class. If you use the function fetchAll() for example, you will have an array of objects with all your result. $results = $query->execute()->fetchAll(); You can do a function to create your markup with that array. You can also not use a ...


2

Use FieldDefinitionInterface::getDefaultValueLiteral: $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page'); if (isset($definitions['field_example'])) { $default_value = $definitions['field_example']->getDefaultValueLiteral(); }


2

Yes, it is different. A SQL INSERT statement doesn't have WHERE clause, thus the condition() mentioned l method doesn't exist for the insert object. You can add your conditional check in PHP code (e.g. $queryDB[0]->value1 != $value1) just as you've redundantly done for your UPDATE query.


2

Drupal 9 is planned for some time in 2020. No, Doctrine ORM is not in a 9.x-dev branch. As far as implementing, there are no immediate plans for it. D8 has its own custom DBAL, utilizing PHP PDO, and it's own custom ORM via its Entity API. Doctrine has been proposed as a replacement but it's not an easy undertaking so don't expect it any time soon. Pre-...


2

hook_schema() is only used to return a structured array representing your module's database tables. You can't execute a query from it. Well, you physically can, but you shouldn't. Since hook_schema() is used on install, your code to create the View should be in hook_install(), and the code to remove it should be in hook_uninstall()


2

Put it in an update hook again. Put it in the your_module_name.install file. (Make sure it existed already in the module when installed, otherwise put it in an other modules install file) /** * Update- Create your_entity_name entity. */ function your_module_name_update_8002() { //check if the table exists first. If not, then create the entity. if(!...


2

In Drupal 8 bundle fields are configuration. This looks like you still have the field storage configured. In this case export configuration: drush cex remove the field storage: rm field.storage.node.field_example.yml and import configuration again: drush cim


2

Those tables are generated dynamically by the field API, and as such don't have a pre-built DDL. This is essentially because fields can be added through the UI/programmatically after installation. Rather than keeping a monolithic registry of those fields and values, the architectural decision was made to split them out into dynamically-created tables with a ...


2

Assuming you're using the Email field module, the column name is email, rather than value. If that's the case, just change that line to: ->fieldCondition('field_email_address', 'email', $email_address, '=') You may need to make a similar change for the password field, depending on what type it is. The easiest way to check for valid column names is in ...


2

I think you should leave the database structure and focus on the entities concept. This brings you to a simpler approach using entityTypeManager to get nodes and filter them by whatever you want. $query = $this->entityTypeManager()->getStorage('node')->getQuery(); $nids = $query->loadByProperties( [ 'type' => 'content_type', ...


2

The issue here is that PDO returns strings, even for integer columns. Drupal handles this issue by providing the Typed Data API, which provides data typing used within Drupal, rather than PHP-level data typing. In the Drupal 8 paradigm, database queries are generally made to query entity IDs, which are then used to load entities. Data is then retrieved from ...


1

According to this Drupal issue, setting the socket in the UI is just not possible right now: (at least in 8.6.10) https://www.drupal.org/project/drupal/issues/793200 I also could not figure out how to set the socket in sites/default/settings.php. I had to resort to hardcoding my socket in this file: core/lib/Drupal/Core/Database/Driver/mysql/Connection....


Only top voted, non community-wiki answers of a minimum length are eligible