public function __construct($fetch = true, $thisMonth = false) { $will_really_fetch = $fetch; parent::__construct('sickleave', $thisMonth ? false : $fetch); if ($will_really_fetch && $thisMonth) { $pdo = \DB::Instance()->pdo; if ($pdo instanceof \PDO) { if ($thisMonth) { $stmt = $pdo->prepare("SELECT sickleave.* FROM sickleave JOIN user ON sickleave.for_id = user.id WHERE year(date) = year(localtime()) AND month(date) = month(localtime()) ORDER BY sickleave.date DESC"); } else { $stmt = $pdo->prepare("SELECT sickleave.* FROM sickleave JOIN user ON sickleave.for_id = user.id WHERE year(date) = year(localtime()) ORDER BY sickleave.date DESC"); } $stmt->execute(); $rows = $stmt->fetchAll(); foreach ($rows as $row) { $sickleave = new Sickleave(); $sickleave->Absorb($row); $this->add($sickleave); } } } }
public function __construct($fetch = true) { parent::__construct('Reports\\MonthlyIndividual', $fetch); }
/** * Loads the relations * * Uses ModelRelations class to load all the relations corresponding to this * model - but without loading the data. * * This method sets lazyloads & count-methods on collections and or models. For * all relation types, it sets the {relation-type}_read method using * collection's and or model's register_loader method. For has_many and HABTM * relations, it also sets the {relation-type}_count method using the * collection's register_counter method. * * Be aware of the fact that any existing relation data will not be overwritten * by the loaded data! * * @return void */ private function load_relations() { if ($this->exists === true && $this->relations_loaded === false) { $relational_data = []; foreach ($this->relations as $relation => $relation_type) { $config = ['model' => to_model($relation), 'local' => $this->table(), 'foreign' => to_table($relation), 'id' => $this->data['id'], 'type' => $relation_type]; $config['local_id'] = singularize($config['local']) . '_id'; $config['foreign_id'] = singularize($config['foreign']) . '_id'; $config['join'] = implode('_', s($config['local'], $config['foreign'])); if ($relation_type == 'belongs_to') { if (isset($this->data[$config['foreign_id']])) { $config['id'] = $this->data[$config['foreign_id']]; } else { $config['id'] = null; } } if ($relation_type == 'has_many' || $relation_type == 'has_and_belongs_to_many') { $output = new ModelCollection(); $output->register_counter(function ($internal) use($config) { $connection = ModelConnection::instance(); $method = $config['type'] . '_count'; $result = $connection->{$method}($config); return $result; }); } else { $output = new $config['model'](); } $output->relation_type($relation_type); $output->register_loader(function ($internal) use($config) { $connection = ModelConnection::instance(); $method = $config['type'] . '_read'; $result = $connection->{$method}($config); if ($config['type'] == 'has_many' || $config['type'] == 'has_and_belongs_to_many') { foreach ($result as $object) { $internal->add(new $config['model']($object)); } } else { $internal->set_data($result); } }); $relational_data[$relation] = $output; } $this->relational_data = array_merge($relational_data, $this->relational_data); $this->relations_loaded = true; } elseif ($this->exists === false && $this->relations_loaded === false) { foreach ($this->relations as $relation => $relation_type) { if ($relation_type == 'has_many' || $relation_type == 'has_and_belongs_to_many') { $this->relational_data[$relation] = new ModelCollection(); $this->relational_data[$relation]->relation_type($relation_type); } else { $model = to_model($relation); $this->relational_data[$relation] = new $model(); $this->relational_data[$relation]->relation_type($relation_type); } } $this->relations_loaded = true; } }
/** * Reads a from the table * * @param string $table * @param string $model * @param string $condition * @param string $order * @return mixed */ public function read($table, $model, $condition = '', $order = '', $limit = '') { $query = 'select * from ' . $table; if ($condition !== '') { $query .= ' where ' . $condition; } if ($order !== '') { $query .= ' order by ' . $order; } if ($limit !== '') { $query .= ' limit ' . $limit; } $query .= ';'; $result = $this->query($query, PDO::FETCH_ASSOC); if ($result->rowCount() == 1) { $result = new $model($result->fetch()); } elseif ($result->rowCount() > 1) { $collection = new ModelCollection(); foreach ($result as $row) { $collection->add(new $model($row)); } $result = $collection; } else { return false; } return $result; }
private function _get_virtual($n) { $sourcefield = $n . "_id"; if (array_key_exists($n, $this->_virtuals)) { #error_log("returning previously set value for virtual $n"); return array(true, $this->_virtuals[$n]); } # first, find out if this table references another table # through an *_id column # user_id => user # postedby_user_id => user # matches a single row, returns one object #error_log("checking for $sourcefield"); if (array_key_exists($sourcefield, $this->_fields)) { #error_log("$sourcefield found"); if (preg_match(self::$RETABLES, $sourcefield, $m)) { #lib::el($m); list($junk, $virtual, $junk, $foreigntab) = $m; # $virtual should equal $n at this point if ($foreigntab != get_class($this)) { # can't reference ourself, there would be confusion # over the virtual field names and single-vs-multiple # values with the forward and back references $fi = self::_modelinfo($foreigntab); if ($fi) { #error_log("joining ".get_class($this).".$sourcefield to $foreigntab"); # populate the virutals list for the model # not the best place to do this self::$MODELINFO[$foreigntab]['virtuals'][$foreigntab] = true; # TEMP, for testing $v = self::_find_first($foreigntab, array($this->{$sourcefield})); $this->_virtuals[$n] = $v; return array(true, $v); } } } } $ti = self::_modelinfo($this); $oi = self::_modelinfo($n); #error_log("looking for intermediary table"); if ($oi && count($ti['primarykey']) == 1) { # now, try to find an intermedary table that joins us to another table # it's going to be named either $n_$this or $this_$n # and it will contain a reference to this table # and a reference to the model named $n # returns an array of objects, could be a many-to-many mapping $o = array(sprintf('%s_%s', $ti['model'], $n), sprintf('%s_%s', $n, $ti['model'])); foreach ($o as $thrutbname) { $thruti = self::_modelinfo($thrutbname); if (!$thruti) { continue; } error_log("found {$thrutbname}"); $refme = sprintf('%s_id', $ti['model']); $refother = sprintf('%s_id', $oi['model']); if (isset($thruti['columns'][$refme]) && isset($thruti['columns'][$refother])) { # oi thruti oi x thruti x thruti x #select a.* from image a left join product_image b on a.id = b.image_id where b.product_id = 10; $q = sprintf('select a.* from %s a left join %s b on a.%s = b.%s where b.%s = ?:id', $oi['tableQ'], $thruti['tableQ'], $oi['primarykey']['id'], $refother, $refme); error_log($q); $v = self::_find($n, array(array($q, array('id' => $this->id)))); $x = new ModelCollection($n); $x->merge($v); $this->_virtuals[$n] = $x; return array(true, $x); } } } # otherwise, the other table may reference this one by having a column # that matches *_THISMODEL_id # returns an array #if (preg_match(self::$RETABLES, $ci['Field'], $m)) return array(false, false); #$ti = self::_modelinfo($n); #if (!$ti) return array(false, false); /* # resolve a virtual column $ti = self::_modelinfo($this); if (!$ti) return NULL; if (isset($ti['virtuals'][$n])) { if (!isset($this->_virtuals[$n])) { error_log("getting value for $n"); $fkmodel = $ti['virtuals'][$n]['fkmodel']; $sourcefield = $ti['virtuals'][$n]['sourcefield']; $this->_virtuals[$n] = self::_find($fkmodel, array($this->$sourcefield)); } return $this->_virtuals[$n]; } */ }
public function __construct($fetch = true) { parent::__construct('Reports\\MonthlyDepartment', $fetch); }
/** * @return ModelCollection */ public function all() { $collection = new ModelCollection(); foreach ($this->allAsArray() as $row) { $collection->push($this->repository->create($this->fromDatabaseFilter($row))); } return $collection; }
/** * Renvoie une collection d'objet Models, à partir d'une requête en BDD. * * @param string $sSQL Requête SQL de type SELECT. * @return \Aouka\ORM\ModelCollection */ public static function getAll($sSQL) { $oEmptyInstance = new static(); $aResults = $oEmptyInstance->getDB()->getArray($sSQL); $oCollection = new ModelCollection(self::UPDATE); if ($aResults) { foreach ($aResults as $aResult) { $oCollection->add(static::init($aResult)); } $oCollection->end(); } return $oCollection; }
public function __construct($fetch = true) { parent::__construct('Reports\\MonthlyOverall', $fetch); }
/** * Create a new Metadata Collection * * @param array $data Collection Data */ public function __construct(array $data) { $processedItems = $this->processItems($data); parent::__construct($processedItems); }
public function __construct($fetch = true) { parent::__construct('user', $fetch); }
/** * Find all relatives of $this Model according to the given arguments. * * In most cases this method will return a ModelCollection instance. * However, in some cases it will return an indexed array, depending on which * arguments have been omitted/included. * * For example, if you do not specify a $modelName then an array of several * ModelCollections may be returned, indexed by the model names of all related * models. * * Also, if you do not specify a relationship reference when finding relatives * in a recursive relationship then you can expect to be given an array of * ModelCollections indexed by each relationship reference. * * If given, the second argument is either a ModelCriteria instance to filter * the results, or a relationship reference. * * @param string $modelName Find relatives of this Model type * @param ModelCriteria|string ** see notes above ** * @param string $relationRef Relationship reference * @return ModelCollection|array */ public function findRelatives($modelName = null, $criteriaOrRelationRef = null, $relationRef = null) { // No specific model name has been specified so we need to build a // collection of Models from ALL relationships with $this Model. // In this case the returned value is an array indexed by model name. If // multiple relationship references exist between $this and $modelName (such // as child/parent in recursive relationships) then each array element is // further broken down into an array indexed by the relationship reference. if ($modelName === null) { $collection = []; $relations = ModelRelation::getRelation($this->modelName); /** @var ModelRelation $relation */ foreach ($relations as $relation) { $tm = $relation->getTargetModel(); $rr = $relation->getReference(); if (isset($collection[$tm])) { if (isset($collection[$tm][$rr])) { $collection[$tm][$rr]->append($this->findRelatives($tm, $rr)); } else { $collection[$tm][$rr] = $this->findRelatives($tm, $rr); } } else { $collection[$tm] = $this->findRelatives($tm, $rr); } } return $collection; } // A specific model name has been specified. // First of all, determine if a relationship reference has been specified // and use that throughout, otherwise get all possible relations and // recursively call $this->findRelatives() for each one. if (is_string($criteriaOrRelationRef)) { $criteria = null; $relationRef = $criteriaOrRelationRef; } else { if ($criteriaOrRelationRef instanceof \Buan\ModelCriteria) { $criteria = $criteriaOrRelationRef; } else { $criteria = null; } } $relation = ModelRelation::getRelation($this->modelName, $modelName, $relationRef); // If multiple relations are available between $this and $modelName, and the // caller hasn't specified which of these relationships to use (ie. // $relationRef===NULL) then assume we're dealing with a recursive // relationship and return an array of ModelCollections for each // relationship, indexed by the relationship reference. if (is_array($relation)) { if (isset($relation[ModelRelation::REF_DEFAULT]) && count($relation) == 1) { $relation = $relation[ModelRelation::REF_DEFAULT]; } else { $collection = []; foreach ($relation as $ref => $rel) { $collection[$ref] = $this->findRelatives($modelName, $criteria, $ref); } return $collection; } } // So by now we should be dealing with a specific model name, some criteria // (if defined) and a specific relationship reference. So let's do some // loading and retrieving from the db with respect to the cardinality of the // relationship. switch ($relation->getCardinality()) { /* No relationship */ case ModelRelation::NONE: break; /* 1:1 */ /* 1:1 */ case ModelRelation::ONE_TO_ONE: // Such relations are held as 1:M,1 relations internally so it'll be // handled by the 1:M case below. break; /* 1:M */ /* 1:M */ case ModelRelation::ONE_TO_MANY: // First, gather all the in-memory relatives attached to $this model. // If $criteria has been specified then we also check that these models // satisfy those criteria. $cd = ModelRelation::ONE_TO_MANY; $tm = $relation->getTargetModel(); $rr = $relation->getReference(); if (isset($this->relatives[$cd][$tm][$rr])) { $collection = new ModelCollection($this->relatives[$cd][$tm][$rr]); if ($criteria !== null) { // TODO: ::applyTo() isn't implemented yet! so this will create an empty collection every time $collection = $criteria->applyTo($collection); } } else { $collection = new ModelCollection(); } // If $this model is in the db then query the db to find all matching // relatives and append them to the collection. // TODO: Need to handle composite or NULL PKs if ($this->isInDatabase()) { // With no specific criteria to follow we'll load ALL relatives from // persistent storage $targetModelName = $relation->getTargetModel(); $targetModel = Model::create($targetModelName); if ($criteria === null) { $pkv = $this->{$relation->getNativeKey()}; //$this->getPrimaryKeyValue(); $c = new ModelCriteria(); $c->addClause(ModelCriteria::EQUALS, "`{$targetModel->getDbTableName()}`." . $targetModel->getForeignKey($this, $relation->getReference()), $pkv); try { $collection->append(ModelManager::select($targetModelName, $c)); } catch (Exception $e) { } } else { $pkv = $this->{$relation->getNativeKey()}; //$this->getPrimaryKeyValue(); $criteria->addClause(ModelCriteria::EQUALS, "`{$targetModel->getDbTableName()}`." . $targetModel->getForeignKey($this, $relation->getReference()), $pkv); $collection->append(ModelManager::select($targetModelName, $criteria)); } } // For recursive 1:M relationships we now need to load Models on the // M:1 side of the relationship in order to establish a link between // the loaded instance on the 1 and M sides, so don't break and instead // pass through to the next switch case ... if ($relation->isRecursive()) { $relation = $relation->getInverseRelation(); $loadingTheInverse = true; } else { return $collection; break; } /* M:1 */ /* M:1 */ case ModelRelation::MANY_TO_ONE: // First, check if an in-memory model exists on the 1-side of this // relationship $cd = ModelRelation::MANY_TO_ONE; $tm = $relation->getTargetModel(); $rr = $relation->getReference(); $col = new ModelCollection(); if (isset($this->relatives[$cd][$tm][$rr])) { $col = new ModelCollection($this->relatives[$cd][$tm][$rr]); } else { $fk = $this->getForeignKey($tm, $rr); //if($this->{$fk}!==NULL && $this->{$fk}!==0 && $this->{$fk}!=='0') { if (!empty($this->{$fk})) { // If the foreign key does not match the target model's primary key // do some manual labour if ($relation->getForeignKey() !== Model::create($tm)->getPrimaryKey()) { $criteria = $criteria === null ? new ModelCriteria() : $criteria; $target = Model::create($tm); $criteria->addClause(ModelCriteria::EQUALS, $target->getDbTableName() . '.' . $relation->getForeignKey(), $this->{$fk}); $col = ModelManager::select($tm, $criteria); } else { $target = Model::create($tm); $criteria = $criteria === null ? new ModelCriteria() : $criteria; $criteria->addClause(ModelCriteria::EQUALS, $target->getDbTableName() . '.' . $target->getPrimaryKey(), $this->{$fk}); $col = ModelManager::select($tm, $criteria); if (!$col->isEmpty()) { $target = $col->get(0); /*$target = Model::create($tm); $mmTarget = ModelManager::create($tm); $target->setPrimaryKeyValue($this->{$fk}); if($mmTarget->load($target)) { $col = new ModelCollection($target);*/ // If the relationship is recursive, then remember we can only // add recursive related Models via 1:M relationships, so we have // to reverse the way we add a related Model here. if ($relation->isRecursive()) { $ir = $relation->getInverseRelation(); $cd = $ir->getCardinality(); $target->relatives[$cd][$tm][$ir->getReference()][] = $this; } else { $this->relatives[$cd][$tm][$rr] = $target; } } } } } // If this switch case has been arrived at by the 1:M case not breaking // then return the 1:M collection, other return the M:1 collection return empty($loadingTheInverse) ? $col : $collection; break; /* M:M */ /* M:M */ case ModelRelation::MANY_TO_MANY: /*// First, find any in-memory instances of the target model (via linking- // model instances) $linkRelation = ModelRelation::getRelation($this->modelName, $relation->getLinkModel(), $relation->getReference()); $scd = ModelRelation::ONE_TO_MANY; $stm = $linkRelation->getTargetModel(); $srr = $linkRelation->getReference(); $collection = new ModelCollection(); if(isset($this->relatives[$scd][$stm][$srr])) { foreach($this->relatives[$scd][$stm][$srr] as $link) { $tcd = ModelRelation::MANY_TO_ONE; $ttm = $relation->getTargetModel(); $trr = $linkRelation->getInverseRelation()->getReference(); if(isset($link->relatives[$tcd][$ttm][$trr])) { $collection->append(new ModelCollection($link->relatives[$tcd][$ttm][$trr])); } } } // TODO: Look in DB for more links*/ // First, find all instances of the linking model that are related to // $this model $links = $this->findRelatives($relation->getLinkModel(), $relation->getReference()); // Now find all instances of the target model that are related to each // of the linking models we just found $collection = new ModelCollection(); $r = $relation->isRecursive() ? $relation->getInverseRelation() : $relation; foreach ($links as $l) { $crt = $criteria !== null ? clone $criteria : null; $collection->append($l->findRelatives($r->getTargetModel(), $crt, $r->getReference())); } // Result return $collection; break; /* THE FOLLOWING HAS JUST BEEN COPIED OVER FROM OLD MODEL CLASS, SO GO THROUGH IT AND PICK OUT WHAT'S NEEDED AS NECESSARY */ // Find any linking models in the db $c = new ModelCriteria(); $c->addClause(ModelCriteria::EQUALS, $linkModel->getDbTableName() . '.' . $linkModel->getForeignKey($this), $this->getPrimaryKeyValue()); $c->addClause(ModelCriteria::EQUALS, $linkModel->getDbTableName() . '.' . $linkModel->getForeignKey($model), $model->getPrimaryKeyValue()); $collection = ModelManager::select($linkModel->modelName, $c); if (!$links->isEmpty()) { // A link already exists so don't do anything return; } // First of all we need to load all instances of the Model that links // the source Model type to the target Model type in this relationship. // The $criteria is ignored at this point, because it (should) applies // to the target model, NOT the linking model. $linkModelName = $relation->getLinkModel(); $linkForeignKey = Model::create($linkModelName)->getForeignKey($this, $relation->getReference()); $linkDbTableName = Model::create($linkModelName)->getDbTableName(); $c = new ModelCriteria(); $c->addClause(ModelCriteria::EQUALS, $linkDbTableName . '.' . $linkForeignKey, $this->getPrimaryKeyValue()); $linkModels = ModelManager::select($linkModelName, $c); // TODO: WHAT IS THIS BIT FOR? COMMENTS JAMES, USEFUL COMMENTS!! // Move all Models from $this->relatedModels that match entries // in $linkModels into $linkModels. foreach ($linkModels as $k => $lModel) { $relatedModel = $this->getRelatedModelByInstance($lModel); if (!is_null($relatedModel)) { $linkModels[$k] = $relatedModel; } } // Get all instances of target model // If the relationship is recursive then we need to get // models on the reverse side of the relationship, ie. // REF_PARENT/REF_CHILD. $ref = $relation->isRecursive() ? $relation->getInverseRelation()->getReference() : $relation->getReference(); $tModelName = $relation->getTargetModel(); $tModelManager = ModelManager::create($tModelName); $lForeignKey = Model::create($linkModelName)->getForeignKey($tModelName, $ref); foreach ($linkModels as $linkModel) { $tModel = Model::create($tModelName); $tModel->setPrimaryKeyValue($linkModel->{$lForeignKey}); if ($tModelManager->load($tModel)) { // TODO: Check that $tModel is not already loaded into $linkModel, and $linkModel is not already loaded into $this $linkModel->addRelatives($tModel, $ref); $this->addRelatives($linkModel, $rel->getReference()); } unset($tModel); } // Now retrieve if ($relation->isRecursive()) { $allLinkModels = $this->findRelatives($relation->getLinkModel()); foreach ($allLinkModels as $ref => $linkModels) { foreach ($linkModels as $linkModel) { $rModels = $linkModel->findRelatives($modelName); unset($rModels[$relationRef]); foreach ($rModels as $rModel) { if ($rModel !== $this) { $collection->append($rModel); } } } } } else { $linkModelName = $relation->getLinkModel(); if (isset($this->relatives[ModelRelation::ONE_TO_MANY][$linkModelName][$relation->getReference()])) { $linkModels = $this->relatives[ModelRelation::ONE_TO_MANY][$linkModelName][$relation->getReference()]; foreach ($linkModels as $linkModel) { $collection->append($linkModel->findRelatives($modelName, $relation->getReference())); } } } break; /* Unknown case */ /* Unknown case */ default: break; } }
private function _run_virtual_conditions($vi) { switch ($vi['type']) { case MODELForeignRefFindByPrimaryKey: #error_log("doing by pk for ".var_export($vi, true)."<br/>"); $cond = $this->_Manipulation_real_columns[$vi['sourcecolumn']]; $value = self::_find_first($vi['model'], array($cond)); break; case MODELForeignRefFindByQuery: #error_log("doing by query for ".var_export($vi, true)); $searchval = $this->_Manipulation_real_columns[$vi['sourcecolumn']]; $cond = array($vi['query'], array('id' => $searchval)); $value = self::_find($vi['model'], array($cond)); $c = new ModelCollection($vi['model']); $c->merge($value); $value = $c; break; case MODELForeignRefFindByRelation: #error_log("doing by relation for ".var_export($vi, true)."<br />"); $searchcol = $vi['foreigncolumn']; $searchval = $this->_Manipulation_real_columns[$vi['sourcecolumn']]; $cond = array($searchcol => $searchval); $value = self::_find($vi['model'], array($cond)); $c = new ModelCollection($vi['model']); $c->merge($value); $value = $c; break; case MODELForeignRefFindByRelationSingle: #error_log("doing by single relation for ".var_export($vi, true)."<br />"); $searchcol = $vi['foreigncolumn']; $searchval = $this->_Manipulation_real_columns[$vi['sourcecolumn']]; $cond = array($searchcol => $searchval); $value = self::_find_first($vi['model'], array($cond)); break; default: throw new Exception("unimplemented Model Type value"); } return $value; }