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'

评论 (写第一个评论)