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'

评论 (写第一个评论)