/** * <code> * <?php * * $limit = 100; * $offset = max(0, $this->request->getQuery('page', 'int') - 1) * $limit; * * $manufacturers = Manufacturer::with('Robots.Parts', [ * 'limit' => [$limit, $offset] * ]); * * foreach ($manufacturers as $manufacturer) { * foreach ($manufacturer->robots as $robot) { * foreach ($robot->parts as $part) { ... } * } * } * * </code> * * @param mixed ...$arguments * @return Phalcon\Mvc\ModelInterface[] */ public static function with(...$arguments) { if (!empty($arguments)) { $numArgs = count($arguments); $lastArg = $numArgs - 1; $parameters = NULL; if ($numArgs >= 2 && is_array($arguments[$lastArg])) { $parameters = $arguments[$lastArg]; unset($arguments[$lastArg]); if (isset($parameters['columns'])) { throw new \LogicException('Results from database must be full models, do not use `columns` key'); } } } else { throw new \BadMethodCallException(sprintf('%s requires at least one argument', __METHOD__)); } $ret = static::find($parameters); if ($ret->count()) { $ret = Loader::fromResultset($ret, ...$arguments); } return $ret; }
public function testManyEagerLoadsAndConstraintsWithLoaderFromResultset() { $manufacturers = Loader::fromResultset(Manufacturer::find(), ['Robots.Bugs' => function (QueryBuilder $builder) { $builder->where('Bug.id > 10'); }]); $robots = array(); foreach ($manufacturers as $m) { $robots = array_merge($robots, $m->robots); } $this->assertEquals(array_sum(array_map(function ($o) { return count($o->bugs); }, $robots)), 134); }