_id
stringlengths
2
7
title
stringlengths
3
151
partition
stringclasses
3 values
text
stringlengths
83
13k
language
stringclasses
1 value
meta_information
dict
q263400
Collection.get_model
test
public function get_model( $pk ) { $i = $this->get_model_index( $pk ); if ( $i === - 1 ) { return null; } return $this->elements[ $i ]; }
php
{ "resource": "" }
q263401
Collection.remove_model
test
public function remove_model( $pk ) { $i = $this->get_model_index( $pk ); if ( $i === - 1 ) { return false; } $this->remove( $i ); return true; }
php
{ "resource": "" }
q263402
Collection.get_model_index
test
protected function get_model_index( $pk ) { if ( isset( $this->pk_map[ $pk ] ) ) { return $this->pk_map[ $pk ]; } foreach ( $this->elements as $i => $element ) { if ( $this->saver->get_pk( $element ) === $pk ) { $this->pk_map[ $pk ] = $i; return $i; } } return - 1; }
php
{ "resource": "" }
q263403
Collection.save
test
public function save( array $options = array() ) { foreach ( $this->elements as $element ) { $this->saver->save( $element, $options ); } }
php
{ "resource": "" }
q263404
InternalQueryHelper.prefixWhereWithTable
test
public function prefixWhereWithTable($data, $tableName) { if (count($data) == 0) { return $data; } foreach ($data as $key => $v) { $newKey = $tableName . '.' . trim($key, '`'); $data[$newKey] = $v; unset($data[$key]); } return $data; }
php
{ "resource": "" }
q263405
InternalQueryHelper.prefixOrderWithTable
test
public function prefixOrderWithTable($data, $tableName) { if (count($data) == 0) { return $data; } foreach ($data as $key => $v) { // if $v is empty, we have a custom order like RAND() and do not have to prefix if (!empty($v)) { $newKey = $tableName . '.' . trim($key, '`'); $data[$newKey] = $v; unset($data[$key]); } } return $data; }
php
{ "resource": "" }
q263406
BaseColumn.get_definition_without_column_name
test
protected function get_definition_without_column_name( array $exclude_options = array() ) { $definition = $this->get_mysql_type(); if ( $this->type_options ) { $definition .= '(' . implode( ',', $this->type_options ) . ')'; } if ( $this->options ) { $options = array_udiff( $this->options, $exclude_options, function ( $a, $b ) { return strcmp( strtolower( $a ), strtolower( $b ) ); } ); $definition .= ' ' . implode( ' ', $options ); } return $definition; }
php
{ "resource": "" }
q263407
Manager.register
test
public static function register( Table $table, $complex_query_class = '', $model_class = '' ) { if ( $complex_query_class && ! is_subclass_of( $complex_query_class, 'IronBound\DB\Query\Complex_Query' ) ) { throw new \InvalidArgumentException( '$complex_query_class must subclass Complex_Query' ); } if ( $model_class && ! is_subclass_of( $model_class, 'IronBound\DB\Model' ) ) { throw new \InvalidArgumentException( '$model_class must subclass Model.' ); } static::$tables[ $table->get_slug() ] = array( 'table' => $table, 'query' => $complex_query_class, 'model' => $model_class ); /** @var \wpdb $wpdb */ global $wpdb; $name = $table->get_table_name( $wpdb ); $name = preg_replace( "/^{$wpdb->prefix}/", "", $name ); $wpdb->{$name} = $table->get_table_name( $wpdb ); $wpdb->tables[] = $name; static::fire_plugin_event( $table, 'registered' ); }
php
{ "resource": "" }
q263408
Manager.make_simple_query_object
test
public static function make_simple_query_object( $slug, \wpdb $wpdb = null ) { $table = static::get( $slug ); $wpdb = $wpdb ?: $GLOBALS['wpdb']; if ( $table ) { return new Simple_Query( $wpdb, $table ); } else { return null; } }
php
{ "resource": "" }
q263409
Manager.make_complex_query_object
test
public static function make_complex_query_object( $slug, array $args = array() ) { $table = static::get( $slug ); if ( empty( $table ) || empty( static::$tables[ $slug ]['query'] ) ) { return null; } $class = static::$tables[ $slug ]['query']; $query = new $class( $args ); return $query; }
php
{ "resource": "" }
q263410
Manager.get_model
test
public static function get_model( $slug ) { $slug = $slug instanceof Table ? $slug->get_slug() : $slug; if ( isset( static::$tables[ $slug ] ) ) { return static::$tables[ $slug ]['model']; } else { return null; } }
php
{ "resource": "" }
q263411
Manager.maybe_install_table
test
public static function maybe_install_table( Table $table, \wpdb $wpdb = null ) { $wpdb = $wpdb ?: $GLOBALS['wpdb']; $installed = (int) get_option( $table->get_table_name( $wpdb ) . '_version', 0 ); if ( $installed >= $table->get_version() ) { return false; } if ( $installed === 0 ) { if ( ! static::is_table_installed( $table, $wpdb ) ) { $prev = $wpdb->show_errors( false ); $wpdb->query( $table->get_creation_sql( $wpdb ) ); $wpdb->show_errors( $prev ); if ( $wpdb->last_error ) { throw new Exception( $wpdb->last_error ); } static::fire_plugin_event( $table, 'installed' ); } } else { $update = $installed + 1; while ( $update <= $table->get_version() ) { if ( method_exists( $table, "v{$update}_schema_update" ) ) { $method = "v{$update}_schema_update"; $table->{$method}( $wpdb, $installed ); static::fire_plugin_event( $table, 'updated_schema', array( $update, $installed ) ); } $update += 1; } static::fire_plugin_event( $table, 'updated' ); } update_option( $table->get_table_name( $wpdb ) . '_version', $table->get_version() ); return true; }
php
{ "resource": "" }
q263412
Manager.maybe_uninstall_table
test
public static function maybe_uninstall_table( Table $table, \wpdb $wpdb = null ) { $wpdb = $wpdb ?: $GLOBALS['wpdb']; if ( static::is_table_installed( $table, $wpdb ) ) { $tn = $table->get_table_name( $wpdb ); $wpdb->query( "DROP TABLE IF EXISTS `{$tn}`" ); static::fire_plugin_event( $table, 'deleted' ); delete_option( $tn . '_version' ); return true; } return false; }
php
{ "resource": "" }
q263413
Manager.is_table_installed
test
public static function is_table_installed( Table $table, \wpdb $wpdb = null ) { $wpdb = $wpdb ?: $GLOBALS['wpdb']; $name = $table->get_table_name( $wpdb ); $results = $wpdb->get_results( "SHOW TABLES LIKE '$name'" ); return is_array( $results ) && count( $results ) > 0; }
php
{ "resource": "" }
q263414
Manager.fire_plugin_event
test
protected static function fire_plugin_event( Table $table, $event, $args = array() ) { foreach ( static::$plugins as $plugin ) { if ( $plugin->accepts( $table ) ) { if ( method_exists( $plugin, $event ) ) { $args = array( $table ) + $args; call_user_func_array( array( $plugin, $event ), $args ); } } } }
php
{ "resource": "" }
q263415
PostSaver.do_save
test
protected function do_save( \WP_Post $post ) { if ( ! $post->ID ) { $id = wp_insert_post( wp_slash( $post->to_array() ), true ); } else { $id = wp_update_post( $post, true ); } if ( is_wp_error( $id ) ) { throw new \InvalidArgumentException( 'Error encountered while saving WP_Post: ' . $id->get_error_message() ); } return get_post( $id ); }
php
{ "resource": "" }
q263416
SimpleForeign.get_column
test
protected function get_column() { $column = $this->foreign_column ?: $this->foreign_table->get_primary_key(); $columns = $this->foreign_table->get_columns(); return $columns[ $column ]; }
php
{ "resource": "" }
q263417
InMemoryTable.parse_defaults
test
protected function parse_defaults( array $columns ) { foreach ( $columns as $column_name => $column ) { if ( array_key_exists( $column_name, $this->defaults ) ) { continue; } switch ( $column->get_mysql_type() ) { case 'TINYINT': case 'SMALLINT': case 'MEDIUMINT': case 'INT': case 'BIGINT': $this->defaults[ $column_name ] = 0; break; case 'FLOAT': case 'DOUBLE': case 'DECIMAL': $this->defaults[ $column_name ] = 0.0; break; case 'DATE': case 'DATETIME': $this->defaults[ $column_name ] = null; break; default: $this->defaults[ $column_name ] = ''; break; } } }
php
{ "resource": "" }
q263418
FluentQuery.from_model
test
public static function from_model( $model ) { $query = new static( $model::table(), $GLOBALS['wpdb'] ); $query->model = $model; return $query; }
php
{ "resource": "" }
q263419
FluentQuery.select
test
public function select( $columns ) { if ( $columns === Select::ALL ) { $this->select->all( $this->alias ); return $this; } if ( ! is_array( $columns ) ) { $columns = func_get_args(); } foreach ( $columns as $column ) { $this->select->also( $this->prepare_column( $column ) ); } $this->select->also( $this->prepare_column( $this->table->get_primary_key() ) ); $this->focused_select = true; return $this; }
php
{ "resource": "" }
q263420
FluentQuery.select_single
test
public function select_single( $column ) { $this->select->also( $this->prepare_column( $column ) ); $this->select->also( $this->prepare_column( $this->table->get_primary_key() ) ); $this->select_single = true; return $this; }
php
{ "resource": "" }
q263421
FluentQuery.select_all
test
public function select_all( $local_only = true ) { $this->select->all( $local_only ? $this->alias : null ); return $this; }
php
{ "resource": "" }
q263422
FluentQuery.expression
test
public function expression( $function, $column, $as ) { $this->select->expression( $function, $this->prepare_column( $column ), $as ); $this->expressions[] = $as; return $this; }
php
{ "resource": "" }
q263423
FluentQuery.where
test
public function where( $column, $equality = '', $value = '', Closure $callback = null, $boolean = null ) { if ( $equality ) { $this->assert_comparator( $equality ); } if ( is_array( $column ) ) { $where = Where::for_clause(); foreach ( $column as $col => $val ) { $where->qAnd( $this->generate_where_tag( $col, true, $val ) ); } } else { $where = $this->generate_where_tag( $column, $equality, $value ); } if ( $callback ) { $_where = $this->where; $this->where = $where; $callback( $this ); $this->where = $_where; } if ( $this->where->is_empty() && ! $where->is_for_clause() ) { $this->where = $where; } else { $boolean = $boolean ?: 'and'; $boolean = 'q' . ucfirst( $boolean ); $this->where->{$boolean}( $where ); } return $this; }
php
{ "resource": "" }
q263424
FluentQuery.generate_where_tag
test
protected function generate_where_tag( $column, $equality = '', $value = '' ) { if ( $column instanceof Where ) { return $column; } if ( is_array( $value ) ) { if ( count( $value ) === 0 ) { throw new \InvalidArgumentException( 'Must provide at least one value for IN query.' ); } $self = $this; $value = array_map( function ( $value ) use ( $column, $self ) { return $self->escape_value( $column, $value ); }, $value ); } else { $value = $this->escape_value( $column, $value ); } $column = $this->prepare_column( $column ); return new Where( $column, $equality, $value ); }
php
{ "resource": "" }
q263425
FluentQuery.add_nested_where
test
public function add_nested_where( Closure $callback, $boolean = 'and' ) { $_where = $this->where; $this->where = Where::for_clause(); $callback( $this ); $clause = $this->where; $this->where = $_where; if ( $clause->is_empty() ) { return; } $boolean = $boolean ?: 'and'; $boolean = 'q' . ucfirst( $boolean ); $this->where->{$boolean}( $clause ); }
php
{ "resource": "" }
q263426
FluentQuery.where_joined
test
public function where_joined( Table $table, Closure $callback, $boolean = 'and' ) { $found = false; foreach ( $this->joined_tables as $alias => $maybe_table ) { if ( $maybe_table->get_slug() === $table->get_slug() ) { $found = $alias; break; } } if ( ! $found ) { throw new \InvalidArgumentException( "Table {$table->get_slug()} has not already been joined to this table." ); } $other_query = new FluentQuery( $table, $this->wpdb ); $other_query->alias = $found; $other_query->alias_count = $this->alias_count + 1; $other_query->where = Where::for_clause(); $other_query->joined_tables = $this->joined_tables; $callback( $other_query ); $boolean = 'q' . ucfirst( $boolean ); $this->where->{$boolean}( $other_query->where ); }
php
{ "resource": "" }
q263427
FluentQuery.or_where
test
public function or_where( $column, $equality = '', $value = '', Closure $callback = null ) { return $this->where( $column, $equality, $value, $callback, 'or' ); }
php
{ "resource": "" }
q263428
FluentQuery.where_date
test
public function where_date( $query, $column, Closure $callback = null, $boolean = 'and' ) { $query = new \WP_Date_Query( $query, $this->prepare_column( $column ) ); return $this->where( new Where_Date( $query ), '', '', $callback, $boolean ); }
php
{ "resource": "" }
q263429
FluentQuery.where_meta
test
public function where_meta( $query, MetaTable $table = null, $meta_type = '' ) { if ( ! $table && $this->model && method_exists( $this->model, 'get_meta_table' ) ) { $table = call_user_func( array( $this->model, 'get_meta_table' ) ); } if ( ! $meta_type && $this->model && method_exists( $this->model, 'get_meta_type' ) ) { $meta_type = call_user_func( array( $this->model, 'get_meta_type' ) ); } if ( ! $table ) { throw new \InvalidArgumentException( "MetaTable can't be determined from the given arguments." ); } if ( ! $meta_type ) { throw new \InvalidArgumentException( "\$meta_type can't be determined from the given arguments." ); } if ( ! $query instanceof \WP_Meta_Query ) { $query = new \WP_Meta_Query( $query ); } $fn = function ( $key, $original ) use ( $table, $meta_type ) { if ( $original === $meta_type . '_id' ) { $key = $table->get_primary_id_column(); } return $key; }; add_filter( 'sanitize_key', $fn, 10, 2 ); $sql = $query->get_sql( $meta_type, $this->alias, $this->table->get_primary_key() ); remove_filter( 'sanitize_key', $fn, 10 ); $this->meta_join = $sql['join']; $where = $sql['where']; $where = preg_replace( '/^\sAND\s/', '', $where ); if ( $this->where ) { $this->where->qAnd( new Where_Raw( $where ) ); } else { $this->where = new Where_Raw( $where ); } return $this; }
php
{ "resource": "" }
q263430
FluentQuery.order_by
test
public function order_by( $column, $direction = null ) { $column = $this->prepare_column( $column ); if ( is_null( $this->order ) ) { $this->order = new Order( $column, $direction ); } else { $this->order->then( $column, $direction ); } return $this; }
php
{ "resource": "" }
q263431
FluentQuery.order_by_expression
test
public function order_by_expression( $expression_alias, $direction = null ) { if ( ! in_array( $expression_alias, $this->expressions, true ) ) { throw new InvalidColumnException( 'Cannot order by expression alias because the alias has not been used.' ); } if ( is_null( $this->order ) ) { $this->order = new Order( $expression_alias, $direction ); } else { $this->order->then( $expression_alias, $direction ); } return $this; }
php
{ "resource": "" }
q263432
FluentQuery.group_by
test
public function group_by( $column ) { $column = $this->prepare_column( $column ); if ( is_null( $this->group ) ) { $this->group = new Group( $column ); } else { $this->group->then( $column ); } return $this; }
php
{ "resource": "" }
q263433
FluentQuery.group_by_expression
test
public function group_by_expression( $function, $column ) { $column = $this->prepare_column( $column ); $group = "{$function}($column)"; if ( is_null( $this->group ) ) { $this->group = new Group( $group ); } else { $this->group->then( $group ); } return $this; }
php
{ "resource": "" }
q263434
FluentQuery.join
test
public function join( Table $table, $this_column, $other_column, $comparator = '=', $callback = null, $type = 'INNER' ) { $this->assert_comparator( $comparator ); $other_alias = 't' . ( ++ $this->alias_count ); $other_query = new FluentQuery( $table, $this->wpdb ); $other_query->alias = $other_alias; $other_query->alias_count = $this->alias_count + 1; $other_query->joined_tables = $this->joined_tables; $other_query->joined_tables[ $this->alias ] = $this->table; $from = new From( $table->get_table_name( $this->wpdb ), $other_alias ); $where = new Where_Raw( "{$this->prepare_column( $this_column )} $comparator {$other_query->prepare_column( $other_column )}" ); if ( $callback ) { $callback( $other_query ); if ( ! $other_query->where->is_empty() ) { $where->qAnd( $other_query->where ); } } $this->joins[] = new Join( $from, $where, $type ); $this->joined_tables[ $other_alias ] = $table; return $this; }
php
{ "resource": "" }
q263435
FluentQuery.join_correlated_subquery
test
public function join_correlated_subquery( Table $table, $this_column, $comparator, $other_column, Closure $callback, $type = 'INNER' ) { $this->assert_comparator( $comparator ); $other_alias = 't' . ( ++ $this->alias_count ); $other_query = new FluentQuery( $table, $this->wpdb ); $other_query->from = new From( $table->get_table_name( $this->wpdb ), $other_alias ); $other_query->alias = $other_alias; $other_query->alias_count = $this->alias_count + 1; $other_query->joined_tables = $this->joined_tables; $other_query->joined_tables[ $this->alias ] = $this->table; $other_query->select->also( $other_query->prepare_column( $other_column ) ); $other_query->take( 1 ); $callback( $other_query ); $from = new From( $table->get_table_name( $this->wpdb ), $other_alias ); $where = new Where_Raw( "{$this->prepare_column( $this_column )} $comparator ({$other_query->build_sql()})" ); $this->joins[] = new Join( $from, $where, $type ); $this->joined_tables[ $other_alias ] = $table; return $this; }
php
{ "resource": "" }
q263436
FluentQuery.paginate
test
public function paginate( $page, $per_page ) { $this->count = $per_page; $this->offset = $per_page * ( $page - 1 ); $this->calc_found_rows = true; return $this; }
php
{ "resource": "" }
q263437
FluentQuery.each
test
public function each( $number, $callback ) { $this->offset = 0; $this->count = $number; $query = clone $this; do { $_query = clone $query; $results = $query->results(); foreach ( $results as $result ) { if ( $callback( $result ) === false ) { return false; } } $_query->offset += $number; $query = $_query; } while ( $results->count() === $number ); return true; }
php
{ "resource": "" }
q263438
FluentQuery.with
test
public function with( $relations, $callback = null ) { $default = null; if ( is_string( $relations ) ) { $relations = func_get_args(); if ( func_num_args() === 2 && $relations[1] instanceof Closure ) { $relations = array( $relations[0] => $relations[1], ); } } $parsed = array(); foreach ( $relations as $relation => $callback ) { if ( ! $callback instanceof Closure ) { $relation = $callback; $callback = $default; } if ( strpos( $relation, '.' ) !== false ) { $parsed = $this->parse_nested_with( $relation, $parsed ); } else { $parsed[ $relation ] = $callback; } } $this->relations = $parsed; return $this; }
php
{ "resource": "" }
q263439
FluentQuery.parse_nested_with
test
protected function parse_nested_with( $name, $results ) { $parts = explode( '.', $name ); $first = $parts[0]; $results[ $first ] = false; $this->assign_array_by_path( $results, $name, true ); return $results; }
php
{ "resource": "" }
q263440
FluentQuery.assign_array_by_path
test
protected function assign_array_by_path( &$arr, $path, $value ) { $keys = explode( '.', $path ); foreach ( $keys as $key ) { $arr = &$arr[ $key ]; } $arr = $value; }
php
{ "resource": "" }
q263441
FluentQuery.make_limit_tag
test
protected function make_limit_tag() { if ( ! $this->count ) { return $this; } $this->limit = new Limit( $this->count, $this->offset ); return $this; }
php
{ "resource": "" }
q263442
FluentQuery.build_sql
test
protected function build_sql() { $this->make_limit_tag(); $builder = new Builder(); if ( ! $this->select->is_all() && ! $this->select->get_columns() ) { $this->select->all( $this->alias ); } $this->select->calc_found_rows( $this->calc_found_rows ); $builder->append( $this->select ); $builder->append( $this->from ); foreach ( $this->joins as $join ) { $builder->append( $join ); } if ( $this->meta_join ) { $builder->append( new Generic( '', $this->meta_join ) ); } if ( $this->where ) { $builder->append( $this->where ); } if ( $this->group ) { $builder->append( $this->group ); } if ( $this->having ) { $builder->append( $this->having ); } if ( $this->order ) { $builder->append( $this->order ); } if ( $this->limit ) { $builder->append( $this->limit ); } return $builder->build(); }
php
{ "resource": "" }
q263443
FluentQuery.update_meta_cache
test
protected function update_meta_cache() { $ids = $this->results->getKeys(); $table = $this->meta_table ?: call_user_func( array( $this->model, 'get_meta_table' ) ); $meta_type = $this->meta_type ?: call_user_func( array( $this->model, 'get_meta_type' ) ); $fn = function ( $key, $original ) use ( $table, $meta_type ) { if ( $original === $meta_type . '_id' ) { $key = $table->get_primary_id_column(); } return $key; }; add_filter( 'sanitize_key', $fn, 10, 2 ); update_meta_cache( $meta_type, $ids ); remove_filter( 'sanitize_key', $fn ); }
php
{ "resource": "" }
q263444
FluentQuery.find
test
public function find( $primary_key ) { if ( is_array( $primary_key ) ) { return $this->find_many( $primary_key ); } return $this->where( $this->table->get_primary_key(), true, $primary_key )->first(); }
php
{ "resource": "" }
q263445
FluentQuery.find_many
test
public function find_many( array $primary_keys ) { return $this->where( $this->table->get_primary_key(), true, $primary_keys )->results(); }
php
{ "resource": "" }
q263446
FluentQuery.find_or_fail
test
public function find_or_fail( $primary_key ) { $result = $this->find( $primary_key ); if ( is_array( $primary_key ) ) { if ( count( $result ) == count( array_unique( $primary_key ) ) ) { return $result; } } elseif ( $result ) { return $result; } throw new ModelNotFoundException( "No model found for '$primary_key'." ); }
php
{ "resource": "" }
q263447
FluentQuery.find_or_new
test
public function find_or_new( $primary_key ) { $model = $this->find( $primary_key ); if ( is_null( $model ) ) { $model = new $this->model; } return $model; }
php
{ "resource": "" }
q263448
FluentQuery.first_or_new
test
public function first_or_new( array $attributes ) { $model = $this->where( $attributes )->first(); if ( $model ) { return $model; } return new $this->model( (object) $attributes ); }
php
{ "resource": "" }
q263449
FluentQuery.first_or_create
test
public function first_or_create( array $attributes ) { $model = $this->where( $attributes )->first(); if ( $model ) { return $model; } $model = new $this->model( (object) $attributes ); $model->save(); return $model; }
php
{ "resource": "" }
q263450
FluentQuery.update_or_create
test
public function update_or_create( array $attributes, array $values ) { $model = $this->first_or_new( $attributes ); $model->fill( $values )->save(); return $model; }
php
{ "resource": "" }
q263451
FluentQuery.handle_eager_loading
test
protected function handle_eager_loading( $models ) { /** @var Model $model */ $model = new $this->model; foreach ( $this->relations as $relation => $customize_callback ) { if ( is_array( $customize_callback ) ) { $loaded = $model->get_relation( $relation )->eager_load( $models ); $this->do_nested_eager_load( $loaded, $relation, $customize_callback ); } else { $model->get_relation( $relation )->eager_load( $models, $customize_callback ); } } }
php
{ "resource": "" }
q263452
FluentQuery.do_nested_eager_load
test
protected function do_nested_eager_load( Collection $loaded, $relation, $nested ) { $model = $loaded->first(); foreach ( $nested as $value => $ignore ) { if ( is_string( $value ) ) { if ( $model instanceof Model ) { $model->get_relation( $value )->eager_load( $loaded->toArray() ); } return; } else { if ( ! $model instanceof Model ) { return; } $loaded = $model->get_relation( $relation )->eager_load( $loaded->toArray() ); $this->do_nested_eager_load( $loaded, $nested, $value ); } } }
php
{ "resource": "" }
q263453
FluentQuery.assert_comparator
test
protected function assert_comparator( $operator ) { if ( is_bool( $operator ) ) { return; } if ( in_array( $operator, array( '=', '!=', '>', '<', '>=', '<=', '<=>', '<>', 'LIKE', 'BETWEEN', 'COALESCE', 'GREATEST', 'IN', 'INTERVAL', 'IS', 'IS NOT', 'IS NOT NULL', 'IS NULL', 'ISNULL', 'LEAST', 'NOT BETWEEN', 'NOT IN', 'NOT LIKE', ), true ) ) { return; } throw new \InvalidArgumentException( sprintf( 'Invalid SQL operator % s . ', $operator ) ); }
php
{ "resource": "" }
q263454
FluentQuery.prepare_column
test
public function prepare_column( $column ) { $columns = $this->table->get_columns(); if ( ! isset( $columns[ $column ] ) ) { throw new InvalidColumnException( "Invalid database column '$column'." ); } return "{$this->alias}.`{$column}`"; }
php
{ "resource": "" }
q263455
FluentQuery.escape_value
test
public function escape_value( $column, $value ) { $columns = $this->table->get_columns(); if ( ! isset( $columns[ $column ] ) ) { throw new InvalidColumnException( "Invalid database column '$column'." ); } if ( is_null( $value ) ) { return null; } if ( empty( $value ) ) { return ''; } return esc_sql( $columns[ $column ]->prepare_for_storage( $value ) ); }
php
{ "resource": "" }
q263456
PicORM.configure
test
final public static function configure(array $configuration) { // override with default configuration if not present $configuration += static::$_defaultConfiguration; // test if datasource is a PDO instance if ($configuration['datasource'] === null || !$configuration['datasource'] instanceof \PDO) { throw new Exception("PDO Datasource is required!"); } // set global datasource for all model static::$_dataSource = $configuration['datasource']; Model::setDataSource(static::$_dataSource); // store PicORM configuration static::$_configuration = $configuration; }
php
{ "resource": "" }
q263457
Builder.get_col_value
test
protected final function get_col_value( $col ) { if ( ! isset( $this->data[ $col ] ) ) { throw new \InvalidArgumentException( "Column does not exist." ); } return $this->data[ $col ]; }
php
{ "resource": "" }
q263458
Builder.create
test
protected function create() { foreach ( $this->data as $col => $val ) { if ( method_exists( $this, "validate_$col" ) ) { $this->data[ $col ] = $this->{"validate_$col"}( $val ); } } return Manager::make_simple_query_object( $this->get_table()->get_slug() ) ->insert( $this->data ); }
php
{ "resource": "" }
q263459
Order.then
test
public function then( $col, $direction = null ) { if ( $this->is_rand ) { throw new \LogicException( "This ORDER BY statement is already RAND()." ); } $this->orders[ $col ] = $direction; return $this; }
php
{ "resource": "" }
q263460
Order.add_order
test
protected function add_order( $col, $direction ) { if ( $direction !== null && ! in_array( $direction, array( self::ASC, self::DESC ) ) ) { throw new \InvalidArgumentException( "Invalid ORDER BY direction." ); } $this->orders[ $col ] = $direction; }
php
{ "resource": "" }
q263461
Select.all
test
public function all( $as = null ) { $this->all_columns = true; if ( $as ) { $this->all_as = $as; } return $this; }
php
{ "resource": "" }
q263462
HasOneOrMany.build_eager_load_map
test
protected function build_eager_load_map( $models ) { $map = array(); $foreign = $this->foreign_key; foreach ( $models as $model ) { $pk = $model->get_raw_attribute( $foreign ); if ( $pk instanceof Model ) { $pk = $pk->get_pk(); } $map[ $pk ][ $model->get_pk() ] = $model; } return $map; }
php
{ "resource": "" }
q263463
CommentSaver.do_save
test
protected function do_save( $comment ) { if ( ! $comment->comment_ID ) { $id = wp_insert_comment( wp_slash( $comment->to_array() ) ); } else { if ( wp_update_comment( wp_slash( $comment->to_array() ) ) ) { $id = $comment->comment_ID; } } if ( empty( $id ) ) { throw new \InvalidArgumentException( 'Error encountered while saving WP_Comment.' ); } return get_comment( $id ); }
php
{ "resource": "" }
q263464
Complex_Query.get_default_arg
test
protected function get_default_arg( $arg ) { $args = $this->get_default_args(); if ( isset( $args[ $arg ] ) ) { return $args[ $arg ]; } else { throw new \InvalidArgumentException(); } }
php
{ "resource": "" }
q263465
Complex_Query.query
test
protected function query() { $results = $this->wpdb->get_results( $this->sql ); // we query for found rows first to prevent instantiation of record objects from interfering with the count if ( $this->args['sql_calc_found_rows'] ) { $count_results = $this->wpdb->get_results( "SELECT FOUND_ROWS() AS COUNT" ); if ( empty( $count_results ) || empty( $count_results[0] ) ) { $this->total_items = 0; } else { $this->total_items = $count_results[0]->COUNT; } } elseif ( $this->args['return_value'] == 'count' ) { if ( empty( $results ) || empty( $results[0] ) ) { $this->total_items = 0; } else { $this->total_items = $results[0]->COUNT; } } $this->results = $this->parse_results( $results ); }
php
{ "resource": "" }
q263466
Complex_Query.parse_results
test
protected function parse_results( $results ) { if ( is_array( $this->args['return_value'] ) ) { return $results; } elseif ( $this->args['return_value'] == 'count' ) { if ( empty( $results ) || empty( $results[0] ) ) { return 0; } return $results[0]->COUNT; } elseif ( $this->args['return_value'] != 'object' ) { $values = array(); $field = $this->args['return_value']; foreach ( $results as $result ) { $values[] = $result->$field; } return $values; } else { $records = array(); foreach ( $results as $result ) { $object = $this->make_object( $result ); Cache::update( $object ); $records[ $result->{$this->table->get_primary_key()} ] = $object; } return $records; } }
php
{ "resource": "" }
q263467
Complex_Query.parse_select
test
protected function parse_select( $alias = 'q' ) { if ( is_array( $this->args['return_value'] ) ) { $select = new Select( null ); foreach ( $this->args['return_value'] as $column ) { $select->also( "$alias.$column" ); } } elseif ( $this->args['return_value'] == 'count' ) { $select = new Select( 'COUNT(1)', 'COUNT' ); } elseif ( $this->args['return_value'] != 'object' ) { $select = new Select( "$alias." . $this->args['return_value'] ); } else { $select = new Select( "$alias.*" ); } if ( $this->args['sql_calc_found_rows'] ) { $select->calc_found_rows(); } $select->filter_distinct( $this->args['distinct'] ); return $select; }
php
{ "resource": "" }
q263468
Complex_Query.parse_order
test
protected function parse_order( $alias = 'q' ) { if ( ! is_array( $this->args['order'] ) && $this->args['order'] === 'rand' ) { return new Order( Order::RAND ); } elseif ( ! is_array( $this->args['order'] ) ) { throw new \InvalidArgumentException( "Order must either be 'rand' or an array of columns to directions." ); } $white_list = $this->table->get_columns(); foreach ( $this->args['order'] as $column => $direction ) { $direction = strtoupper( $direction ); if ( ! in_array( $direction, array( Order::ASC, Order::DESC ) ) ) { throw new \InvalidArgumentException( "Invalid order direction $direction for column $column." ); } $column = $this->translate_order_by_to_column_name( $column ); if ( ! isset( $white_list[ $column ] ) ) { throw new \InvalidArgumentException( "Invalid order column $column." ); } $column = "{$alias}.$column"; if ( ! isset( $order ) ) { $order = new Order( $column, $direction ); } else { $order->then( $column, $direction ); } } if ( isset( $order ) ) { return $order; } else { return new Order( "{$alias}.{$this->table->get_primary_key()}", Order::ASC ); } }
php
{ "resource": "" }
q263469
Complex_Query.parse_pagination
test
protected function parse_pagination() { if ( $this->args['items_per_page'] == - 1 ) { return null; } if ( $this->args['page'] < 1 ) { throw new \InvalidArgumentException( "page parameter must be at least 1." ); } $per_page = absint( $this->args['items_per_page'] ); $page = absint( $this->args['page'] ); $count = $per_page; $offset = $per_page * ( $page - 1 ); return new Limit( $count, $offset ); }
php
{ "resource": "" }
q263470
Saver.numerically_equivalent
test
protected function numerically_equivalent( $a, $b ) { return is_numeric( $a ) && is_numeric( $b ) && strcmp( (string) $a, (string) $b ) === 0; }
php
{ "resource": "" }
q263471
Saver.has_changes
test
protected function has_changes( $old, $new ) { foreach ( $new as $key => $value ) { if ( $value !== $old[ $key ] && ! $this->numerically_equivalent( $value, $old[ $key ] ) ) { return true; } } return false; }
php
{ "resource": "" }
q263472
Collection.fetch
test
public function fetch() { $modelName = $this->_className; // execute fetch query $query = $this->_dataSource->prepare($this->_queryHelper->buildQuery()); $query->execute($this->_queryHelper->getWhereParamsValues()); // check for mysql error $errorcode = $query->errorInfo(); if ($errorcode[0] != "00000") { throw new Exception($errorcode[2]); } // fetch query and hydrate models $fetch = $query->fetchAll(\PDO::FETCH_ASSOC); foreach ($fetch as &$unRes) { /** @var $object \PicORM\Model */ $object = new $modelName(); $object->hydrate($unRes, false); $unRes = $object; } // configure collection after fetch $this->isFetched = true; $this->models = $fetch; // if pagination used grab the total found model if ($this->_usePagination) { $this->_paginationFoundModels = $this->queryFoundModels(); } return $this; }
php
{ "resource": "" }
q263473
Collection.delete
test
public function delete() { $modelClass = $this->_className; // cloning fetch query to get where,order by and limit values $deleteQuery = clone($this->_queryHelper); // transform query to delete $deleteQuery->cleanQueryBeforeSwitching() ->delete($modelClass::formatTableNameMySQL()); // execute query $query = $this->_dataSource->prepare($deleteQuery->buildQuery()); $query->execute($deleteQuery->getWhereParamsValues()); // check for mysql error $errorcode = $query->errorInfo(); if ($errorcode[0] != "00000") { throw new Exception($errorcode[2]); } return true; }
php
{ "resource": "" }
q263474
Collection.update
test
public function update(array $setValues) { $modelClass = $this->_className; // cloning fetch query to get where,order by and limit values $updateQuery = clone($this->_queryHelper); // transform query to update $updateQuery->cleanQueryBeforeSwitching() ->update($modelClass::formatTableNameMySQL()); // build set values $params = array(); foreach ($setValues as $fieldName => $value) { $updateQuery->set($fieldName, '?'); $params[] = $value; } // merge set values with where values $params = array_merge($params, $updateQuery->getWhereParamsValues()); // execute query $query = $this->_dataSource->prepare($updateQuery->buildQuery()); $query->execute($params); // check for mysql error $errorcode = $query->errorInfo(); if ($errorcode[0] != "00000") { throw new Exception($errorcode[2]); } }
php
{ "resource": "" }
q263475
Collection.getTotalPages
test
public function getTotalPages() { if ($this->_usePagination === false) { return 0; } if (!$this->isFetched) { $this->fetch(); } return (int)ceil($this->_paginationFoundModels / $this->_paginationNbModelByPage); }
php
{ "resource": "" }
q263476
Collection.paginate
test
public function paginate($neededNumPage) { if ($this->_usePagination === false) { return $this; } // build the limit start $limitStart = max(0, $neededNumPage - 1) * $this->_paginationNbModelByPage; // limit fetch query for page $neededNumPage $this->_queryHelper->limit($limitStart, $this->_paginationNbModelByPage); return $this; }
php
{ "resource": "" }
q263477
Collection.queryFoundModels
test
protected function queryFoundModels() { $countQueryHelper = clone($this->_queryHelper); $countQueryHelper->resetSelect("count(*)"); $countQueryHelper->resetOrderBy(); $countQueryHelper->resetLimit(); $query = $this->_dataSource->prepare($countQueryHelper->buildQuery()); $query->execute($countQueryHelper->getWhereParamsValues()); return (int)$query->fetch(\PDO::FETCH_COLUMN); }
php
{ "resource": "" }
q263478
Collection.countModelsWithoutLimit
test
public function countModelsWithoutLimit() { if (!$this->isFetched) { $this->fetch(); } // no pagination, we have to manually count number of model if (!$this->_usePagination) { $this->_paginationFoundModels = $this->queryFoundModels(); } return $this->_paginationFoundModels; }
php
{ "resource": "" }
q263479
Collection.offsetExists
test
public function offsetExists($offset) { if (!$this->isFetched) { $this->fetch(); } return isset($this->models[$offset]); }
php
{ "resource": "" }
q263480
Collection.offsetSet
test
public function offsetSet($offset, $value) { if (!$this->isFetched) { $this->fetch(); } if (is_null($offset)) { $this->models[] = $value; } else { $this->models[$offset] = $value; } }
php
{ "resource": "" }
q263481
Collection.offsetGet
test
public function offsetGet($offset) { if (!$this->isFetched) { $this->fetch(); } return isset($this->models[$offset]) ? $this->models[$offset] : null; }
php
{ "resource": "" }
q263482
Builder.build
test
public function build() { if ( empty( $this->parts ) ) { return ''; } $query = ''; foreach ( $this->parts as $part) { if ($part instanceof Builder) { $query .= "($part)"; } elseif ( $sql = (string) $part ) { $query .= "$part "; } } return $query; }
php
{ "resource": "" }
q263483
Model.boot_if_not_booted
test
protected static function boot_if_not_booted() { if ( isset( static::$_booted[ get_called_class() ] ) ) { return; } static::$_booted[ get_called_class() ] = true; $instance = new static(); $instance->fire_model_event( 'booting' ); static::boot(); $instance->fire_model_event( 'booted' ); }
php
{ "resource": "" }
q263484
Model.boot_traits
test
protected static function boot_traits() { $class = get_called_class(); // this method will return an empty array on PHP < 5.4 $uses = class_uses_recursive( get_called_class() ); foreach ( $uses as $trait ) { if ( method_exists( $class, $method = 'boot_' . class_basename( $trait ) ) ) { forward_static_call( array( $class, $method ) ); } } }
php
{ "resource": "" }
q263485
Model.fill
test
public function fill( array $data = array() ) { foreach ( $data as $column => $value ) { if ( $this->is_fillable( $column ) ) { $this->set_attribute( $column, $value ); } } return $this; }
php
{ "resource": "" }
q263486
Model.with_guarded
test
public function with_guarded( $attribute, $callback ) { if ( is_array( $attribute ) ) { $attributes = $attribute; } else { $attributes = func_get_args(); $callback = array_pop( $attributes ); } $unguarded = static::$_unguarded; if ( $unguarded ) { static::$_unguarded = false; } $_fillable = $this->_fillable; $_guarded = $this->_guarded; if ( $this->_fillable ) { $this->_fillable = array_diff( $this->_fillable, $attributes ); // If the diff removes all fillable attributes we need to use _guarded. if ( ! $this->_fillable ) { $this->_guarded = $attributes; } } else { $this->_guarded = array_unique( array_merge( $this->_guarded, $attributes ) ); } $callback( $this ); $this->_fillable = $_fillable; $this->_guarded = $_guarded; static::$_unguarded = $unguarded; }
php
{ "resource": "" }
q263487
Model.with_unguarded
test
public function with_unguarded( $attribute, $callback ) { if ( static::$_unguarded ) { $callback( $this ); return; } if ( is_array( $attribute ) ) { $attributes = $attribute; } else { $attributes = func_get_args(); $callback = array_pop( $attributes ); } $_fillable = $this->_fillable; $_guarded = $this->_guarded; if ( $this->_fillable ) { $this->_fillable = array_merge( $this->_fillable, $attributes ); } else { $this->_guarded = array_diff( $this->_guarded, $attributes ); } $callback( $this ); $this->_fillable = $_fillable; $this->_guarded = $_guarded; }
php
{ "resource": "" }
q263488
Model.is_fillable
test
protected function is_fillable( $column ) { if ( static::$_unguarded ) { return true; } if ( empty( $this->_fillable ) ) { return ! in_array( $column, $this->_guarded ); } else { return in_array( $column, $this->_fillable ); } }
php
{ "resource": "" }
q263489
Model.set_attribute
test
public function set_attribute( $attribute, $value ) { if ( $this->has_relation( $attribute ) ) { if ( is_object( $value ) ) { $this->set_relation_value( $attribute, $value ); return $this; } unset( $this->_relations[ $attribute ] ); } elseif ( ! array_key_exists( $attribute, static::table()->get_columns() ) ) { throw new \OutOfBoundsException( sprintf( "Requested attribute '%s' does not exist for '%s'.", $attribute, get_class( $this ) ) ); } elseif ( method_exists( $this, $this->get_mutator_method_for_attribute( $attribute ) ) ) { $value = call_user_func( array( $this, $this->get_mutator_method_for_attribute( $attribute ) ), $value ); } $columns = static::table()->get_columns(); $this->set_raw_attribute( $attribute, $columns[ $attribute ]->prepare_for_storage( $value ) ); return $this; }
php
{ "resource": "" }
q263490
Model.set_raw_attribute
test
public function set_raw_attribute( $attribute, $value ) { $attributes = $this->get_raw_attributes(); $attributes[ $attribute ] = $value; $this->set_raw_attributes( $attributes ); return $this; }
php
{ "resource": "" }
q263491
Model.get_attribute
test
public function get_attribute( $attribute ) { if ( $this->has_relation( $attribute ) ) { return $this->get_relation_value( $attribute ); } if ( array_key_exists( $attribute, static::table()->get_columns() ) ) { return $this->get_attribute_value( $attribute ); } throw new \OutOfBoundsException( sprintf( "Requested attribute '%s' does not exist for '%s'.", $attribute, get_class( $this ) ) ); }
php
{ "resource": "" }
q263492
Model.get_raw_attribute
test
public function get_raw_attribute( $attribute ) { $attributes = $this->get_raw_attributes(); return isset( $attributes[ $attribute ] ) ? $attributes[ $attribute ] : null; }
php
{ "resource": "" }
q263493
Model.get_attribute_value
test
protected function get_attribute_value( $attribute ) { $value = $this->get_attribute_from_array( $attribute ); if ( method_exists( $this, $this->get_accessor_method_for_attribute( $attribute ) ) ) { $value = call_user_func( array( $this, $this->get_accessor_method_for_attribute( $attribute ) ), $value ); } return $value; }
php
{ "resource": "" }
q263494
Model.get_attribute_from_array
test
protected function get_attribute_from_array( $attribute ) { $raw = $this->get_raw_attribute( $attribute ); $columns = static::table()->get_columns(); return $columns[ $attribute ]->convert_raw_to_value( $raw ); }
php
{ "resource": "" }
q263495
Model.get_all_relations
test
public function get_all_relations() { if ( isset( static::$_relation_attribute_cache[ get_class( $this ) ] ) ) { return static::$_relation_attribute_cache[ get_class( $this ) ]; } $relations = array(); $methods = get_class_methods( $this ); foreach ( $methods as $method ) { preg_match( '/^_(\S+)_relation$/', $method, $matches ); if ( empty( $matches[1] ) ) { continue; } $relations[] = $matches[1]; $matches = null; } static::$_relation_attribute_cache[ get_class( $this ) ] = $relations; return $relations; }
php
{ "resource": "" }
q263496
Model.get_relation
test
public function get_relation( $attribute ) { if ( ! $this->has_relation( $attribute ) ) { throw new \OutOfBoundsException( sprintf( "Requested relation '%s' does not exist for '%s'.", $attribute, get_class( $this ) ) ); } $method = "_{$attribute}_relation"; $relation = $this->{$method}(); if ( ! $relation instanceof Relation ) { throw new \UnexpectedValueException( 'Relation methods must return an IronBound\DB\Relations\Relation object.' ); } return $relation; }
php
{ "resource": "" }
q263497
Model.set_relation_value
test
public function set_relation_value( $attribute, $value ) { if ( ! $this->has_relation( $attribute ) ) { throw new \OutOfBoundsException( sprintf( "Requested relation '%s' does not exist for '%s'.", $attribute, get_class( $this ) ) ); } $this->_relations[ $attribute ] = $value; return $this; }
php
{ "resource": "" }
q263498
Model.get_relation_value
test
protected function get_relation_value( $attribute ) { if ( array_key_exists( $attribute, $this->_relations ) ) { return $this->_relations[ $attribute ]; } $relation = $this->get_relation( $attribute ); $this->set_relation_value( $attribute, $relation->get_results() ); return $this->get_relation_value( $attribute ); }
php
{ "resource": "" }
q263499
Model.refresh
test
public function refresh( $destroy_changes = false ) { if ( ! $this->exists() ) { return; } $data = (array) static::get_data_from_pk( $this->get_pk() ); $this->_original = $data; if ( $destroy_changes ) { $this->set_raw_attributes( $data ); } }
php
{ "resource": "" }