function test_add_model_relation_chain_onto_where_conditions() { global $current_user; $current_user = $this->factory->user->create_and_get(); $value1 = 12; $value2 = 23; $value3 = array(1, 2, 3); $value4 = 'eee'; $default_where_conditions = new EE_Default_Where_Conditions(); $default_where_conditions->_finalize_construct(EEM_Event::instance()); $this->assertEquals(array('OR*' => array('Event.EVT_ID' => $value1, 'Event.Datetime.DTT_ID' => $value2), 'AND' => array('Event.EVT_name' => array('IN', $value3), 'Event.Datetime.DTT_name' => $value4), 'Event.EVT_wp_user' => $current_user->ID), $default_where_conditions->prepare_where_conditions_for_querying(array('OR*' => array('EVT_ID' => $value1, 'Datetime.DTT_ID' => $value2), 'AND' => array('EVT_name' => array('IN', $value3), 'Datetime.DTT_name' => $value4), EEM_Event::instance()->wp_user_field_name() => EE_Default_Where_Conditions::current_user_placeholder), 'Event.')); }
/** * About all child constructors: * they should define the _tables, _fields and _model_relations arrays. * Should ALWAYS be called after child constructor. * In order to make the child constructors to be as simple as possible, this parent constructor * finalizes constructing all the object's attributes. * Generally, rather than requiring a child to code * $this->_tables = array( * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') * ...); * (thus repeating itself in the array key and in the constructor of the new EE_Table,) * each EE_Table has a function to set the table's alias after the constructor, using * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations * do something similar. */ protected function __construct($timezone = NULL) { //if we're in maintenance mode level 2, DON'T run any queries //because level 2 indicates the database needs updating and //is probably out of sync with the code if (!EE_Maintenance_Mode::instance()->models_can_query()) { throw new EE_Error(sprintf(__("EE Level 2 Maintenance mode is active. That means EE cant run ANY database queries until the necessary migration scripts have run which will take EE out of maintenance mode level 2", "event_espresso"))); } foreach ($this->_tables as $table_alias => $table_obj) { /** @var $table_obj EE_Table_Base */ $table_obj->_construct_finalize_with_alias($table_alias); if ($table_obj instanceof EE_Secondary_Table) { /** @var $table_obj EE_Secondary_Table */ $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); } } $this->_fields = apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); foreach ($this->_fields as $table_alias => $fields_for_table) { if (!array_key_exists($table_alias, $this->_tables)) { throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", 'event_espresso'), $table_alias, implode(",", $this->_fields))); } foreach ($fields_for_table as $field_name => $field_obj) { //primary key field base has a slightly different _construct_finalize /** @var $field_obj EE_Model_Field_Base */ $field_obj->_construct_finalize($table_alias, $field_name); if ($field_obj instanceof EE_Primary_Key_Field_Base) { /** @var $field_obj EE_Primary_Key_Field_Base */ $field_obj->_construct_finalize_set_model_name($this->get_this_model_name()); } } } // everything is related to Extra_Meta if (get_class($this) != 'EEM_Extra_Meta') { $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(); } $this->_model_relations = apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', $this->_model_relations); foreach ($this->_model_relations as $model_name => $relation_obj) { /** @var $relation_obj EE_Model_Relation_Base */ $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); } foreach ($this->_indexes as $index_name => $index_obj) { /** @var $index_obj EE_Index */ $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); } $this->set_timezone($timezone); //finalize default where condition strategy, or set default if (!$this->_default_where_conditions_strategy) { //nothing was set during child constructor, so set default $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); } $this->_default_where_conditions_strategy->_finalize_construct($this); do_action('AHEE__' . get_class($this) . '__construct__end'); }
/** * About all child constructors: * they should define the _tables, _fields and _model_relations arrays. * Should ALWAYS be called after child constructor. * In order to make the child constructors to be as simple as possible, this parent constructor * finalizes constructing all the object's attributes. * Generally, rather than requiring a child to code * $this->_tables = array( * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') * ...); * (thus repeating itself in the array key and in the constructor of the new EE_Table,) * each EE_Table has a function to set the table's alias after the constructor, using * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations * do something similar. * * @param null $timezone * @throws \EE_Error */ protected function __construct($timezone = NULL) { // check that the model has not been loaded too soon if (!did_action('AHEE__EE_System__load_espresso_addons')) { throw new EE_Error(sprintf(__('The %1$s model can not be loaded before the "AHEE__EE_System__load_espresso_addons" hook has been called. This gives other addons a chance to extend this model.', 'event_espresso'), get_class($this))); } /** * Filters the list of tables on a model. It is best to NOT use this directly and instead * just use EE_Register_Model_Extension * @var EE_Table_Base[] $_tables */ $this->_tables = apply_filters('FHEE__' . get_class($this) . '__construct__tables', $this->_tables); foreach ($this->_tables as $table_alias => $table_obj) { /** @var $table_obj EE_Table_Base */ $table_obj->_construct_finalize_with_alias($table_alias); if ($table_obj instanceof EE_Secondary_Table) { /** @var $table_obj EE_Secondary_Table */ $table_obj->_construct_finalize_set_table_to_join_with($this->_get_main_table()); } } /** * Filters the list of fields on a model. It is best to NOT use this directly and instead just use * EE_Register_Model_Extension * @param EE_Model_Field_Base[] $_fields */ $this->_fields = apply_filters('FHEE__' . get_class($this) . '__construct__fields', $this->_fields); foreach ($this->_fields as $table_alias => $fields_for_table) { if (!array_key_exists($table_alias, $this->_tables)) { throw new EE_Error(sprintf(__("Table alias %s does not exist in EEM_Base child's _tables array. Only tables defined are %s", 'event_espresso'), $table_alias, implode(",", $this->_fields))); } foreach ($fields_for_table as $field_name => $field_obj) { /** @var $field_obj EE_Model_Field_Base | EE_Primary_Key_Field_Base */ //primary key field base has a slightly different _construct_finalize /** @var $field_obj EE_Model_Field_Base */ $field_obj->_construct_finalize($table_alias, $field_name, $this->get_this_model_name()); } } // everything is related to Extra_Meta if (get_class($this) != 'EEM_Extra_Meta') { //make extra meta related to everything, but don't block deleting things just //because they have related extra meta info. For now just orphan those extra meta //in the future we should automatically delete them $this->_model_relations['Extra_Meta'] = new EE_Has_Many_Any_Relation(FALSE); } //and change logs if (get_class($this) != 'EEM_Change_Log') { $this->_model_relations['Change_Log'] = new EE_Has_Many_Any_Relation(FALSE); } /** * Filters the list of relations on a model. It is best to NOT use this directly and instead just use * EE_Register_Model_Extension * @param EE_Model_Relation_Base[] $_model_relations */ $this->_model_relations = apply_filters('FHEE__' . get_class($this) . '__construct__model_relations', $this->_model_relations); foreach ($this->_model_relations as $model_name => $relation_obj) { /** @var $relation_obj EE_Model_Relation_Base */ $relation_obj->_construct_finalize_set_models($this->get_this_model_name(), $model_name); } foreach ($this->_indexes as $index_name => $index_obj) { /** @var $index_obj EE_Index */ $index_obj->_construct_finalize($index_name, $this->get_this_model_name()); } $this->set_timezone($timezone); //finalize default where condition strategy, or set default if (!$this->_default_where_conditions_strategy) { //nothing was set during child constructor, so set default $this->_default_where_conditions_strategy = new EE_Default_Where_Conditions(); } $this->_default_where_conditions_strategy->_finalize_construct($this); //if the cap slug hasn't been set, and we haven't set it to false on purpose //to indicate to NOT set it, set it to the logical default if ($this->_caps_slug === null) { EE_Registry::instance()->load_helper('Inflector'); $this->_caps_slug = EEH_Inflector::pluralize_and_lower($this->get_this_model_name()); } //initialize the standard cap restriction generators if none were specified by the child constructor if ($this->_cap_restriction_generators !== false) { foreach ($this->cap_contexts_to_cap_action_map() as $cap_context => $action) { if (!isset($this->_cap_restriction_generators[$cap_context])) { $this->_cap_restriction_generators[$cap_context] = apply_filters('FHEE__EEM_Base___construct__standard_cap_restriction_generator', new EE_Restriction_Generator_Protected(), $cap_context, $this); } } } //if there are cap restriction generators, use them to make the default cap restrictions if ($this->_cap_restriction_generators !== false) { foreach ($this->_cap_restriction_generators as $context => $generator_object) { if (!$generator_object) { continue; } if (!$generator_object instanceof EE_Restriction_Generator_Base) { throw new EE_Error(sprintf(__('Index "%1$s" in the model %2$s\'s _cap_restriction_generators is not a child of EE_Restriction_Generator_Base. It should be that or NULL.', 'event_espresso'), $context, $this->get_this_model_name())); } $action = $this->cap_action_for_context($context); if (!$generator_object->construction_finalized()) { $generator_object->_construct_finalize($this, $action); } } } do_action('AHEE__' . get_class($this) . '__construct__end'); }