Ejemplo n.º 1
0
 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));
 }
Ejemplo n.º 2
0
 public function testBelongsToIndex()
 {
     Cache::flush();
     Schema\Builder::getInstance()->migrate(App::collection('auths')->getModel(), array('attributes' => array(array('name' => 'lucky_number_id', 'type' => 'integer', 'unique' => true)), 'relationships' => array('belongs_to' => 'lucky_numbers')));
     $auths_cache = SchemaCache::get('auths');
     $this->assertTrue($auths_cache['attributes'][0]['name'] == 'lucky_number_id');
 }
Ejemplo n.º 3
0
 public function testModifyField()
 {
     Schema\Builder::getInstance()->migrate(App::collection('modify')->getModel(), array('attributes' => array(array('name' => "field", 'type' => "string"))));
     App::collection('modify')->create(array('field' => "5"));
     $data = App::collection('modify')->first()->toArray();
     $this->assertTrue($data['field'] == "5");
     Schema\Builder::getInstance()->migrate(App::collection('modify')->getModel(), array('attributes' => array(array('name' => "field", 'type' => "integer", 'index' => true))));
     $data = App::collection('modify')->first()->toArray();
     $this->assertTrue($data['field'] == 5);
 }
Ejemplo n.º 4
0
 public function testMigrateFields()
 {
     Cache::flush();
     $attributes = array(array('name' => "default_is_string"), array('name' => "string", 'type' => "string"), array('name' => "int", 'type' => "integer"), array('name' => "float", 'type' => "float"), array('name' => "boolean", 'type' => "boolean"));
     // books / authors / contacts
     Schema\Builder::getInstance()->migrate(App::collection('schema')->getModel(), array('attributes' => $attributes));
     $dump = Schema\Builder::getInstance()->dump();
     $this->assertTrue(count($dump['schemas']['attributes']) == 5);
     $this->assertTrue($dump['schemas']['attributes'] == $attributes);
 }
Ejemplo n.º 5
0
 public static function mounted()
 {
     Router::hook('slim.before.router', function () {
         if (SchemaBuilder::hasTable(self::PAGES_COLLECTION)) {
             $page = App::collection(self::PAGES_COLLECTION)->where('slug', Request::path())->first() ?: App::collection(self::PAGES_COLLECTION)->where('name', '404')->first();
             Router::any(Request::path(), 'Hook\\CMS\\Controllers\\PageController:show');
             PageController::$page = $page;
         }
     });
 }
Ejemplo n.º 6
0
 public function upload_schema()
 {
     $schema = Input::get();
     foreach ($schema as $collection => $config) {
         Schema\Builder::getInstance()->migrate(Model\App::collection($collection)->getModel(), $config);
     }
     return array('success' => true);
 }
Ejemplo n.º 7
0
 public function testMigrateAddRelationship()
 {
     Schema\Builder::getInstance()->migrate(App::collection('schema_cache')->getModel(), array('relationships' => array('has_many' => array('contacts'))));
     Schema\Builder::getInstance()->migrate(App::collection('schema_cache')->getModel(), array('relationships' => array('has_many' => array('contacts'))));
     $this->assertTrue(SchemaCache::get('schema_caches') == array('attributes' => array(), 'relationships' => array('has_many' => array('contacts' => array('collection' => 'contacts', 'foreign_key' => 'schema_cach_id', 'primary_key' => '_id'))), 'lock_attributes' => false));
 }
Ejemplo n.º 8
0
 protected function extractRelatedFields(&$attributes)
 {
     $relatedFields = array('beforeSave' => array(), 'afterSave' => array());
     foreach ($attributes as $field => $values) {
         // Model\Collection found, use it's data as array
         if (is_object($values) && method_exists($values, 'toArray')) {
             $values = $values->toArray();
         }
         // nested values found
         if (is_array($values)) {
             // does a relationship with this name exists?
             $relationship = Relationship::getRelation($this, $field);
             if (!is_null($relationship)) {
                 // remove embedded field to insert into it's respective relationship
                 unset($attributes[$field]);
                 if (preg_match('/\\./', $relationship->getForeignKey())) {
                     $relatedFields['afterSave'][$field] = $values;
                 } else {
                     $relatedFields['beforeSave'][$field] = $values;
                 }
             } else {
                 if (Schema\Builder::getInstance()->isSupported()) {
                     // no relationship with field name found.
                     // probably it's a programming issue. throw an exception.
                     throw new BadRequestException("nested objects not supported.");
                 }
             }
         }
     }
     return $relatedFields;
 }
Ejemplo n.º 9
0
 /**
  * Update a record in the database.
  *
  * @param  array $values
  * @return int
  */
 public function update(array $values)
 {
     // migrate the collection, if needed
     Schema\Builder::getInstance()->dynamic($this->query->getModel(), $values);
     $allowed = $this->fireEvent('updating_multiple', array($this, $values));
     if ($allowed === false) {
         return false;
     } elseif (is_array($allowed)) {
         $values = $allowed;
     }
     $result = $this->query->update($values);
     $this->fireEvent('updated_multiple', array($this, $values));
     return $result;
 }