6.2 加载实体

加载实体

龷龴龱龯龫 execute() 龚龘龗龓龑龎龌龋 ID 龂齿齻齸齴齰齮齬龌龋齪齨齧齣齠齜齮齬龌龋龱龯齻Drupal 7 齴齰 entity_load() 鼺龂龑龎鼹鼶龌龋龱龯龂齿鼵鼱鼮鼬鼩鼦鼢龫 ID 龂齿鼞齻Drupal 8 鼗齮鼶鼺龂鼔鼒龗龑龎鼹鼶龌龋龱龯鼵鼑鼎鼍龂鼌鼊鼈龓 ID 龂齿鼅鼃龗龓鼹鼶 ID鼞鼅鼃鼁黾黻黷黶黲鼹鼶齠齜黱鼶龌龋龫龚龘黯 entity_load_multiple()

黩黨黤龚龘齠齜龌龋黯

类方法(推荐)

黢點黛黚默黕黔黓黏黍黉黈齻黆黄黂龫麾齸麽麺龓 example() 龚龘鼅鼢齴齰 entityManager 龱龯麲麱黲鼹鼶黍麭麬麨龱龯齻黔黓齪齨齴齰齮鼶麬麨龱龯麥麣鼹鼶黍麭龱龯麢麠黍麭龱龯麟麞齻

protected $entityManager;

/**
 * {@inheritdoc}
 */
public function __construct(EntityManagerInterface $entityManager) {
  $this->entityManager = $entityManager;
}

/**
 * {@inheritdoc}
 */
public static function create(ContainerInterface $container) {
  return new static(
    $container->get('entity.manager')
  );
}

/**
 * Return a list of nodes that are published.
 */
protected function example() {
  // We get the node storage object.
  $node_storage = $this->entityManager->getStorage('node');

  // We use the load function to load a single node object.
  $nid = 1;
  $node = $node_storage->load($nid);

  // We load a revision
  $revision_id = 1;
  $node = $node_storage->loadRevision($revision_id);

  // We use the loadMultiple function to load an array of node objects keyed 
  // by node ID.
  $nids = [1,2,3,4];
  $nodes = $node_storage->loadMultiple($nids);

  // We will discuss this in future lessons, but you can get the value of 
  // simple fields using ->get('field_name')->value.
  return [
    '#markup' => $node->get('title')->value,
  ];
}

静态方法(不推荐)

鼑麜麘麗麓麐鼅麌鼌麈麄齴齰麁麀龚龘齻

// Get a node storage object.
$node_storage = \Drupal::entityManager()->getStorage('node');
 
// Load a single node.
$node_storage->load($nid);
// Load multiple nodes.
$node_storage->loadMultiple($nids);

全局方法(不推荐)

鹽鹹龚龘鹵鹳 Drupal 7齻

// Load a single entity.
$node = entity_load('node', $nid);
 
// Load multiple entities.
$nodes = entity_load_multiple('node', $nids);

创建控制器

齪齨鹯齮齬鹭鹬鹨鹤鹢齻齴齰鹞鹝鹚鹘鼅黕 QueryExampleController.php 龫龚龘鹊鹉齠龷龴龱龯麥麣黍麭 ID齻

<?php
/**
 * @file
 * Contains \Drupal\query_example\Controller\QueryExampleController.
 */

namespace Drupal\query_example\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for page example routes.
 */
class QueryExampleController extends ControllerBase {

  protected $entity_query;

  public function __construct(QueryFactory $entity_query) {
    $this->entity_query = $entity_query;
  }

  public static function create(ContainerInterface $container) {
    return new static(
     $container->get('entity.query')
    );
  }

  protected function simpleQuery() {
    $query = $this->entity_query->get('node')
      ->condition('status', 1);
    $nids = $query->execute();
    return array_values($nids);
  }

  public function basicQuery() {
    return [
      '#title' => 'Published Nodes',
      'content' => [
        '#theme' => 'item_list',
        '#items' => $this->simpleQuery()
      ]
    ];
  }

  protected function intermediateQuery() {
    $query = $this->entity_query->get('node')
      ->condition('status', 1)
      ->condition('changed', REQUEST_TIME, '<')
      ->condition('title', 'ipsum lorem', 'CONTAINS')
      ->condition('field_tags.entity.name', 'test');
    $nids = $query->execute();
    return array_values($nids);
  }

  public function conditionalQuery() {
    return [
      '#title' => 'Published Nodes Called "ipsum lorem" That Have a Tag "test"',
      'content' => [
        '#theme' => 'item_list',
        '#items' => $this->intermediateQuery()
      ]
    ];
  }

  protected function advancedQuery() {
    $query = $this->entity_query->get('node')
      ->condition('status', 1)
      ->condition('changed', REQUEST_TIME, '<');
    $group = $query->orConditionGroup()
      ->condition('title', 'ipsum lorem', 'CONTAINS')
      ->condition('field_tags.entity.name', 'test');
    $nids = $query->condition($group)->execute();
    return array_values($nids);
  }

  public function conditionalGroupQuery() {
    return [
      '#title' => 'Published Nodes That Are Called "ipsum lorem" Or Have a Tag "test"',
      'content' => [
        '#theme' => 'item_list',
        '#items' => $this->advancedQuery()
      ]
    ];
  }

鹢鼹鹆鹂鸿鸽麬龫鸼鸺鸸鸶鼅鸴齰鸲黩龚龘黯

query_example.simple:  
  path: 'examples/query_example/simple'
  defaults:
    _controller: '\Drupal\query_example\Controller\QueryExampleController::basicQuery'
  requirements:
    _access: 'TRUE'
query_example.intermediate:
  path: 'examples/query_example/intermediate'
  defaults:
    _controller: '\Drupal\query_example\Controller\QueryExampleController::conditionalQuery'
  requirements:
    _access: 'TRUE'
query_example.advanced:
  path: 'examples/query_example/advanced'
  defaults:
    _controller: '\Drupal\query_example\Controller\QueryExampleController::conditionalGroupQuery'
  requirements:
    _access: 'TRUE'

评论 (写第一个评论)