/** * Attach image (only filename) to Product. * * @helper Model create|attach * @param string $image * @return \Veer\Services\Administration\Elements\Product */ public function attachImage($image) { $new = new \Veer\Models\Image(); $new->img = $image; $new->save(); $new->products()->attach($this->id); return $this; }
public function testAttachAndDetach($detach = true) { $image = Veer\Models\Image::orderBy('id', 'desc')->first(); $file = Veer\Models\Download::orderBy('id', 'desc')->first(); $category_id = (new Veer\Services\Administration\Elements\Category())->add('TestCategory', 99999, null, false, true); $attributes = [['name' => 'TestAttribute' . time(), 'val' => 'TestValue', 'type' => 'descr', 'descr' => 'TestDescription']]; $tagsOne = 'test tag, another tag, third tag, ' . str_random(); $tagsTwo = ['arrayTag1', 'arrayTag2']; $tagsThree = $this->text; $product = (new Veer\Services\Administration\Elements\Product())->add(['title' => 'TestProduct', 'price' => 99.98999999999999])->getEntity(); $pages = Veer\Models\Page::take(3)->orderBy('id')->get(); $this->page->add(['title' => 'TestAttach2', 'url' => ' http://bolshaya.net ', 'txt' => '{{ Small text }} Main Text'])->attach('images', $image->id)->attach('files', $file->id)->attach('categories', $category_id)->attach('attributes', $attributes)->attach('attributes', $attributes[0])->attach('tags', $tagsOne, false, ',')->attach('tags', 'word')->attach('tags', $tagsTwo)->attach('tags', $tagsThree)->attach('products', $product->id)->attach('parentpages', $pages->first()->id)->attach('subpages', $pages->last()->id); $entity = Veer\Models\Page::find($this->page->getId()); $entity->load('images', 'downloads', 'categories', 'attributes', 'tags', 'products', 'parentpages', 'subpages'); $this->assertEquals(1, $entity->images->where('id', $image->id)->count()); $this->assertEquals(1, $entity->downloads->where('elements_id', $entity->id)->count()); $this->assertArrayHasKey($category_id, $entity->categories->lists('title', 'id')->toArray()); // ? $this->assertEquals(1, $entity->attributes->where('name', $attributes[0]['name'])->count()); $this->assertEquals(1, $entity->tags->where('name', 'third tag')->count()); $this->assertEquals(1, $entity->tags->where('name', 'arrayTag2')->count()); $this->assertEquals(1, $entity->tags->where('name', 'word')->count()); $this->assertEquals(1, $entity->tags->where('name', 'Tag on line 3')->count()); $this->assertArrayHasKey($product->id, $entity->products->lists('title', 'id')->toArray()); // ? $this->assertEquals(1, $entity->parentpages->where('id', $pages->first()->id)->count()); $this->assertEquals(1, $entity->subpages->where('id', $pages->last()->id)->count()); if ($detach) { $this->page->detach('images', $image->id, true)->detach('files', $entity->downloads->first()->id, true)->detach('categories', $category_id, true)->detach('attributes', $entity->attributes->lists('id')->toArray(), true)->detach('tags', $entity->tags->lists('id')->toArray(), true)->detach('products', $product->id, true)->detach('parentpages', $pages->first()->id, true)->detach('subpages', $pages->last()->id, true); $entity = Veer\Models\Page::find($this->page->getId()); $entity->load('images', 'downloads', 'categories', 'attributes', 'tags', 'products', 'parentpages', 'subpages'); foreach ($entity->getRelations() as $relation) { $this->assertEquals(0, count($relation)); } } }