12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612 |
- <?php
- /**
- * [Object Relational Mapping][ref-orm] (ORM) is a method of abstracting database
- * access to standard PHP calls. All table rows are represented as model objects,
- * with object properties representing row data. ORM in Kohana generally follows
- * the [Active Record][ref-act] pattern.
- *
- * [ref-orm]: http://wikipedia.org/wiki/Object-relational_mapping
- * [ref-act]: http://wikipedia.org/wiki/Active_record
- *
- * @package Kohana/ORM
- * @author Kohana Team
- * @copyright (c) Kohana Team
- * @license https://koseven.ga/LICENSE.md
- */
- class Kohana_ORM extends Model implements serializable {
- /**
- * Stores column information for ORM models
- * @var array
- */
- protected static $_column_cache = [];
-
- /**
- * Initialization storage for ORM models
- * @var array
- */
- protected static $_init_cache = [];
- /**
- * Creates and returns a new model.
- * Model name must be passed with its' original casing, e.g.
- *
- * $model = ORM::factory('User_Token');
- *
- * @chainable
- * @param string $model Model name
- * @param mixed $id Parameter for find()
- * @return ORM
- */
- public static function factory($model, $id = NULL)
- {
- // Set class name
- $model = 'Model_'.$model;
- return new $model($id);
- }
- /**
- * "Has one" relationships
- * @var array
- */
- protected $_has_one = [];
- /**
- * "Belongs to" relationships
- * @var array
- */
- protected $_belongs_to = [];
- /**
- * "Has many" relationships
- * @var array
- */
- protected $_has_many = [];
- /**
- * Relationships that should always be joined
- * @var array
- */
- protected $_load_with = [];
- /**
- * Validation object created before saving/updating
- * @var Validation
- */
- protected $_validation = NULL;
- /**
- * Current object
- * @var array
- */
- protected $_object = [];
- /**
- * @var array
- */
- protected $_changed = [];
- /**
- * @var array
- */
- protected $_original_values = [];
- /**
- * @var array
- */
- protected $_related = [];
- /**
- * @var bool
- */
- protected $_valid = FALSE;
- /**
- * @var bool
- */
- protected $_loaded = FALSE;
- /**
- * @var bool
- */
- protected $_saved = FALSE;
- /**
- * @var array
- */
- protected $_sorting;
- /**
- * Foreign key suffix
- * @var string
- */
- protected $_foreign_key_suffix = '_id';
- /**
- * Model name
- * @var string
- */
- protected $_object_name;
- /**
- * Plural model name
- * @var string
- */
- protected $_object_plural;
- /**
- * Table name
- * @var string
- */
- protected $_table_name;
- /**
- * Table columns
- * @var array
- */
- protected $_table_columns;
- /**
- * Auto-update columns for updates
- * @var string
- */
- protected $_updated_column = NULL;
- /**
- * Auto-update columns for creation
- * @var string
- */
- protected $_created_column = NULL;
- /**
- * Auto-serialize and unserialize columns on get/set
- * @var array
- */
- protected $_serialize_columns = [];
- /**
- * Table primary key
- * @var string
- */
- protected $_primary_key = 'id';
- /**
- * Primary key value
- * @var mixed
- */
- protected $_primary_key_value;
- /**
- * Model configuration, table names plural?
- * @var bool
- */
- protected $_table_names_plural = TRUE;
- /**
- * Model configuration, reload on wakeup?
- * @var bool
- */
- protected $_reload_on_wakeup = TRUE;
- /**
- * Database Object
- * @var Database
- */
- protected $_db = NULL;
- /**
- * Database config group
- * @var String
- */
- protected $_db_group = NULL;
- /**
- * Database methods applied
- * @var array
- */
- protected $_db_applied = [];
- /**
- * Database methods pending
- * @var array
- */
- protected $_db_pending = [];
- /**
- * Reset builder
- * @var bool
- */
- protected $_db_reset = TRUE;
- /**
- * Database query builder
- * @var Database_Query_Builder_Select
- */
- protected $_db_builder;
- /**
- * With calls already applied
- * @var array
- */
- protected $_with_applied = [];
- /**
- * Data to be loaded into the model from a database call cast
- * @var array
- */
- protected $_cast_data = [];
- /**
- * The message filename used for validation errors.
- * Defaults to ORM::$_object_name
- * @var string
- */
- protected $_errors_filename = NULL;
- /**
- * List of behaviors
- * @var array
- */
- protected $_behaviors = [];
- /**
- * List of private columns that will not appear in array or object
- * @var array
- */
- protected $_private_columns = FALSE;
- /**
- * Constructs a new model and loads a record if given
- *
- * @param mixed $id Parameter for find or object to load
- */
- public function __construct($id = NULL)
- {
- $this->_initialize();
- // Invoke all behaviors
- foreach ($this->_behaviors as $behavior)
- {
- if (( ! $behavior->on_construct($this, $id)) OR $this->_loaded)
- return;
- }
- if ($id !== NULL)
- {
- if (is_array($id))
- {
- foreach ($id as $column => $value)
- {
- // Passing an array of column => values
- $this->where($column, '=', $value);
- }
- $this->find();
- }
- else
- {
- // Passing the primary key
- $this->where($this->_object_name.'.'.$this->_primary_key, '=', $id)->find();
- }
- }
- elseif ( ! empty($this->_cast_data))
- {
- // Load preloaded data from a database call cast
- $this->_load_values($this->_cast_data);
- $this->_cast_data = [];
- }
- }
- /**
- * Prepares the model database connection, determines the table name,
- * and loads column information.
- *
- * @return void
- */
- protected function _initialize()
- {
- // Set the object name if none predefined
- if (empty($this->_object_name))
- {
- $this->_object_name = strtolower(substr(get_class($this), 6));
- }
-
- // Check if this model has already been initialized
- if ( ! $init = Arr::get(ORM::$_init_cache, $this->_object_name, FALSE))
- {
- $init = [
- '_belongs_to' => [],
- '_has_one' => [],
- '_has_many' => [],
- ];
-
- // Set the object plural name if none predefined
- if ( ! isset($this->_object_plural))
- {
- $init['_object_plural'] = Inflector::plural($this->_object_name);
- }
- if ( ! $this->_errors_filename)
- {
- $init['_errors_filename'] = $this->_object_name;
- }
- if ( ! is_object($this->_db))
- {
- // Get database instance
- $init['_db'] = Database::instance($this->_db_group);
- }
- if (empty($this->_table_name))
- {
- // Table name is the same as the object name
- $init['_table_name'] = $this->_object_name;
- if ($this->_table_names_plural === TRUE)
- {
- // Make the table name plural
- $init['_table_name'] = Arr::get($init, '_object_plural', $this->_object_plural);
- }
- }
-
- $defaults = [];
- foreach ($this->_belongs_to as $alias => $details)
- {
- if ( ! isset($details['model']))
- {
- $defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', $alias)));
- }
-
- $defaults['foreign_key'] = $alias.$this->_foreign_key_suffix;
- $init['_belongs_to'][$alias] = array_merge($defaults, $details);
- }
- foreach ($this->_has_one as $alias => $details)
- {
- if ( ! isset($details['model']))
- {
- $defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', $alias)));
- }
-
- $defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
- $init['_has_one'][$alias] = array_merge($defaults, $details);
- }
- foreach ($this->_has_many as $alias => $details)
- {
- if ( ! isset($details['model']))
- {
- $defaults['model'] = str_replace(' ', '_', ucwords(str_replace('_', ' ', Inflector::singular($alias))));
- }
-
- $defaults['foreign_key'] = $this->_object_name.$this->_foreign_key_suffix;
- $defaults['through'] = NULL;
-
- if ( ! isset($details['far_key']))
- {
- $defaults['far_key'] = Inflector::singular($alias).$this->_foreign_key_suffix;
- }
-
- $init['_has_many'][$alias] = array_merge($defaults, $details);
- }
-
- ORM::$_init_cache[$this->_object_name] = $init;
- }
-
- // Assign initialized properties to the current object
- foreach ($init as $property => $value)
- {
- $this->{$property} = $value;
- }
-
- // Load column information
- $this->reload_columns();
- // Clear initial model state
- $this->clear();
-
- // Create the behaviors classes
- foreach ($this->behaviors() as $behavior => $behavior_config)
- {
- $this->_behaviors[] = ORM_Behavior::factory($behavior, $behavior_config);
- }
- }
- /**
- * Initializes validation rules, and labels
- *
- * @return void
- */
- protected function _validation()
- {
- // Build the validation object with its rules
- $this->_validation = Validation::factory($this->_object)
- ->bind(':model', $this)
- ->bind(':original_values', $this->_original_values)
- ->bind(':changed', $this->_changed);
- foreach ($this->rules() as $field => $rules)
- {
- $this->_validation->rules($field, $rules);
- }
- // Use column names by default for labels
- $columns = array_keys($this->_table_columns);
- // Merge user-defined labels
- $labels = array_merge(array_combine($columns, $columns), $this->labels());
- foreach ($labels as $field => $label)
- {
- $this->_validation->label($field, $label);
- }
- }
- /**
- * Reload column definitions.
- *
- * @chainable
- * @param boolean $force Force reloading
- * @return ORM
- */
- public function reload_columns($force = FALSE)
- {
- if ($force === TRUE OR empty($this->_table_columns))
- {
- if (isset(ORM::$_column_cache[$this->_object_name]))
- {
- // Use cached column information
- $this->_table_columns = ORM::$_column_cache[$this->_object_name];
- }
- else
- {
- // Grab column information from database
- $this->_table_columns = $this->list_columns();
- // Load column cache
- ORM::$_column_cache[$this->_object_name] = $this->_table_columns;
- }
- }
- return $this;
- }
- /**
- * Unloads the current object and clears the status.
- *
- * @chainable
- * @return ORM
- */
- public function clear()
- {
- // Create an array with all the columns set to NULL
- $values = array_combine(array_keys($this->_table_columns), array_fill(0, count($this->_table_columns), NULL));
- // Replace the object and reset the object status
- $this->_object = $this->_changed = $this->_related = $this->_original_values = [];
- // Replace the current object with an empty one
- $this->_load_values($values);
- // Reset primary key
- $this->_primary_key_value = NULL;
-
- // Reset the loaded state
- $this->_loaded = FALSE;
- $this->reset();
- return $this;
- }
- /**
- * Reloads the current object from the database.
- *
- * @chainable
- * @return ORM
- */
- public function reload()
- {
- $primary_key = $this->pk();
- // Replace the object and reset the object status
- $this->_object = $this->_changed = $this->_related = $this->_original_values = [];
- // Only reload the object if we have one to reload
- if ($this->_loaded)
- return $this->clear()
- ->where($this->_object_name.'.'.$this->_primary_key, '=', $primary_key)
- ->find();
- else
- return $this->clear();
- }
- /**
- * Checks if object data is set.
- *
- * @param string $column Column name
- * @return boolean
- */
- public function __isset($column)
- {
- return (isset($this->_object[$column]) OR
- isset($this->_related[$column]) OR
- isset($this->_has_one[$column]) OR
- isset($this->_belongs_to[$column]) OR
- isset($this->_has_many[$column]));
- }
- /**
- * Unsets object data.
- *
- * @param string $column Column name
- * @return void
- */
- public function __unset($column)
- {
- unset($this->_object[$column], $this->_changed[$column], $this->_related[$column]);
- }
- /**
- * Displays the primary key of a model when it is converted to a string.
- *
- * @return string
- */
- public function __toString()
- {
- return (string) $this->pk();
- }
- public function __serialize(): array
- {
- // Store only information about the object
- foreach (['_primary_key_value', '_object', '_changed', '_loaded', '_saved', '_sorting', '_original_values'] as $var)
- {
- $data[$var] = $this->{$var};
- }
- return $data;
- }
- /**
- * Allows serialization of only the object data and state, to prevent
- * "stale" objects being unserialized, which also requires less memory.
- *
- * @return string
- */
- public function serialize()
- {
- return serialize($this->__serialize());
- }
- /**
- * Check whether the model data has been modified.
- * If $field is specified, checks whether that field was modified.
- *
- * @param string $field field to check for changes
- * @return bool Whether or not the field has changed
- */
- public function changed($field = NULL)
- {
- return ($field === NULL)
- ? $this->_changed
- : Arr::get($this->_changed, $field);
- }
- public function __unserialize($data)
- {
- // Initialize model
- $this->_initialize();
- foreach ($data as $name => $var)
- {
- $this->{$name} = $var;
- }
- if ($this->_reload_on_wakeup === TRUE)
- {
- // Reload the object
- $this->reload();
- }
- }
- /**
- * Prepares the database connection and reloads the object.
- *
- * @param string $data String for unserialization
- * @return void
- */
- public function unserialize($data)
- {
- $this->__unserialize(unserialize($data));
- }
- /**
- * Handles retrieval of all model values, relationships, and metadata.
- * [!!] This should not be overridden.
- *
- * @param string $column Column name
- * @return mixed
- */
- public function __get($column)
- {
- return $this->get($column);
- }
-
- /**
- * Handles getting of column
- * Override this method to add custom get behavior
- *
- * @param string $column Column name
- * @throws Kohana_Exception
- * @return mixed
- */
- public function get($column)
- {
- if (array_key_exists($column, $this->_object))
- {
- return (in_array($column, $this->_serialize_columns))
- ? $this->_unserialize_value($this->_object[$column])
- : $this->_object[$column];
- }
- elseif (isset($this->_related[$column]))
- {
- // Return related model that has already been fetched
- return $this->_related[$column];
- }
- elseif (isset($this->_belongs_to[$column]))
- {
- $model = $this->_related($column);
- // Use this model's column and foreign model's primary key
- $col = $model->_object_name.'.'.$model->_primary_key;
- $val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
- // Make sure we don't run WHERE "AUTO_INCREMENT column" = NULL queries. This would
- // return the last inserted record instead of an empty result.
- // See: http://mysql.localhost.net.ar/doc/refman/5.1/en/server-session-variables.html#sysvar_sql_auto_is_null
- if ($val !== NULL)
- {
- $model->where($col, '=', $val)->find();
- }
- return $this->_related[$column] = $model;
- }
- elseif (isset($this->_has_one[$column]))
- {
- $model = $this->_related($column);
- // Use this model's primary key value and foreign model's column
- $col = $model->_object_name.'.'.$this->_has_one[$column]['foreign_key'];
- $val = $this->pk();
- $model->where($col, '=', $val)->find();
- return $this->_related[$column] = $model;
- }
- elseif (isset($this->_has_many[$column]))
- {
- $model = ORM::factory($this->_has_many[$column]['model']);
- if (isset($this->_has_many[$column]['through']))
- {
- // Grab has_many "through" relationship table
- $through = $this->_has_many[$column]['through'];
- // Join on through model's target foreign key (far_key) and target model's primary key
- $join_col1 = $through.'.'.$this->_has_many[$column]['far_key'];
- $join_col2 = $model->_object_name.'.'.$model->_primary_key;
- $model->join($through)->on($join_col1, '=', $join_col2);
- // Through table's source foreign key (foreign_key) should be this model's primary key
- $col = $through.'.'.$this->_has_many[$column]['foreign_key'];
- $val = $this->pk();
- }
- else
- {
- // Simple has_many relationship, search where target model's foreign key is this model's primary key
- $col = $model->_object_name.'.'.$this->_has_many[$column]['foreign_key'];
- $val = $this->pk();
- }
- return $model->where($col, '=', $val);
- }
- else
- {
- throw new Kohana_Exception('The :property property does not exist in the :class class',
- [':property' => $column, ':class' => get_class($this)]);
- }
- }
- /**
- * Base set method.
- * [!!] This should not be overridden.
- *
- * @param string $column Column name
- * @param mixed $value Column value
- * @return void
- */
- public function __set($column, $value)
- {
- $this->set($column, $value);
- }
- /**
- * Handles setting of columns
- * Override this method to add custom set behavior
- *
- * @param string $column Column name
- * @param mixed $value Column value
- * @throws Kohana_Exception
- * @return ORM
- */
- public function set($column, $value)
- {
- if ( ! isset($this->_object_name))
- {
- // Object not yet constructed, so we're loading data from a database call cast
- $this->_cast_data[$column] = $value;
-
- return $this;
- }
-
- if (in_array($column, $this->_serialize_columns))
- {
- $value = $this->_serialize_value($value);
- }
- if (array_key_exists($column, $this->_object))
- {
- // Filter the data
- $value = $this->run_filter($column, $value);
- // See if the data really changed
- if ($value !== $this->_object[$column])
- {
- $this->_object[$column] = $value;
- // Data has changed
- $this->_changed[$column] = $column;
- // Object is no longer saved or valid
- $this->_saved = $this->_valid = FALSE;
- }
- }
- elseif (isset($this->_belongs_to[$column]))
- {
- // Update related object itself
- $this->_related[$column] = $value;
- // Update the foreign key of this model
- $this->_object[$this->_belongs_to[$column]['foreign_key']] = ($value instanceof ORM)
- ? $value->pk()
- : NULL;
- $this->_changed[$column] = $this->_belongs_to[$column]['foreign_key'];
- }
- elseif (isset($this->_has_many[$column]))
- {
- if (Arr::get($this->_has_many[$column], 'update', FALSE))
- {
- $model = $this->_has_many[$column]['model'];
- $pk = ORM::factory($model)->primary_key();
-
- $current_ids = $this->get($column)->find_all()->as_array(NULL, 'id');
- $new_ids = array_diff($value, $current_ids);
- if (count($new_ids) > 0)
- {
- $objects = ORM::factory($model)->where($pk, 'IN', $new_ids)->find_all();
- foreach ($objects as $object)
- {
- $this->add($column, $object);
- }
- }
- $delete_ids = array_diff($current_ids, $value);
- if (count($delete_ids) > 0)
- {
- $objects = ORM::factory($model)->where($pk, 'IN', $delete_ids)->find_all();
- foreach ($objects as $object)
- {
- $this->remove($column, $object);
- }
- }
- }
- else
- {
- throw new Kohana_Exception('The :property: property is a to many relation in the :class: class',
- [':property:' => $column, ':class:' => get_class($this)]);
- }
- }
- else
- {
- throw new Kohana_Exception('The :property: property does not exist in the :class: class',
- [':property:' => $column, ':class:' => get_class($this)]);
- }
- return $this;
- }
- /**
- * Set values from an array with support for one-one relationships. This method should be used
- * for loading in post data, etc.
- *
- * @param array $values Array of column => val
- * @param array $expected Array of keys to take from $values
- * @return ORM
- */
- public function values(array $values, array $expected = NULL)
- {
- // Default to expecting everything except the primary key
- if ($expected === NULL)
- {
- $expected = array_keys($this->_table_columns);
- // Don't set the primary key by default
- unset($values[$this->_primary_key]);
- }
- foreach ($expected as $key => $column)
- {
- if (is_string($key))
- {
- // isset() fails when the value is NULL (we want it to pass)
- if ( ! array_key_exists($key, $values))
- continue;
- // Try to set values to a related model
- $this->{$key}->values($values[$key], $column);
- }
- else
- {
- // isset() fails when the value is NULL (we want it to pass)
- if ( ! array_key_exists($column, $values))
- continue;
- // Update the column, respects __set()
- $this->$column = $values[$column];
- }
- }
- return $this;
- }
- /**
- * Returns the type of the column
- *
- * @param string $column
- * @return string
- */
- public function table_column_type($column)
- {
- if ( ! array_key_exists($column, $this->_table_columns))
- return FALSE;
-
- return $this->_table_columns[$column]['type'];
- }
- /**
- * Returns a value as the native type, will return FALSE if the
- * value could not be casted.
- *
- * @param string $column
- * @return mixed
- */
- protected function get_typed($column)
- {
- $value = $this->get($column);
-
- if ($value === NULL)
- return NULL;
- // Call __get for any user processing
- switch($this->table_column_type($column))
- {
- case 'float': return floatval($this->__get($column));
- case 'int': return intval($this->__get($column));
- case 'string': return strval($this->__get($column));
- }
-
- return $value;
- }
- /**
- * Returns the values of this object as an array, including any related one-one
- * models that have already been loaded using with()
- *
- * @return array
- */
- public function as_array($show_all=FALSE)
- {
- $object = [];
- if ($show_all OR !is_array($this->_private_columns))
- {
- foreach ($this->_object as $column => $value)
- {
- // Call __get for any user processing
- $object[$column] = $this->__get($column);
- }
- }
- else
- {
- foreach ($this->_object as $column => $value)
- {
- // Call __get for any user processing
- if (!in_array($column, $this->_private_columns))
- $object[$column] = $this->__get($column);
- }
- }
- foreach ($this->_related as $column => $model)
- {
- // Include any related objects that are already loaded
- $object[$column] = $model->as_array();
- }
- return $object;
- }
- /**
- * Returns the values of this object as an new object, including any related
- * one-one models that have already been loaded using with(). Removes private
- * columns.
- *
- * @return array
- */
- public function as_object($show_all=FALSE)
- {
- $object = new stdClass;
- if ($show_all OR !is_array($this->_private_columns))
- {
- foreach ($this->_object as $column => $value)
- {
- $object->{$column} = $this->get_typed($column);
- }
- }
- else
- {
- foreach ($this->_object as $column => $value)
- {
- if (!in_array($column, $this->_private_columns))
- {
- $object->{$column} = $this->get_typed($column);
- }
- }
- }
- foreach ($this->_related as $column => $model)
- {
- // Include any related objects that are already loaded
- $object->{$column} = $model->as_object();
- }
-
- return $object;
- }
- /**
- * Binds another one-to-one object to this model. One-to-one objects
- * can be nested using 'object1:object2' syntax
- *
- * @param string $target_path Target model to bind to
- * @return ORM
- */
- public function with($target_path)
- {
- if (isset($this->_with_applied[$target_path]))
- {
- // Don't join anything already joined
- return $this;
- }
- // Split object parts
- $aliases = explode(':', $target_path);
- $target = $this;
- foreach ($aliases as $alias)
- {
- // Go down the line of objects to find the given target
- $parent = $target;
- $target = $parent->_related($alias);
- if ( ! $target)
- {
- // Can't find related object
- return $this;
- }
- }
- // Target alias is at the end
- $target_alias = $alias;
- // Pop-off top alias to get the parent path (user:photo:tag becomes user:photo - the parent table prefix)
- array_pop($aliases);
- $parent_path = implode(':', $aliases);
- if (empty($parent_path))
- {
- // Use this table name itself for the parent path
- $parent_path = $this->_object_name;
- }
- else
- {
- if ( ! isset($this->_with_applied[$parent_path]))
- {
- // If the parent path hasn't been joined yet, do it first (otherwise LEFT JOINs fail)
- $this->with($parent_path);
- }
- }
- // Add to with_applied to prevent duplicate joins
- $this->_with_applied[$target_path] = TRUE;
- // Use the keys of the empty object to determine the columns
- foreach (array_keys($target->_object) as $column)
- {
- $name = $target_path.'.'.$column;
- $alias = $target_path.':'.$column;
- // Add the prefix so that load_result can determine the relationship
- $this->select([$name, $alias]);
- }
- if (isset($parent->_belongs_to[$target_alias]))
- {
- // Parent belongs_to target, use target's primary key and parent's foreign key
- $join_col1 = $target_path.'.'.$target->_primary_key;
- $join_col2 = $parent_path.'.'.$parent->_belongs_to[$target_alias]['foreign_key'];
- }
- else
- {
- // Parent has_one target, use parent's primary key as target's foreign key
- $join_col1 = $parent_path.'.'.$parent->_primary_key;
- $join_col2 = $target_path.'.'.$parent->_has_one[$target_alias]['foreign_key'];
- }
- // Join the related object into the result
- $this->join([$target->_table_name, $target_path], 'LEFT')->on($join_col1, '=', $join_col2);
- return $this;
- }
- /**
- * Initializes the Database Builder to given query type
- *
- * @param integer $type Type of Database query
- * @return ORM
- */
- protected function _build($type)
- {
- // Construct new builder object based on query type
- switch ($type)
- {
- case Database::SELECT:
- $this->_db_builder = DB::select();
- break;
- case Database::UPDATE:
- $this->_db_builder = DB::update([$this->_table_name, $this->_object_name]);
- break;
- case Database::DELETE:
- // Cannot use an alias for DELETE queries
- $this->_db_builder = DB::delete($this->_table_name);
- }
- // Process pending database method calls
- foreach ($this->_db_pending as $method)
- {
- $name = $method['name'];
- $args = $method['args'];
- $this->_db_applied[$name] = $name;
- call_user_func_array([$this->_db_builder, $name], $args);
- }
- return $this;
- }
- /**
- * Finds and loads a single database row into the object.
- *
- * @chainable
- * @throws Kohana_Exception
- * @return ORM
- */
- public function find()
- {
- if ($this->_loaded)
- throw new Kohana_Exception('Method find() cannot be called on loaded objects');
- if ( ! empty($this->_load_with))
- {
- foreach ($this->_load_with as $alias)
- {
- // Bind auto relationships
- $this->with($alias);
- }
- }
- $this->_build(Database::SELECT);
- return $this->_load_result(FALSE);
- }
- /**
- * Finds multiple database rows and returns an iterator of the rows found.
- *
- * @throws Kohana_Exception
- * @return Database_Result
- */
- public function find_all()
- {
- if ($this->_loaded)
- throw new Kohana_Exception('Method find_all() cannot be called on loaded objects');
- if ( ! empty($this->_load_with))
- {
- foreach ($this->_load_with as $alias)
- {
- // Bind auto relationships
- $this->with($alias);
- }
- }
- $this->_build(Database::SELECT);
- return $this->_load_result(TRUE);
- }
- /**
- * Returns an array of columns to include in the select query. This method
- * can be overridden to change the default select behavior.
- *
- * @return array Columns to select
- */
- protected function _build_select()
- {
- $columns = [];
- foreach ($this->_table_columns as $column => $_)
- {
- $columns[] = [$this->_object_name.'.'.$column, $column];
- }
- return $columns;
- }
- /**
- * Loads a database result, either as a new record for this model, or as
- * an iterator for multiple rows.
- *
- * @chainable
- * @param bool $multiple Return an iterator or load a single row
- * @return ORM|Database_Result
- */
- protected function _load_result($multiple = FALSE)
- {
- $this->_db_builder->from([$this->_table_name, $this->_object_name]);
- if ($multiple === FALSE)
- {
- // Only fetch 1 record
- $this->_db_builder->limit(1);
- }
- // Select all columns by default
- $this->_db_builder->select_array($this->_build_select());
- if ( ! isset($this->_db_applied['order_by']) AND ! empty($this->_sorting))
- {
- foreach ($this->_sorting as $column => $direction)
- {
- if (strpos($column, '.') === FALSE)
- {
- // Sorting column for use in JOINs
- $column = $this->_object_name.'.'.$column;
- }
- $this->_db_builder->order_by($column, $direction);
- }
- }
- if ($multiple === TRUE)
- {
- // Return database iterator casting to this object type
- $result = $this->_db_builder->as_object(get_class($this))->execute($this->_db);
- $this->reset();
- return $result;
- }
- else
- {
- // Load the result as an associative array
- $result = $this->_db_builder->as_assoc()->execute($this->_db);
- $this->reset();
- if ($result->count() === 1)
- {
- // Load object values
- $this->_load_values($result->current());
- }
- else
- {
- // Clear the object, nothing was found
- $this->clear();
- }
- return $this;
- }
- }
- /**
- * Loads an array of values into into the current object.
- *
- * @chainable
- * @param array $values Values to load
- * @return ORM
- */
- protected function _load_values(array $values)
- {
- if (array_key_exists($this->_primary_key, $values))
- {
- if ($values[$this->_primary_key] !== NULL)
- {
- // Flag as loaded and valid
- $this->_loaded = $this->_valid = TRUE;
- // Store primary key
- $this->_primary_key_value = $values[$this->_primary_key];
- }
- else
- {
- // Not loaded or valid
- $this->_loaded = $this->_valid = FALSE;
- }
- }
- // Related objects
- $related = [];
- foreach ($values as $column => $value)
- {
- if (strpos($column, ':') === FALSE)
- {
- // Load the value to this model
- $this->_object[$column] = $value;
- }
- else
- {
- // Column belongs to a related model
- list ($prefix, $column) = explode(':', $column, 2);
- $related[$prefix][$column] = $value;
- }
- }
- if ( ! empty($related))
- {
- foreach ($related as $object => $values)
- {
- // Load the related objects with the values in the result
- $this->_related($object)->_load_values($values);
- }
- }
- if ($this->_loaded)
- {
- // Store the object in its original state
- $this->_original_values = $this->_object;
- }
- return $this;
- }
- /**
- * Behavior definitions
- *
- * @return array
- */
- public function behaviors()
- {
- return [];
- }
- /**
- * Rule definitions for validation
- *
- * @return array
- */
- public function rules()
- {
- return [];
- }
- /**
- * Filters a value for a specific column
- *
- * @param string $field The column name
- * @param string $value The value to filter
- * @return string
- */
- protected function run_filter($field, $value)
- {
- $filters = $this->filters();
- // Get the filters for this column
- $wildcards = empty($filters[TRUE]) ? [] : $filters[TRUE];
- // Merge in the wildcards
- $filters = empty($filters[$field]) ? $wildcards : array_merge($wildcards, $filters[$field]);
- // Bind the field name and model so they can be used in the filter method
- $_bound = [
- ':field' => $field,
- ':model' => $this,
- ];
- foreach ($filters as $array)
- {
- // Value needs to be bound inside the loop so we are always using the
- // version that was modified by the filters that already ran
- $_bound[':value'] = $value;
- // Filters are defined as array($filter, $params)
- $filter = $array[0];
- $params = Arr::get($array, 1, [':value']);
- foreach ($params as $key => $param)
- {
- if (is_string($param) AND array_key_exists($param, $_bound))
- {
- // Replace with bound value
- $params[$key] = $_bound[$param];
- }
- }
- if (is_array($filter) OR ! is_string($filter))
- {
- // This is either a callback as an array or a lambda
- $value = call_user_func_array($filter, $params);
- }
- elseif (strpos($filter, '::') === FALSE)
- {
- // Use a function call
- $function = new ReflectionFunction($filter);
- // Call $function($this[$field], $param, ...) with Reflection
- $value = $function->invokeArgs($params);
- }
- else
- {
- // Split the class and method of the rule
- list($class, $method) = explode('::', $filter, 2);
- // Use a static method call
- $method = new ReflectionMethod($class, $method);
- // Call $Class::$method($this[$field], $param, ...) with Reflection
- $value = $method->invokeArgs(NULL, $params);
- }
- }
- return $value;
- }
- /**
- * Filter definitions for validation
- *
- * @return array
- */
- public function filters()
- {
- return [];
- }
- /**
- * Label definitions for validation
- *
- * @return array
- */
- public function labels()
- {
- return [];
- }
- /**
- * Validates the current model's data
- *
- * @param Validation $extra_validation Validation object
- * @throws ORM_Validation_Exception
- * @return ORM
- */
- public function check(Validation $extra_validation = NULL)
- {
- // Determine if any external validation failed
- $extra_errors = ($extra_validation AND ! $extra_validation->check());
- // Always build a new validation object
- $this->_validation();
- $array = $this->_validation;
- if (($this->_valid = $array->check()) === FALSE OR $extra_errors)
- {
- $exception = new ORM_Validation_Exception($this->errors_filename(), $array);
- if ($extra_errors)
- {
- // Merge any possible errors from the external object
- $exception->add_object('_external', $extra_validation);
- }
- throw $exception;
- }
- return $this;
- }
- /**
- * Insert a new object to the database
- * @param Validation $validation Validation object
- * @throws Kohana_Exception
- * @return ORM
- */
- public function create(Validation $validation = NULL)
- {
- if ($this->_loaded)
- throw new Kohana_Exception('Cannot create :model model because it is already loaded.', [':model' => $this->_object_name]);
- // Invoke all behaviors
- foreach ($this->_behaviors as $behavior)
- {
- $behavior->on_create($this);
- }
- // Require model validation before saving
- if ( ! $this->_valid OR $validation)
- {
- $this->check($validation);
- }
- $data = [];
- foreach ($this->_changed as $column)
- {
- // Generate list of column => values
- $data[$column] = $this->_object[$column];
- }
- if (is_array($this->_created_column))
- {
- // Fill the created column
- $column = $this->_created_column['column'];
- $format = $this->_created_column['format'];
- $data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
- }
- $result = DB::insert($this->_table_name)
- ->columns(array_keys($data))
- ->values(array_values($data))
- ->execute($this->_db);
- if ( ! array_key_exists($this->_primary_key, $data) OR ($this->_object[$this->_primary_key] === NULL))
- {
- // Load the insert id as the primary key if it was left out
- $this->_object[$this->_primary_key] = $this->_primary_key_value = $result[0];
- }
- else
- {
- $this->_primary_key_value = $this->_object[$this->_primary_key];
- }
- // Object is now loaded and saved
- $this->_loaded = $this->_saved = TRUE;
- // All changes have been saved
- $this->_changed = [];
- $this->_original_values = $this->_object;
- return $this;
- }
- /**
- * Updates a single record or multiple records
- *
- * @chainable
- * @param Validation $validation Validation object
- * @throws Kohana_Exception
- * @return ORM
- */
- public function update(Validation $validation = NULL)
- {
- if ( ! $this->_loaded)
- throw new Kohana_Exception('Cannot update :model model because it is not loaded.', [':model' => $this->_object_name]);
- // Invoke all behaviors
- foreach ($this->_behaviors as $behavior)
- {
- $behavior->on_update($this);
- }
- // Run validation if the model isn't valid or we have additional validation rules.
- if ( ! $this->_valid OR $validation)
- {
- $this->check($validation);
- }
- if (empty($this->_changed))
- {
- // Nothing to update
- return $this;
- }
- $data = [];
- foreach ($this->_changed as $column)
- {
- // Compile changed data
- $data[$column] = $this->_object[$column];
- }
- if (is_array($this->_updated_column))
- {
- // Fill the updated column
- $column = $this->_updated_column['column'];
- $format = $this->_updated_column['format'];
- $data[$column] = $this->_object[$column] = ($format === TRUE) ? time() : date($format);
- }
- // Use primary key value
- $id = $this->pk();
- // Update a single record
- DB::update($this->_table_name)
- ->set($data)
- ->where($this->_primary_key, '=', $id)
- ->execute($this->_db);
- if (isset($data[$this->_primary_key]))
- {
- // Primary key was changed, reflect it
- $this->_primary_key_value = $data[$this->_primary_key];
- }
- // Object has been saved
- $this->_saved = TRUE;
- // All changes have been saved
- $this->_changed = [];
- $this->_original_values = $this->_object;
- return $this;
- }
- /**
- * Updates or Creates the record depending on loaded()
- *
- * @chainable
- * @param Validation $validation Validation object
- * @return ORM
- */
- public function save(Validation $validation = NULL)
- {
- return $this->loaded() ? $this->update($validation) : $this->create($validation);
- }
- /**
- * Deletes a single record while ignoring relationships.
- *
- * @chainable
- * @throws Kohana_Exception
- * @return ORM
- */
- public function delete()
- {
- if ( ! $this->_loaded)
- throw new Kohana_Exception('Cannot delete :model model because it is not loaded.', [':model' => $this->_object_name]);
- // Use primary key value
- $id = $this->pk();
- // Delete the object
- DB::delete($this->_table_name)
- ->where($this->_primary_key, '=', $id)
- ->execute($this->_db);
- return $this->clear();
- }
- /**
- * Tests if this object has a relationship to a different model,
- * or an array of different models. When providing far keys, the number
- * of relations must equal the number of keys.
- *
- *
- * // Check if $model has the login role
- * $model->has('roles', ORM::factory('role', array('name' => 'login')));
- * // Check for the login role if you know the roles.id is 5
- * $model->has('roles', 5);
- * // Check for all of the following roles
- * $model->has('roles', array(1, 2, 3, 4));
- * // Check if $model has any roles
- * $model->has('roles')
- *
- * @param string $alias Alias of the has_many "through" relationship
- * @param mixed $far_keys Related model, primary key, or an array of primary keys
- * @return boolean
- */
- public function has($alias, $far_keys = NULL)
- {
- $count = $this->count_relations($alias, $far_keys);
- if ($far_keys === NULL)
- {
- return (bool) $count;
- }
- else
- {
- if (is_array($far_keys) OR $far_keys instanceof Countable)
- {
- $keys = count($far_keys);
- }
- else
- {
- $keys = 1;
- }
- return $keys === $count;
- }
- }
- /**
- * Tests if this object has a relationship to a different model,
- * or an array of different models. When providing far keys, this function
- * only checks that at least one of the relationships is satisfied.
- *
- * // Check if $model has the login role
- * $model->has('roles', ORM::factory('role', array('name' => 'login')));
- * // Check for the login role if you know the roles.id is 5
- * $model->has('roles', 5);
- * // Check for any of the following roles
- * $model->has('roles', array(1, 2, 3, 4));
- * // Check if $model has any roles
- * $model->has('roles')
- *
- * @param string $alias Alias of the has_many "through" relationship
- * @param mixed $far_keys Related model, primary key, or an array of primary keys
- * @return boolean
- */
- public function has_any($alias, $far_keys = NULL)
- {
- return (bool) $this->count_relations($alias, $far_keys);
- }
- /**
- * Returns the number of relationships
- *
- * // Counts the number of times the login role is attached to $model
- * $model->count_relations('roles', ORM::factory('role', array('name' => 'login')));
- * // Counts the number of times role 5 is attached to $model
- * $model->count_relations('roles', 5);
- * // Counts the number of times any of roles 1, 2, 3, or 4 are attached to
- * // $model
- * $model->count_relations('roles', array(1, 2, 3, 4));
- * // Counts the number roles attached to $model
- * $model->count_relations('roles')
- *
- * @param string $alias Alias of the has_many "through" relationship
- * @param mixed $far_keys Related model, primary key, or an array of primary keys
- * @return integer
- */
- public function count_relations($alias, $far_keys = NULL)
- {
- if ($far_keys === NULL)
- {
- return (int) DB::select([DB::expr('COUNT(*)'), 'records_found'])
- ->from($this->_has_many[$alias]['through'])
- ->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
- ->execute($this->_db)->get('records_found');
- }
- $far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
- // We need an array to simplify the logic
- $far_keys = (array) $far_keys;
- // Nothing to check if the model isn't loaded or we don't have any far_keys
- if ( ! $far_keys OR ! $this->_loaded)
- return 0;
- $count = (int) DB::select([DB::expr('COUNT(*)'), 'records_found'])
- ->from($this->_has_many[$alias]['through'])
- ->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk())
- ->where($this->_has_many[$alias]['far_key'], 'IN', $far_keys)
- ->execute($this->_db)->get('records_found');
- // Rows found need to match the rows searched
- return (int) $count;
- }
- /**
- * Adds a new relationship to between this model and another.
- *
- * // Add the login role using a model instance
- * $model->add('roles', ORM::factory('role', array('name' => 'login')));
- * // Add the login role if you know the roles.id is 5
- * $model->add('roles', 5);
- * // Add multiple roles (for example, from checkboxes on a form)
- * $model->add('roles', array(1, 2, 3, 4));
- *
- * @param string $alias Alias of the has_many "through" relationship
- * @param mixed $far_keys Related model, primary key, or an array of primary keys
- * @return ORM
- */
- public function add($alias, $far_keys)
- {
- $far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
- $columns = [$this->_has_many[$alias]['foreign_key'], $this->_has_many[$alias]['far_key']];
- $foreign_key = $this->pk();
- $query = DB::insert($this->_has_many[$alias]['through'], $columns);
- foreach ( (array) $far_keys as $key)
- {
- $query->values([$foreign_key, $key]);
- }
- $query->execute($this->_db);
- return $this;
- }
- /**
- * Removes a relationship between this model and another.
- *
- * // Remove a role using a model instance
- * $model->remove('roles', ORM::factory('role', array('name' => 'login')));
- * // Remove the role knowing the primary key
- * $model->remove('roles', 5);
- * // Remove multiple roles (for example, from checkboxes on a form)
- * $model->remove('roles', array(1, 2, 3, 4));
- * // Remove all related roles
- * $model->remove('roles');
- *
- * @param string $alias Alias of the has_many "through" relationship
- * @param mixed $far_keys Related model, primary key, or an array of primary keys
- * @return ORM
- */
- public function remove($alias, $far_keys = NULL)
- {
- $far_keys = ($far_keys instanceof ORM) ? $far_keys->pk() : $far_keys;
- $query = DB::delete($this->_has_many[$alias]['through'])
- ->where($this->_has_many[$alias]['foreign_key'], '=', $this->pk());
- if ($far_keys !== NULL)
- {
- // Remove all the relationships in the array
- $query->where($this->_has_many[$alias]['far_key'], 'IN', (array) $far_keys);
- }
- $query->execute($this->_db);
- return $this;
- }
- /**
- * Count the number of records in the table.
- *
- * @return integer
- */
- public function count_all()
- {
- $selects = [];
- foreach ($this->_db_pending as $key => $method)
- {
- if ($method['name'] == 'select')
- {
- // Ignore any selected columns for now
- $selects[$key] = $method;
- unset($this->_db_pending[$key]);
- }
- }
- if ( ! empty($this->_load_with))
- {
- foreach ($this->_load_with as $alias)
- {
- // Bind relationship
- $this->with($alias);
- }
- }
- $this->_build(Database::SELECT);
- $records = $this->_db_builder->from([$this->_table_name, $this->_object_name])
- ->select([DB::expr('COUNT('.$this->_db->quote_column($this->_object_name.'.'.$this->_primary_key).')'), 'records_found'])
- ->execute($this->_db)
- ->get('records_found');
- // Add back in selected columns
- $this->_db_pending += $selects;
- $this->reset();
- // Return the total number of records in a table
- return (int) $records;
- }
- /**
- * Proxy method to Database list_columns.
- *
- * @return array
- */
- public function list_columns()
- {
- // Proxy to database
- return $this->_db->list_columns($this->_table_name);
- }
- /**
- * Returns an ORM model for the given one-one related alias
- *
- * @param string $alias Alias name
- * @return ORM
- */
- protected function _related($alias)
- {
- if (isset($this->_related[$alias]))
- {
- return $this->_related[$alias];
- }
- elseif (isset($this->_has_one[$alias]))
- {
- return $this->_related[$alias] = ORM::factory($this->_has_one[$alias]['model']);
- }
- elseif (isset($this->_belongs_to[$alias]))
- {
- return $this->_related[$alias] = ORM::factory($this->_belongs_to[$alias]['model']);
- }
- else
- {
- return FALSE;
- }
- }
- /**
- * Returns the value of the primary key
- *
- * @return mixed Primary key
- */
- public function pk()
- {
- return $this->_primary_key_value;
- }
- /**
- * Returns last executed query
- *
- * @return string
- */
- public function last_query()
- {
- return $this->_db->last_query;
- }
- /**
- * Clears query builder. Passing FALSE is useful to keep the existing
- * query conditions for another query.
- *
- * @param bool $next Pass FALSE to avoid resetting on the next call
- * @return ORM
- */
- public function reset($next = TRUE)
- {
- if ($next AND $this->_db_reset)
- {
- $this->_db_pending = [];
- $this->_db_applied = [];
- $this->_db_builder = NULL;
- $this->_with_applied = [];
- }
- // Reset on the next call?
- $this->_db_reset = $next;
- return $this;
- }
- /**
- * @param mixed $value
- * @return string
- */
- protected function _serialize_value($value)
- {
- return json_encode($value);
- }
- /**
- * @param string $value
- * @return array
- */
- protected function _unserialize_value($value)
- {
- return json_decode($value, TRUE);
- }
- /**
- * @return string
- */
- public function object_name()
- {
- return $this->_object_name;
- }
- /**
- * @return object
- */
- public function object_plural()
- {
- return $this->_object_plural;
- }
- /**
- * @return bool
- */
- public function loaded()
- {
- return $this->_loaded;
- }
- /**
- * @return bool
- */
- public function saved()
- {
- return $this->_saved;
- }
- /**
- * @return string
- */
- public function primary_key()
- {
- return $this->_primary_key;
- }
- /**
- * @return string
- */
- public function table_name()
- {
- return $this->_table_name;
- }
- /**
- * @return array
- */
- public function table_columns()
- {
- return $this->_table_columns;
- }
- /**
- * @return array
- */
- public function has_one()
- {
- return $this->_has_one;
- }
- /**
- * @return array
- */
- public function belongs_to()
- {
- return $this->_belongs_to;
- }
- /**
- * @return array
- */
- public function has_many()
- {
- return $this->_has_many;
- }
- /**
- * @return array
- */
- public function load_with()
- {
- return $this->_load_with;
- }
- /**
- * @return array
- */
- public function original_values()
- {
- return $this->_original_values;
- }
- /**
- * @return string
- */
- public function created_column()
- {
- return $this->_created_column;
- }
- /**
- * @return string
- */
- public function updated_column()
- {
- return $this->_updated_column;
- }
- /**
- * @return Validation
- */
- public function validation()
- {
- if ( ! isset($this->_validation))
- {
- // Initialize the validation object
- $this->_validation();
- }
- return $this->_validation;
- }
- /**
- * @return object
- */
- public function object()
- {
- return $this->_object;
- }
- /**
- * @return string
- */
- public function errors_filename()
- {
- return $this->_errors_filename;
- }
- /**
- * Alias of and_where()
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function where($column, $op, $value)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'where',
- 'args' => [$column, $op, $value],
- ];
- return $this;
- }
- /**
- * Creates a new "AND WHERE" condition for the query.
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function and_where($column, $op, $value)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_where',
- 'args' => [$column, $op, $value],
- ];
- return $this;
- }
- /**
- * Creates a new "OR WHERE" condition for the query.
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function or_where($column, $op, $value)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_where',
- 'args' => [$column, $op, $value],
- ];
- return $this;
- }
- /**
- * Alias of and_where_open()
- *
- * @return ORM
- */
- public function where_open()
- {
- return $this->and_where_open();
- }
- /**
- * Opens a new "AND WHERE (...)" grouping.
- *
- * @return ORM
- */
- public function and_where_open()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_where_open',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Opens a new "OR WHERE (...)" grouping.
- *
- * @return ORM
- */
- public function or_where_open()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_where_open',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Closes an open "AND WHERE (...)" grouping.
- *
- * @return ORM
- */
- public function where_close()
- {
- return $this->and_where_close();
- }
- /**
- * Closes an open "AND WHERE (...)" grouping.
- *
- * @return ORM
- */
- public function and_where_close()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_where_close',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Closes an open "OR WHERE (...)" grouping.
- *
- * @return ORM
- */
- public function or_where_close()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_where_close',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Applies sorting with "ORDER BY ..."
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $direction direction of sorting
- * @return ORM
- */
- public function order_by($column, $direction = NULL)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'order_by',
- 'args' => [$column, $direction],
- ];
- return $this;
- }
- /**
- * Return up to "LIMIT ..." results
- *
- * @param integer $number maximum results to return
- * @return ORM
- */
- public function limit($number)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'limit',
- 'args' => [$number],
- ];
- return $this;
- }
- /**
- * Enables or disables selecting only unique columns using "SELECT DISTINCT"
- *
- * @param boolean $value enable or disable distinct columns
- * @return ORM
- */
- public function distinct($value)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'distinct',
- 'args' => [$value],
- ];
- return $this;
- }
- /**
- * Choose the columns to select from.
- *
- * @param mixed $columns column name or array($column, $alias) or object
- * @param ...
- * @return ORM
- */
- public function select(...$columns)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'select',
- 'args' => $columns,
- ];
- return $this;
- }
- /**
- * Choose the tables to select "FROM ..."
- *
- * @param mixed $tables table name or array($table, $alias) or object
- * @param ...
- * @return ORM
- */
- public function from(...$tables)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'from',
- 'args' => $tables,
- ];
- return $this;
- }
- /**
- * Adds addition tables to "JOIN ...".
- *
- * @param mixed $table column name or array($column, $alias) or object
- * @param string $type join type (LEFT, RIGHT, INNER, etc)
- * @return ORM
- */
- public function join($table, $type = NULL)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'join',
- 'args' => [$table, $type],
- ];
- return $this;
- }
- /**
- * Adds "ON ..." conditions for the last created JOIN statement.
- *
- * @param mixed $c1 column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $c2 column name or array($column, $alias) or object
- * @return ORM
- */
- public function on($c1, $op, $c2)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'on',
- 'args' => [$c1, $op, $c2],
- ];
- return $this;
- }
- /**
- * Creates a "GROUP BY ..." filter.
- *
- * @param mixed $columns column name or array($column, $alias) or object
- * @param ...
- * @return ORM
- */
- public function group_by(...$columns)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'group_by',
- 'args' => $columns,
- ];
- return $this;
- }
- /**
- * Alias of and_having()
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function having($column, $op, $value = NULL)
- {
- return $this->and_having($column, $op, $value);
- }
- /**
- * Creates a new "AND HAVING" condition for the query.
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function and_having($column, $op, $value = NULL)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_having',
- 'args' => [$column, $op, $value],
- ];
- return $this;
- }
- /**
- * Creates a new "OR HAVING" condition for the query.
- *
- * @param mixed $column column name or array($column, $alias) or object
- * @param string $op logic operator
- * @param mixed $value column value
- * @return ORM
- */
- public function or_having($column, $op, $value = NULL)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_having',
- 'args' => [$column, $op, $value],
- ];
- return $this;
- }
- /**
- * Alias of and_having_open()
- *
- * @return ORM
- */
- public function having_open()
- {
- return $this->and_having_open();
- }
- /**
- * Opens a new "AND HAVING (...)" grouping.
- *
- * @return ORM
- */
- public function and_having_open()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_having_open',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Opens a new "OR HAVING (...)" grouping.
- *
- * @return ORM
- */
- public function or_having_open()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_having_open',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Closes an open "AND HAVING (...)" grouping.
- *
- * @return ORM
- */
- public function having_close()
- {
- return $this->and_having_close();
- }
- /**
- * Closes an open "AND HAVING (...)" grouping.
- *
- * @return ORM
- */
- public function and_having_close()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'and_having_close',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Closes an open "OR HAVING (...)" grouping.
- *
- * @return ORM
- */
- public function or_having_close()
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'or_having_close',
- 'args' => [],
- ];
- return $this;
- }
- /**
- * Start returning results after "OFFSET ..."
- *
- * @param integer $number starting result number
- * @return ORM
- */
- public function offset($number)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'offset',
- 'args' => [$number],
- ];
- return $this;
- }
- /**
- * Enables the query to be cached for a specified amount of time.
- *
- * @param integer $lifetime number of seconds to cache
- * @return ORM
- * @uses Kohana::$cache_life
- */
- public function cached($lifetime = NULL)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'cached',
- 'args' => [$lifetime],
- ];
- return $this;
- }
- /**
- * Set the value of a parameter in the query.
- *
- * @param string $param parameter key to replace
- * @param mixed $value value to use
- * @return ORM
- */
- public function param($param, $value)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'param',
- 'args' => [$param, $value],
- ];
- return $this;
- }
- /**
- * Adds "USING ..." conditions for the last created JOIN statement.
- *
- * @param mixed $columns column names
- * @param ...
- * @return ORM
- */
- public function using(...$columns)
- {
- // Add pending database call which is executed after query type is determined
- $this->_db_pending[] = [
- 'name' => 'using',
- 'args' => $columns,
- ];
- return $this;
- }
- /**
- * Checks whether a column value is unique.
- * Excludes itself if loaded.
- *
- * @param string $field the field to check for uniqueness
- * @param mixed $value the value to check for uniqueness
- * @return bool whteher the value is unique
- */
- public function unique($field, $value)
- {
- $model = ORM::factory($this->object_name())
- ->where($field, '=', $value)
- ->find();
- if ($this->loaded())
- {
- return ( ! ($model->loaded() AND $model->pk() != $this->pk()));
- }
- return ( ! $model->loaded());
- }
- /**
- * Get the quoted table name from the model name
- *
- * @param string $orm_model Model name
- * @return string Quoted table name
- */
- public static function quote_table($orm_model)
- {
- return Database::instance()->quote_table(strtolower($orm_model));
- }
- }
|