Example #1
0
 public function envelope(NgModelBase $model)
 {
     /** @type ModelMetaData $modelsMetadata */
     $modelsMetadata = $model->getModelsMetaData();
     $data = array();
     $data['id'] = (int) $model->getId();
     $publics = $model::getPublicFields();
     $fields = $modelsMetadata->getDataTypes($model);
     foreach ($publics as $field) {
         if (!isset($fields[$field])) {
             continue;
         }
         $func = sprintf("get%s", ucfirst($field));
         switch ($fields[$field]) {
             case 0:
                 $data[$field] = (int) $model->{$func}();
                 break;
             default:
                 $data[$field] = $model->{$func}();
         }
     }
     return $data;
 }
Example #2
0
 protected final function hasMany(NgModelBase $model, ModelRelation $relation)
 {
     // check options for alias
     $opts = $relation->getOptions();
     if (!isset($opts["alias"])) {
         return;
     }
     // build needed variable(s)
     $references = $relation->getReferencedFields();
     $modelRelation = $relation->getReferencedModel();
     $query = new Query();
     $query->addCondition(new SimpleCondition($references, Operator::OP_EQUALS, $model->getId()));
     // fetch resultset
     try {
         $handler = new Crud();
         /** @type Resultset $resultSet */
         $resultSet = $handler->read(new $modelRelation(), $query, false);
         unset($handler);
     } catch (CrudException $e) {
         throw new Exception($e->getMessage());
     }
     // check and prepare data.links
     if (!isset($this->data["links"][$references])) {
         $this->data["links"][$references] = array();
     }
     // check and prepare linked
     if (!isset($this->linked[$references])) {
         $this->linked[$references] = array();
     }
     foreach ($resultSet as $ngModel) {
         /** @type NgModelBase $ngModel */
         // check if this model already populated
         if (in_array($ngModel->getId(), $this->hasManyIds)) {
             continue;
         }
         // check if this model already in our data.links
         if (in_array($ngModel->getId(), $this->data["links"][$references])) {
             continue;
         }
         // put relation id on data.links
         $this->data["links"][$references][] = (int) $ngModel->getId();
         // envelope model into relation data
         $relationData = $this->envelope->envelope($ngModel);
         // check if relationData already in our linked
         if (in_array($relationData, $this->linked[$references])) {
             continue;
         }
         // put relation data on our linked
         $this->linked[$references][] = $relationData;
     }
 }