1

I am using the following code.

$conn = Database::getConnection();
$conn->insert('testimonial')->fields(array('name' => $form_state->getValue('name')))->execute();

I get the following error:

Class 'Drupal\Core\Database' not found

I am using use Drupal\Core\Database;. What is wrong in my code?

Update: i find using namespaces a problem, like now i want to get row count, if insert was successfull or not. i tried with

$result->allowRowCount = TRUE;
if ($result->rowCount() == 0) { }

and for rowcount i added the name space use Drupal\Core\Database; i get

Cannot use Drupal\Core\Database as Database because the name is already in use in

and if i remove this . i get

Call to undefined method Drupal\Core\Database\Driver\mysql\Connection::rowCount()

How to use namespaces exactly. like i am assuming if i do

use Drupal\Core\Database

then all the classes in that namespace and all the children namespace should get inehrited. Is this right ?

7

Drupal\Core\Database is the namespace of the class, which is not what the use statement requires. It needs the full-qualified class name, which, for that class, is Drupal\Core\Database\Database.

use Drupal\Core\Database\Database;

That is what Drupal core uses, for example in the DbLog.php file.

namespace Drupal\dblog\Logger;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.