function AdoDBRecord_AssociationProxy($client, $returns_many, $options) { $this->_client = $client; $this->_returns_many = $returns_many; $this->_options = $options; # build base data from options # TODO select source from polymorphic type $source = $this->_source = Singleton::instance($options["class_name"]); if (!is_subclass_of($this->_source, 'AdoDBRecord')) { die("AdoDBRecord_AssociationProxy: associated class isn't an ancestor of AdoDBRecord"); } # extract find options $find_options = array(); if (isset($options["order"])) { $find_options["order"] = $options["order"]; } # TODO if (isset($options["select"])) $find_options["select"] = $options["select"]; # parse conditions into string + parameters # FIXME make this DRY $parsed_conditions = array(); $parsed_params = array(); if (isset($options["conditions"])) { $arg = $options["conditions"]; if (!is_array($arg)) { $arg = array($arg); } $conditions = array_merge_recursive($conditions, $arg); AdoDBRecord_Tools::parse_conditions($conditions, $parsed_conditions, $parsed_params); } if (isset($options["primary_key"])) { $parsed_conditions[] = sprintf("%s = ?", $options["foreign_key"]); # FIXME PHP4 does not like $obj->$property indirection, fix this after dropping PHP4 support $parsed_params[] = $client->_attributes[$options["primary_key"]]; } else { $parsed_conditions[] = sprintf("%s = ?", 'id'); # FIXME PHP4 does not like $obj->$property indirection, fix this after dropping PHP4 support $parsed_params[] = $client->_attributes[$options["foreign_key"]]; } $find_conditions = sprintf("(%s)", join($parsed_conditions, ") AND (")); $find_conditions = strtr($find_conditions, array("%" => "%%", "?" => "'%s'")); # FIXME not database agnostic $find_conditions = vsprintf($find_conditions, $parsed_params); $find_options = array_merge($find_options, array("conditions" => $find_conditions)); if (isset($options["primary_key"])) { $this->_wrap_scope = array("find" => $find_options, "create" => array($options["foreign_key"] => $client->{$options}["primary_key"])); } else { $this->_wrap_scope = array("find" => $find_options); } }
function is_association_property($name) { return AdoDBRecord_Tools::is_belongs_to_property($name) || AdoDBRecord_Tools::is_has_many_property($name) || AdoDBRecord_Tools::is_has_one_property($name); }
function parse_member() { $args = func_get_args(); switch (count($args)) { case 1: # this call was made by __get() list($property) = $args; if (AdoDBRecord_Tools::is_column_property($property)) { return $this->_attributes[$property]; } if ($property == "id") { return $this->_attributes[$this->_primary_key]; } # if no column property check for association or proxy # TODO cache proxy $use_proxy = substr($property, -1) == "_"; if ($use_proxy) { $property = substr($property, 0, -1); } if (AdoDBRecord_Tools::is_association_property($property)) { if (AdoDBRecord_Tools::is_has_many_property($property)) { $returns_many = true; $options = $this->_has_many[$property]; } elseif (AdoDBRecord_Tools::is_belongs_to_property($property)) { $returns_many = false; $options = $this->_belongs_to[$property]; } elseif (AdoDBRecord_Tools::is_has_one_property($property)) { $returns_many = false; $options = $this->_has_one[$property]; } else { die("AdoDBRecord_Base::parse_member(): fatal association inconsistency"); } $proxy = new AdoDBRecord_AssociationProxy($this, $returns_many, $options); if ($use_proxy) { return $proxy; } return $proxy->find(); } # TODO write a real error handler die(get_class($this) . "->{$property}: No such property"); case 2: # this call was made by __set() # TODO check property is valid (_associations) list($property, $value) = $args; if (AdoDBRecord_Tools::is_column_property($property)) { return $this->set_attributes(array($property => $value)); } if ($property == "id") { return $this->set_attributes(array($this->_primary_key => $value)); } # TODO write a real error handler die(get_class($this) . "->{$property}: No such property"); default: # this call was made by some chaotic wizard and is invalid # TODO write a real error handler die("AdoDBRecord_Base::parse_member(): unexpected arguments received"); } }
function end_scope() { if (defined("AR_PHP4_COMPAT")) { # PHP4 cannot pass properties of overloaded objects by reference - work around it $temp = $this->_scoped_methods; array_pop($temp); $this->_scoped_methods = $temp; } else { array_pop($this->_scoped_methods); } AdoDBRecord_Tools::join_scope_stack(); }