public function setUp() { parent::setUp(); Cache::flush(); // books / authors / contacts Schema\Builder::getInstance()->migrate(App::collection('contacts')->getModel(), array('relationships' => array('belongs_to' => 'author'))); Schema\Builder::getInstance()->migrate(App::collection('authors')->getModel(), array('relationships' => array('has_many' => array('books', 'contacts')))); Schema\Builder::getInstance()->migrate(App::collection('books')->getModel(), array('relationships' => array('belongs_to' => array('author')))); // clear tables before running tests App::collection('authors')->truncate(); App::collection('books')->truncate(); App::collection('contacts')->truncate(); // populate simple data $author = App::collection('authors')->create(array('name' => "Rasmus Lerdorf")); $author->contacts()->create(array('name' => "Kevin Tatroe")); $author->contacts()->create(array('name' => "Peter MacIntyre")); // default create $book = App::collection('books')->create(array('name' => "Programming PHP", 'author_id' => $author->_id)); $this->assertFalse(isset($book['author']), "shouldn't eager load related data by default"); $eager_loaded_book = CollectionDelegator::queryEagerLoadRelations($book, array('author')); $this->assertTrue($eager_loaded_book['author']['name'] == "Rasmus Lerdorf", "should eager load related data"); $book_with_author = App::collection('books')->join("author")->create(array('name' => "Programming PHP", 'author_id' => $author->_id)); $this->assertTrue($book_with_author['author']['name'] == "Rasmus Lerdorf", "should eager load related data"); // teams / matches Schema\Builder::getInstance()->migrate(App::collection('matches')->getModel(), array('relationships' => array('belongs_to' => array(array('house' => array('collection' => 'teams')), array('guest' => array('collection' => 'teams')))))); Schema\Builder::getInstance()->migrate(App::collection('teams')->getModel(), array('relationships' => array('has_many' => 'matches'))); App::collection('teams')->truncate(); App::collection('matches')->truncate(); $brazil = App::collection('teams')->create(array('name' => "Brazil")); $germany = App::collection('teams')->create(array('name' => "Germany")); $argentina = App::collection('teams')->create(array('name' => "Argentina")); $netherlands = App::collection('teams')->create(array('name' => "Netherlands")); App::collection('matches')->create(array('name' => "Brazil vs Germany", 'house_id' => $brazil->_id, 'guest_id' => $germany->_id)); App::collection('matches')->create(array('name' => "Argentina vs Netherlands", 'house_id' => $argentina->_id, 'guest_id' => $netherlands->_id)); }
public function store($name) { $collection = Model\App::collection($name); $method = Input::get('f') ? 'firstOrCreate' : 'create'; $model = call_user_func(array($collection, $method), static::getData()); // TODO: DRY with 'index' method // with - eager load relationships if ($with = Input::get('with')) { return CollectionDelegator::queryEagerLoadRelations($model, $with); } else { return $model; } }
public function store($name) { $collection = Model\App::collection($name); $method = Input::get('f') ? 'firstOrCreate' : 'create_new'; $model = call_user_func(array($collection, $method), static::getData()); if ($model->isModified() && !$model->save()) { throw new ForbiddenException("Can't save '{$model->getName()}'."); } // TODO: DRY with 'index' method // with - eager load relationships if ($with = Input::get('with')) { return CollectionDelegator::queryEagerLoadRelations($model, $with); } else { return $model; } }