Beispiel #1
0
 public function models(Models $models)
 {
     $key = $this->getRepo()->getPrimaryKey();
     $ids = $models->pluckProperty($key);
     $this->whereIn($key, $ids);
     return $this;
 }
Beispiel #2
0
 /**
  * Get all of the linked models
  *
  * @return Models
  */
 public function getModels()
 {
     $all = new Models();
     foreach ($this->items as $item) {
         $all->addAll($item->getCurrentAndOriginal());
     }
     return $all;
 }
Beispiel #3
0
 public function executeModels(Models $models)
 {
     if ($models->count() == 1) {
         $this->model($models->getFirst());
     } else {
         $this->models($models);
     }
     $this->execute();
 }
Beispiel #4
0
 /**
  * @covers ::executeModels
  */
 public function testExecuteModels()
 {
     $models1 = new Models([new City(), new City()]);
     $models2 = new Models([new City()]);
     $update = $this->getMock('Harp\\Harp\\Query\\Update', ['models', 'model', 'execute'], [City::getRepo()]);
     $update->expects($this->at(0))->method('models')->with($this->identicalTo($models1))->will($this->returnSelf());
     $update->expects($this->at(1))->method('execute');
     $update->expects($this->at(2))->method('model')->with($this->identicalTo($models2->getFirst()))->will($this->returnSelf());
     $update->executeModels($models1);
     $update->executeModels($models2);
 }
Beispiel #5
0
 /**
  * @param  LinkMany       $link
  */
 public function update(LinkMany $link)
 {
     parent::update($link);
     $affected = new Models();
     foreach ($link->getRemoved() as $removed) {
         $affected->addAll($removed->setPathAndUpdateDescendants(''));
     }
     foreach ($link->getAdded() as $added) {
         $affected->addAll($added->setPathAndUpdateDescendants($link->getModel()->getChildrenPath()));
     }
     return $affected;
 }
Beispiel #6
0
 /**
  * @param  Models $models
  * @param  integer $flags
  * @return \Harp\Harp\Find
  */
 public function findModels(Models $models, $flags = null)
 {
     $keys = $models->pluckPropertyUnique($this->getKey());
     return $this->findAllWhereIn($this->getForeignKey(), $keys, $flags)->where($this->getForeignClassKey(), $this->getConfig()->getModelClass());
 }
Beispiel #7
0
 public function insert(LinkMany $link)
 {
     $inserted = new Models();
     if (count($link->getAdded()) > 0) {
         $through = $link->getModel()->getLink($this->through);
         $repo = $this->getThroughRepo();
         foreach ($link->getAdded() as $added) {
             $inserted->add($repo->newModel([$this->getKey() => $link->getModel()->getId(), $this->getForeignKey() => $added->getId()]));
         }
         $through->get()->addAll($inserted);
     }
     return $inserted;
 }
Beispiel #8
0
 /**
  * @return boolean
  */
 public function valid()
 {
     return $this->current->valid();
 }
Beispiel #9
0
 /**
  * Group models by repo, call yield for each repo
  *
  * @param Closure $yield Call for each repo ($repo, $models)
  */
 public function byRepo(Closure $yield)
 {
     $repos = Objects::groupBy($this->models, function (AbstractModel $model) {
         return $model->getRepo()->getRootRepo();
     });
     foreach ($repos as $repo) {
         $models = new Models();
         $models->addObjects($repos->getInfo());
         $yield($repo, $models);
     }
 }
Beispiel #10
0
 /**
  * @return int
  */
 public function count()
 {
     return $this->models->count();
 }
Beispiel #11
0
 /**
  * @covers ::current
  * @covers ::key
  * @covers ::next
  * @covers ::rewind
  * @covers ::valid
  */
 public function testIterator()
 {
     $source = [new City(), new City()];
     $models = new Models($source);
     $key = $models->key();
     foreach ($models as $i => $model) {
         $this->assertSame(current($source), $model);
         next($source);
     }
 }
Beispiel #12
0
 public function hasModels(Models $models)
 {
     return !($models->isEmptyProperty($this->key) or $models->isEmptyProperty($this->classKey));
 }
Beispiel #13
0
 /**
  * If model doesn't exist, return a void model
  *
  * @return AbstractModel
  */
 public function getNext()
 {
     return parent::getNext() ?: $this->getRepo()->newVoidModel();
 }