/** * Construct the bridge while setting the model. * * Extra parameters can be added in subclasses, but the first parameter * must remain the model. * * @param \MUtil_Model_ModelAbstract $model * @param \Zend_Form $form Rquired */ public function __construct(\MUtil_Model_ModelAbstract $model, \Zend_Form $form = null) { $this->model = $model; $this->form = $form; if (!$form instanceof \Zend_Form) { throw new \MUtil_Model_ModelException("No form specified while create a form bridge for model " . $model->getName()); } if (!$form->getName()) { $form->setName($model->getName()); } }
/** * Add an (extra) model to the join * * @param \MUtil_Model_ModelAbstract $subModel * @param array $joinFields * @return \MUtil_Model_Transform_NestedTransformer (continuation pattern) */ public function addModel(\MUtil_Model_ModelAbstract $subModel, array $joinFields) { // \MUtil_Model::$verbose = true; $name = $subModel->getName(); $this->_subModels[$name] = $subModel; $this->_joins[$name] = $joinFields; return $this; }
/** * Checks name for being a key id field and in that case returns the real field name * * @param string $name The field name or key name * @param boolean $throwError By default we throw an error until rendering * @return string The real name and not e.g. the key id * @throws \MUtil_Model_ModelException */ protected function _checkName($name, $throwError = true) { if ($this->model->has($name)) { return $name; } $modelKeys = $this->model->getKeys(); if (isset($modelKeys[$name])) { return $modelKeys[$name]; } if ($throwError) { throw new \MUtil_Model_ModelException(sprintf('Request for unknown item %s from model %s.', $name, $this->model->getName())); } return $name; }
/** * Add an extra model to the union * * @param \MUtil_Model_ModelAbstract $model * @param array $fieldMap Map from the sub model field names to this models names * @param string $name * @return \MUtil_Model_UnionModelAbstract (continuation pattern) */ public function addUnionModel(\MUtil_Model_ModelAbstract $model, array $fieldMap = null, $name = null) { if (null === $name) { $name = $model->getName(); } $this->_unionModels[$name] = $model; if ($fieldMap) { $this->_unionMapsFrom[$name] = $fieldMap; $this->_unionMapsTo[$name] = array_flip($fieldMap); } else { $this->_unionMapsFrom[$name] = false; $this->_unionMapsTo[$name] = false; $fieldMap = array(); } foreach ($model->getItemsOrdered() as $subName) { if (isset($fieldMap[$subName])) { $mainName = $fieldMap[$subName]; } else { $mainName = $subName; } $this->set($mainName, $model->get($subName)); } return $this; }
/** * Add a 'submodel' field to the model. * * You get a nested join where a set of rows is placed in the $name field * of each row of the parent model. * * @param \MUtil_Model_ModelAbstract $model * @param array $joins The join fields for the sub model * @param string $name Optional 'field' name, otherwise model name is used * @return \MUtil_Model_Transform_NestedTransformer The added transformer */ public function addModel(\MUtil_Model_ModelAbstract $model, array $joins, $name = null) { if (null === $name) { $name = $model->getName(); } $trans = new \MUtil_Model_Transform_NestedTransformer(); $trans->addModel($model, $joins); $this->addTransformer($trans); $this->set($name, 'model', $model, 'elementClass', 'FormTable', 'type', \MUtil_Model::TYPE_CHILD_MODEL); return $trans; }
/** * * @param \MUtil_Task_TaskBatch $batch Optional batch with different source etc.. * @return \MUtil_Task_TaskBatch */ public function getImportOnlyBatch(\MUtil_Task_TaskBatch $batch = null) { if (!$this->_importBatch instanceof \MUtil_Task_TaskBatch) { $batch = new \MUtil_Task_TaskBatch(__CLASS__ . '_import_' . basename($this->sourceModel->getName()) . '_' . __FUNCTION__); $this->registrySource->applySource($batch); $batch->setSource($this->registrySource); $batch->setVariable('targetModel', $this->getTargetModel()); $this->_importBatch = $batch; } else { $batch = $this->_importBatch; } $this->_importBatch->getStack()->registerAllowedClass('MUtil_Date'); // \MUtil_Echo::track($this->_importBatch->count()); if (!$batch->isLoaded()) { if ($this->_filename) { $batch->addTask('AddTask', 'File_CopyFileWhenTask', $this->_filename, $this->getSuccessDirectory() . DIRECTORY_SEPARATOR . $this->getLongtermFilename() . '.' . $this->_extension, 'import_errors', 0, 0); } // Rest of loading is done by getCheckOnlyBatch, but when started, the above task must be added. } return $this->_importBatch; }