RedBeanPHP\Finder::findMulti PHP Метод

findMulti() публичный Метод

.. JOIN review' this method will return movie and review beans. Example: $stuff = $finder->findMulti('movie,review', ' SELECT movie.*, review.* FROM movie LEFT JOIN review ON review.movie_id = movie.id'); After this operation, $stuff will contain an entry 'movie' containing all movies and an entry named 'review' containing all reviews (all beans). You can also pass bindings. If you want to re-map your beans, so you can use $movie->ownReviewList without having RedBeanPHP executing an SQL query you can use the fourth parameter to define a selection of remapping closures. The remapping argument (optional) should contain an array of arrays. Each array in the remapping array should contain the following entries: array( 'a' => TYPE A 'b' => TYPE B 'matcher' => MATCHING FUNCTION ACCEPTING A, B and ALL BEANS 'do' => OPERATION FUNCTION ACCEPTING A, B, ALL BEANS, ALL REMAPPINGS ) Using this mechanism you can build your own 'preloader' with tiny function snippets (and those can be re-used and shared online of course). Example: array( 'a' => 'movie' //define A as movie 'b' => 'review' //define B as review 'matcher' => function( $a, $b ) { return ( $b->movie_id == $a->id ); //Perform action if review.movie_id equals movie.id } 'do' => function( $a, $b ) { $a->noLoad()->ownReviewList[] = $b; //Add the review to the movie $a->clearHistory(); //optional, act 'as if these beans have been loaded through ownReviewList'. } ) The Query Template parameter is optional as well but can be used to set a different SQL template (sprintf-style) for processing the original query.
public findMulti ( string | array $types, $sql, array $bindings = [], array $remappings = [], string $queryTemplate = ' %s.%s AS %s__%s' ) : array
$types string | array a list of types (either array or comma separated string)
$bindings array optional, bindings for SQL query
$remappings array optional, an array of remapping arrays
$queryTemplate string optional, query template
Результат array
    public function findMulti($types, $sql, $bindings = array(), $remappings = array(), $queryTemplate = ' %s.%s AS %s__%s')
    {
        if (!is_array($types)) {
            $types = explode(',', $types);
        }
        if (!is_array($sql)) {
            $writer = $this->toolbox->getWriter();
            $adapter = $this->toolbox->getDatabaseAdapter();
            //Repair the query, replace book.* with book.id AS book_id etc..
            foreach ($types as $type) {
                $pattern = " {$type}.*";
                if (strpos($sql, $pattern) !== FALSE) {
                    $newSelectorArray = array();
                    $columns = $writer->getColumns($type);
                    foreach ($columns as $column => $definition) {
                        $newSelectorArray[] = sprintf($queryTemplate, $type, $column, $type, $column);
                    }
                    $newSelector = implode(',', $newSelectorArray);
                    $sql = str_replace($pattern, $newSelector, $sql);
                }
            }
            $rows = $adapter->get($sql, $bindings);
        } else {
            $rows = $sql;
        }
        //Gather the bean data from the query results using the prefix
        $wannaBeans = array();
        foreach ($types as $type) {
            $wannaBeans[$type] = array();
            $prefix = "{$type}__";
            foreach ($rows as $rowkey => $row) {
                $wannaBean = array();
                foreach ($row as $cell => $value) {
                    if (strpos($cell, $prefix) === 0) {
                        $property = substr($cell, strlen($prefix));
                        unset($rows[$rowkey][$cell]);
                        $wannaBean[$property] = $value;
                    }
                }
                if (!isset($wannaBean['id'])) {
                    continue;
                }
                if (is_null($wannaBean['id'])) {
                    continue;
                }
                $wannaBeans[$type][$wannaBean['id']] = $wannaBean;
            }
        }
        //Turn the rows into beans
        $beans = array();
        foreach ($wannaBeans as $type => $wannabees) {
            $beans[$type] = $this->redbean->convertToBeans($type, $wannabees);
        }
        //Apply additional re-mappings
        foreach ($remappings as $remapping) {
            $a = $remapping['a'];
            $b = $remapping['b'];
            $matcher = $remapping['matcher'];
            $do = $remapping['do'];
            foreach ($beans[$a] as $bean) {
                foreach ($beans[$b] as $putBean) {
                    if ($matcher($bean, $putBean, $beans)) {
                        $do($bean, $putBean, $beans, $remapping);
                    }
                }
            }
        }
        return $beans;
    }

Usage Example

Пример #1
0
 /**
  * Finds multiple types of beans at once and offers additional
  * remapping functionality. This is a very powerful yet complex function.
  * For details see Finder::findMulti().
  *
  * @see Finder::findMulti()
  *
  * @param array|string $types      a list of bean types to find
  * @param string|array $sqlOrArr   SQL query string or result set array
  * @param array        $bindings   SQL bindings
  * @param array        $remappings an array of remapping arrays containing closures
  *
  * @return array
  */
 public static function findMulti($types, $sql, $bindings = array(), $remappings = array())
 {
     return self::$finder->findMulti($types, $sql, $bindings, $remappings);
 }