RedBeanPHP\Repository::batch PHP Method

batch() public method

important note: Because this method loads beans using the load() function (but faster) it will return empty beans with ID 0 for every bean that could not be located. The resulting beans will have the passed IDs as their keys.
public batch ( string $type, array $ids ) : array
$type string type of beans
$ids array ids to load
return array
    public function batch($type, $ids)
    {
        if (!$ids) {
            return array();
        }
        $collection = array();
        try {
            $rows = $this->writer->queryRecord($type, array('id' => $ids));
        } catch (SQLException $e) {
            $this->handleException($e);
            $rows = FALSE;
        }
        $this->stash[$this->nesting] = array();
        if (!$rows) {
            return array();
        }
        foreach ($rows as $row) {
            $this->stash[$this->nesting][$row['id']] = $row;
        }
        foreach ($ids as $id) {
            $collection[$id] = $this->load($type, $id);
        }
        $this->stash[$this->nesting] = NULL;
        return $collection;
    }

Usage Example

示例#1
0
 /**
  * Returns an array of beans. Pass a type and a series of ids and
  * this method will bring you the corresponding beans.
  *
  * important note: Because this method loads beans using the load()
  * function (but faster) it will return empty beans with ID 0 for
  * every bean that could not be located. The resulting beans will have the
  * passed IDs as their keys.
  *
  * @param string $type type of beans
  * @param array  $ids  ids to load
  *
  * @return array
  */
 public function batch($type, $ids)
 {
     return $this->repository->batch($type, $ids);
 }