示例#1
0
文件: Entity.php 项目: blerby/blerby
 public function init($options = array())
 {
     // TODO: dont depend on zend_db_model
     $componentTable = new Blerby_Entity_Model_Component();
     $components = $componentTable->fetchAll($componentTable->getAdapter()->quoteInto("entity_id = ?", $this->id), "id ASC");
     // TODO: move towards a "object set" for iteration and array ops
     $aComponents = array();
     foreach ($components as $component) {
         // TODO: handle this all in the type!
         $found = false;
         foreach ($this->components as $typeComponent) {
             if ((string) $typeComponent->part == (string) $component->part) {
                 $found = $typeComponent;
                 break;
             }
         }
         if (!$found) {
             continue;
         }
         $oComponent = Blerby_Entity_Component::factory($found->uri);
         $oComponent->parent($this);
         // add type config
         $oComponent->fromArray($found->toArray());
         // merge in component data
         $oComponent->fromArray($component->toArray());
         // TODO: move this into the component (functionally complete objects)
         $componentConfigTable = new Blerby_Entity_Model_Component_Config();
         $componentConfig = $componentConfigTable->fetchAll($componentTable->getAdapter()->quoteInto("entity_component_id = ?", $oComponent->id), "id ASC");
         foreach ($componentConfig as $configItem) {
             $oComponent->set($configItem->name, $configItem->value);
         }
         $aComponents[] = $oComponent;
     }
     $this->bind('entity.save', $this);
     $this->set("components", $aComponents);
     parent::init($options);
 }
示例#2
0
 public function update($event, $extra = array())
 {
     // TODO: add some sort of handle->method handling system
     switch ($event->type()) {
         case 'get.type.update.form':
             return $this->meta('name');
             break;
         case 'get.type.update.config':
             ob_start();
             include $this->directory() . '/templates/entity.type.update.phtml';
             return ob_get_clean();
             break;
         case 'get.entity.update':
             ob_start();
             include $this->directory() . '/templates/entity.update.phtml';
             return ob_get_clean();
             break;
         case 'component.save':
             // TODO: Fix this, tight coupling is aweful.  going agile though.
             $model = new Blerby_Entity_Model_Component();
             // collect appropriate rows for config
             $componentTable = new Blerby_Entity_Model_Component();
             $componentTableMeta = $componentTable->info();
             $componentCols = array_keys($componentTableMeta['metadata']);
             // collect type config keys
             $toStore = array();
             foreach ($componentCols as $name) {
                 if ($this->{$name}) {
                     // databases dont like objects, store only strings.
                     $toStore[$name] = (string) $this->{$name};
                 }
             }
             $toStore['entity_id'] = (int) $this->parent()->id;
             // TODO: more rigorous checks
             // insert
             if ($this->get('id', false) === false) {
                 $componentTable->insert($toStore);
                 $this->id = $componentTable->getAdapter()->lastInsertId();
             } else {
                 $model->update($toStore, $model->getAdapter()->quoteInto("id=?", array($this->id)));
             }
             // TODO: Error checking
             foreach ($this->parent()->type->components->toArray() as $template) {
                 if ($template->part == $this->part) {
                     // get all of the configuration for this component that is not set
                     // in the type.
                     $componentConfig = array_diff($this->toArray(), $template->toArray());
                     // TODO: this needs to be reworked. at somepoint in the future, we may really need to map to another entity id
                     //       and this functionality kills it of if the config name is 'entity_id'
                     foreach (array('id', 'entity_id') as $name) {
                         if (isset($componentConfig[$name])) {
                             unset($componentConfig[$name]);
                         }
                     }
                     $componentConfigTable = new Blerby_Entity_Model_Component_Config();
                     // TODO with auto-incrementing fields this is aweful, add indexes to table
                     $where = $componentConfigTable->getAdapter()->quoteInto("entity_component_id = ?", array((int) $this->id));
                     $componentConfigTable->delete($where);
                     // store each piece of config
                     foreach ($componentConfig as $name => $value) {
                         // no need to store extra data
                         if (!isset($componentCols[$name])) {
                             $toStore = array('entity_component_id' => (int) $this->id, 'name' => $name, 'value' => $value);
                             $componentConfigTable->insert($toStore);
                         }
                     }
                 }
             }
     }
     return parent::update($event);
 }