/solar/source/miku/Miku/Filter/SanitizeBelongsTo.php
PHP | 114 lines | 74 code | 14 blank | 26 comment | 13 complexity | ff05bd5d8d91501353bf476b0ca9f1a0 MD5 | raw file
Possible License(s): MIT
- <?php
-
- /* prepare the related col, witch should be unique
- * @param String $col Unique col in the foreign
- * @param String $foreign_alias Belongs to's foreign_alias, used to insert a new parent record if not exist
- * @param String $cache_col Cache col in the foreign
- * @param Mix $where Additional "WHERE" conditions to exclude records from the uniqueness check.
- * @return Null
- */
-
- class Miku_Filter_SanitizeBelongsTo extends Solar_Filter_Abstract
- {
- //$this->_addFilter('_foo', 'sanitizeBelongsTo');
- public function sanitizeBelongsTo($value, $col, $foreign_alias, $cache_col = null, $where = null)
- {
- if(! is_array($value)) {
- throw $this->_exception('ERR_NOT_ARRAY_VALUE');
- }
-
- $record = $this->_filter->getData();
- if (! $record instanceof Solar_Sql_Model_Record) {
- throw $this->_exception('ERR_NOT_MODEL_RECORD');
- }
-
- $key = $this->_filter->getDataKey();
- $key = substr($key, 1);
-
- $native = $record->getModel();
- if(empty($native->related[$key])) {
- throw $this->_exception('ERR_NOT_RELATED_VANLUE');
- }
-
- $related = $native->getRelated($key);
- $foreign = $related->getModel();
-
- settype($where, 'array');
-
- // the column we're validating as unique. what we do is select the
- // current value from the database, and if it's there, that means
- // the current value is not unique. we'll add exclusion conditions
- // below.
- $where[] = "$col = :$col";
-
- // what is the primary-key column for the record model?
- $primary = $foreign->primary_col;
-
- // only add a primary key exclusion if the column being validated
- // is not itself the primary key.
- if ($col != $primary) {
- // exclude the current record by its primary key value.
- if (! isset($value[$primary])) {
- $where[] = "$primary IS NOT NULL";
- } else {
- $where[] = "$primary != :$primary";
- }
- }
- if($cache_col) {
- $result = $foreign->fetchOne(array(
- 'where' => $where,
- 'cols' => array($primary, $cache_col),
- 'bind' => $value,
- ));
- }else {
- // see if we can fetch a row, with only the primary-key column to
- // reduce resource usage.
- $result = $foreign->fetchOne(array(
- 'where' => $where,
- 'cols' => array($primary),
- 'bind' => $value,
- ));
- }
-
- $old = $record->$key;
- // if empty, no result was returned, so the value haven't been set , and need insert
- if(empty($result)) {
- if($cache_col) {
- $value[$cache_col] = 1;
- if(isset($old->$primary)) {
- //count down the old
- $foreign->update(
- array($cache_col => (int)$old->$cache_col - 1),
- array("$primary = ?" => $old->$primary)
- );
- }
- }
- $id = $foreign->insert($value);
- $native_col = $related->native_col;
- $record[$native_col] = $id;
- //if already exist
- }elseif ($cache_col) {
-
- if($old->$primary !== $result->$primary){
- //count down the old
- $foreign->update(
- array($cache_col => (int)$old->$cache_col - 1),
- array("$primary = ?" => $old->$primary)
- );
-
- //count up the new
- $foreign->update(
- array($cache_col => (int)$result->$cache_col + 1),
- array("$primary = ?" => $result->$primary)
- );
- $native_col = $related->native_col;
- $record[$native_col] = $result->$primary;
- }
- }
-
- // noneed to retain the sanitized value to the related key
- // see Solar_Sql_Model_Record::_save
- // http://php.net/manual/en/function.empty.php
- return null;
- }
- }