Esempio n. 1
0
    public static function __init() {
    /**
	 * The following code will append a library Page model's $_schema and
	 * $validates properites to this Page model. $_schema can never be changed,
	 * only extended. $validates can be changed and extended, but if default
	 * field rules are left out, they will be used from this parent model.
	*/
	$class =  __CLASS__;
	
        /*
        $extended_schema = static::_object()->_schema;
        
	// Loop through and ensure no one forgot to set the form key		
	foreach($extended_schema as $k => $v) {
		$extended_schema[$k] += array('form' => array('position' => 'default'));
	}
	// Append extended schema
	$class::_object()->_schema += $extended_schema;
        */
        
	// Use the library Page model's validation rules combined with the default (but the library gets priority) this way the default rules can be changed, but if left out will still be used (to help make things nicer)
	$class::_object()->validates = static::_object()->validates += $class::_object()->validates;
	// Same for the search schema, the library gets priority, but combine them.
	$class::_object()->search_schema = static::_object()->search_schema += $class::_object()->search_schema;
	
	// Replace any set display name for context
	$class::_object()->display_name = static::_object()->display_name;
	
	// Lock the schema so values that aren't part of it can't be saved to the db.
	self::meta('locked', true);
        
        parent::__init();
    }
Esempio n. 2
0
 public static function __init()
 {
     static::_isBase(__CLASS__, true);
     parent::__init();
     self::_addRelations();
     self::_connectionFilters();
 }
 public static function __init(array $options = array())
 {
     parent::__init($options);
     $self = static::_instance();
     $self->_finders['count'] = function ($self, $params, $chain) use(&$query, &$classes) {
         $db = Connections::get($self::meta('connection'));
         $records = $db->read('SELECT count(*) as count FROM posts', array('return' => 'array'));
         return $records[0]['count'];
     };
     Post::applyFilter('save', function ($self, $params, $chain) {
         $post = $params['record'];
         if (!$post->id) {
             $post->created = date('Y-m-d H:i:s');
         } else {
             $post->modified = date('Y-m-d H:i:s');
         }
         $params['record'] = $post;
         return $chain->next($self, $params, $chain);
     });
     Validator::add('isUniqueTitle', function ($value, $format, $options) {
         $conditions = array('title' => $value);
         // If editing the post, skip the current psot
         if (isset($options['values']['id'])) {
             $conditions[] = 'id != ' . $options['values']['id'];
         }
         // Lookup for posts with same title
         return !Post::find('first', array('conditions' => $conditions));
     });
 }
Esempio n. 4
0
 /**
  * Override _init to ensure MongoDb indexes
  */
 public static function __init()
 {
     parent::__init();
     $collection = static::connection()->connection->{static::meta('source')};
     $collection->ensureIndex(array('index' => 1));
     $collection->ensureIndex(array('app' => 1));
 }
Esempio n. 5
0
 /**
  * Override _init to ensure MongoDb indexes
  */
 public static function __init()
 {
     parent::__init();
     $collection = static::connection()->connection->{static::meta('source')};
     $collection->ensureIndex(array('created' => -1, 'username' => 1));
     $collection->ensureIndex(array('tags' => 1));
 }
Esempio n. 6
0
 /**
  * Override _init to ensure MongoDb indexes
  */
 public static function __init()
 {
     parent::__init();
     $collection = static::connection()->connection->{static::meta('source')};
     $collection->ensureIndex(array('path' => 1), array('unique' => true));
     $collection->ensureIndex(array('repo' => 1), array('unique' => true));
     $collection->ensureIndex(array('title' => 1));
 }
Esempio n. 7
0
	public static function __init() {
		static::_isBase(__CLASS__, true);
		parent::__init();
		$class = get_called_class();

		if (!static::_isBase($class) && $behaviors = static::_object()->_actsAs) {
			Behaviors::apply($class, $behaviors);
		}
	}
Esempio n. 8
0
 public static function __init()
 {
     parent::__init();
     static::applyFilter('create', function ($self, $params, $chain) {
         if (empty($params['data']['id'])) {
             $params['data']['id'] = String::uuid();
         }
         return $chain->next($self, $params, $chain);
     });
 }
Esempio n. 9
0
 public static function __init($options = array())
 {
     $self = static::_instance();
     if (!isset($self->_meta['source'])) {
         $model = get_class($self);
         $tmp = explode('\\', $model);
         $modelName = end($tmp);
         $self->_meta['source'] = \lithium\util\Inflector::tableize($modelName);
     }
     parent::__init($options);
 }
Esempio n. 10
0
 public static function __init()
 {
     parent::__init();
     $class = __CLASS__;
     Validator::add('modelIsSet', function ($value, $format, $options) use($class) {
         if (isset($options['model']) && ($options['model'] = $class)) {
             return true;
         }
         return false;
     });
 }
Esempio n. 11
0
 /**
  * Override _init to ensure MongoDb indexes
  */
 public static function __init()
 {
     parent::__init();
     static::finder('slug', function ($self, $params, $chain) {
         $params['options']['conditions'] = array('slug' => $params['options']['conditions']['_id']);
         $data = $chain->next($self, $params, $chain);
         $data = is_object($data) ? $data->rewind() : $data;
         return $data ?: null;
     });
     $collection = static::connection()->connection->{static::meta('source')};
     $collection->ensureIndex(array('title' => 1));
 }
Esempio n. 12
0
 public static function __init(array $options = array())
 {
     parent::__init($options);
     $self = static::_object();
     #		$self = static::_instance(__CLASS__);
     #var_dump($self);
     $self->_finders['count'] = function ($self, $params, $chain) use(&$query, &$classes) {
         $db = Connections::get($self::meta('connection'));
         $records = $db->read('SELECT count(*) as count FROM posts', array('return' => 'array'));
         return $records[0]['count'];
     };
 }
Esempio n. 13
0
 public static function __init()
 {
     $class = __CLASS__;
     // Note: If any of the following properties are not set in any extended model,
     // they will be picked up from this base model.
     $class::_object()->search_schema = static::_object()->search_schema += $class::_object()->search_schema;
     $class::_object()->url_field = static::_object()->url_field;
     $class::_object()->url_separator = static::_object()->url_separator;
     // Future compatibility.
     if (method_exists('\\lithium\\data\\Model', '__init')) {
         parent::__init();
     }
 }
Esempio n. 14
0
 public function testClassInitialization()
 {
     $expected = MockPost::instances();
     MockPost::__init();
     $this->assertEqual($expected, MockPost::instances());
     Model::__init();
     $this->assertEqual($expected, MockPost::instances());
     $this->assertEqual('mock_posts', \lithium\tests\mocks\data\MockPost::meta('source'));
     MockPost::__init(array('source' => 'post'));
     $this->assertEqual('post', MockPost::meta('source'));
     MockPost::__init(array('source' => false));
     $this->assertIdentical(false, MockPost::meta('source'));
     MockPost::__init(array('source' => null));
     $this->assertIdentical('mock_posts', MockPost::meta('source'));
 }
 public static function __init(array $options = array())
 {
     parent::__init($options);
     $self = static::_instance();
     Comment::applyFilter('save', function ($self, $params, $chain) {
         $comment = $params['record'];
         if (!$comment->id) {
             $comment->created = date('Y-m-d h:i:s');
         } else {
             $comment->modified = date('Y-m-d h:i:s');
         }
         $params['record'] = $comment;
         return $chain->next($self, $params, $chain);
     });
     Validator::add('validName', '/^[A-Za-z0-9\'\\s]+$/');
 }
Esempio n. 16
0
	/**
	 * init
	 * 
	 * init Tree Callback Methods
	 */
	public static function __init() {
		parent::__init();
		
		$class = get_called_class();
		static::$_tree_config[$class] = array_merge(static::$_tree_defaults, static::$_tree_config);
		
		static::applyFilter('save', function($self,$params,$chain){
        	if($self::beforeSave($self,$params)){
	        	$chain->next($self,$params,$chain);
        	}
        });
        
        static::applyFilter('delete', function($self,$params,$chain){
        	if($self::beforeDelete($self,$params)){
	        	$chain->next($self,$params,$chain);
        	}
        });
	}
Esempio n. 17
0
 public static function __init()
 {
     static::_isBase(__CLASS__, true);
     parent::__init();
 }
Esempio n. 18
0
 public static function __init() {
     self::$fields += static::$fields;
     self::$validate += static::$validate;
     
     parent::__init();
 }