Esempio n. 1
0
 /**
  * Creates an instance of the extending model wrapping the given
  * bean. For use only by models. Beans are never used by the
  * application directly.
  * @param $bean A <a href="http://www.redbeanphp.com/">RedBean</a>
  * bean.
  * @param $modelClassName Pass only when getting it at runtime
  *                        gets the wrong name.
  * @return An instance of the type of the extending model.
  */
 public static function makeModel(RedBean_OODBBean $bean, $modelClassName = null)
 {
     assert('$modelClassName === null || is_string($modelClassName) && $modelClassName != ""');
     if ($modelClassName === null) {
         $modelClassName = get_called_class();
     }
     $modelIdentifier = $modelClassName . strval($bean->id);
     try {
         $model = RedBeanModelsCache::getModel($modelIdentifier);
         $model->constructIncomplete($bean, false);
         return $model;
     } catch (NotFoundException $e) {
         return new $modelClassName(false, $bean);
         //return new $modelClassName(true, $bean, $forceTreatAsCreation); //no need to set defaults here and force creation since it is always false and the bean already exists
     }
 }
 public function testFileContentModelNotBeingCached()
 {
     $fileContent = new FileContent();
     $fileContent->content = str_repeat('a', 1000);
     $this->assertTrue($fileContent->save());
     $modelIdentifier = $fileContent->getModelIdentifier();
     RedBeanModelsCache::cacheModel($fileContent);
     try {
         RedBeanModelsCache::getModel($modelIdentifier);
     } catch (NotFoundException $e) {
         $this->fail('NotFoundException exception is thrown.');
     }
     $prefix = RedBeanModelsCache::getCachePrefix($modelIdentifier, RedBeanModelsCache::$cacheType);
     $cachedData = false;
     if (isset(Yii::app()->cache)) {
         $cachedData = Yii::app()->cache->get($prefix . $modelIdentifier);
     }
     $this->assertFalse($cachedData);
 }
Esempio n. 3
0
 /**
  * Creates an instance of the extending model wrapping the given
  * bean. For use only by models. Beans are never used by the
  * application directly.
  * @param $bean A <a href="http://www.redbeanphp.com/">RedBean</a>
  * bean.
  * @param $modelClassName Pass only when getting it at runtime
  *                        gets the wrong name.
  * @return An instance of the type of the extending model.
  */
 public static function makeModel(RedBean_OODBBean $bean, $modelClassName = null, $forceTreatAsCreation = false)
 {
     assert('$modelClassName === null || is_string($modelClassName) && $modelClassName != ""');
     if ($modelClassName === null) {
         $modelClassName = get_called_class();
     }
     $modelIdentifier = $modelClassName . strval($bean->id);
     try {
         $model = RedBeanModelsCache::getModel($modelIdentifier);
         $model->constructIncomplete($bean, false);
         return $model;
     } catch (NotFoundException $e) {
         return new $modelClassName(true, $bean, $forceTreatAsCreation);
     }
 }
Esempio n. 4
0
 public function testMixedInPersonInUser()
 {
     $user = new User();
     $user->username = '******';
     $user->lastName = 'Dude';
     $this->assertTrue($user->save());
     $this->assertTrue($user->isAttribute('id'));
     // From RedBeanModel.
     $this->assertTrue($user->isAttribute('createdDateTime'));
     // From Item.
     $this->assertTrue($user->isAttribute('firstName'));
     // From Person.
     $this->assertTrue($user->isAttribute('username'));
     // From User.
     $this->assertTrue($user->isRelation('createdByUser'));
     // From Item.
     $this->assertTrue($user->isRelation('rights'));
     // From Permitable.
     $this->assertTrue($user->isRelation('title'));
     // From Person.
     $this->assertTrue($user->isRelation('manager'));
     // From User.
     unset($user);
     $user = User::getByUsername('dude');
     $this->assertTrue($user->isAttribute('id'));
     // From RedBeanModel.
     $this->assertTrue($user->isAttribute('createdDateTime'));
     // From Item.
     $this->assertTrue($user->isAttribute('firstName'));
     // From Person.
     $this->assertTrue($user->isAttribute('username'));
     // From User.
     $this->assertTrue($user->isRelation('createdByUser'));
     // From Item.
     $this->assertTrue($user->isRelation('rights'));
     // From Permitable.
     $this->assertTrue($user->isRelation('title'));
     // From Person.
     $this->assertTrue($user->isRelation('manager'));
     // From User.
     RedBeanModelsCache::cacheModel($user);
     $modelIdentifier = $user->getModelIdentifier();
     unset($user);
     RedBeanModelsCache::forgetAll(true);
     // Forget it at the php level.
     RedBeansCache::forgetAll();
     if (MEMCACHE_ON) {
         $user = RedBeanModelsCache::getModel($modelIdentifier);
         $this->assertTrue($user->isAttribute('id'));
         // From RedBeanModel.
         $this->assertTrue($user->isAttribute('createdDateTime'));
         // From Item.
         $this->assertTrue($user->isAttribute('firstName'));
         // From Person.
         $this->assertTrue($user->isAttribute('username'));
         // From User.
         $this->assertTrue($user->isRelation('createdByUser'));
         // From Item.
         $this->assertTrue($user->isRelation('rights'));
         // From Permitable.
         $this->assertTrue($user->isRelation('title'));
         // From Person.
         $this->assertTrue($user->isRelation('manager'));
         // From User.
     }
 }
Esempio n. 5
0
 public function testForgetAll()
 {
     $a = new A();
     $a->a = 1;
     $a->uniqueRequiredEmail = '*****@*****.**';
     $this->assertTrue($a->save());
     $modelIdentifier = $a->getModelIdentifier();
     $modelFromCache = RedBeanModelsCache::getModel($modelIdentifier);
     $this->assertEquals(1, $modelFromCache->a);
     $this->assertEquals('*****@*****.**', $modelFromCache->uniqueRequiredEmail);
     // Set some GeneralCache, which should stay in cache after cleanup
     GeneralCache::cacheEntry('somethingForTesting', 34);
     $value = GeneralCache::getEntry('somethingForTesting');
     $this->assertEquals(34, $value);
     RedBeanModelsCache::forgetAll();
     try {
         RedBeanModelsCache::getModel($modelIdentifier);
         $this->fail('NotFoundException exception is not thrown.');
     } catch (NotFoundException $e) {
         // Data from generalCache should still be in cache
         $value = GeneralCache::getEntry('somethingForTesting');
         $this->assertEquals(34, $value);
     }
 }