public function testSetOnce() { $page = new Page(); $page->title = 'Initial title'; $page->save(); $id = $page->id; // Completely remove current instance EntityPool::getInstance()->remove($page); // Load fresh instance from database $page = Page::findById($id); // Disable update of 'title' attribute $page->getAttribute('title')->setOnce()->setValue('New title'); $this->assertEquals('Initial title', $page->title); $page->populate(['title' => 'Some title']); $this->assertEquals('Initial title', $page->title); // Try populating a null value attribute $page->getAttribute('settings')->setOnce()->setValue([]); $this->assertEquals([], $page->settings->val()); // Try updating an attribute that has a value already assigned to it $page->settings = [1, 2, 3]; $this->assertEquals([], $page->settings->val()); // Enable update of 'title' attribute $page->getAttribute('title')->setOnce(false)->setValue('New title'); $this->assertEquals('New title', $page->title); }
/** * (PHP 5 >= 5.0.0)<br/> * Retrieve an external iterator * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return Traversable An instance of an object implementing <b>Iterator</b> or * <b>Traversable</b> */ public function getIterator() { if ($this->loaded) { return new \ArrayIterator($this->value); } $dbItems = []; foreach ($this->cursor as $data) { $instance = new $this->entityClass(); $instance->populate($data)->setDirty(false); /** * Check if loaded instance is already in the pool and if yes - use the existing object */ if ($itemInPool = EntityPool::getInstance()->get($this->entityClass, $instance->getId()->getValue())) { $dbItems[] = $itemInPool; } else { $dbItems[] = EntityPool::getInstance()->add($instance); } } $this->value = array_merge($dbItems, $this->value); $this->loaded = true; return new \ArrayIterator($this->value); }