class HasMany extends AbstractRelationship (View source)

One-to-many relationship.

Table: people

Primary key: id

Foreign key: school_id

class Person extends ActiveRecord\Model {}

Table: schools

Primary key: id

class School extends ActiveRecord\Model { static $has_many = array( array('people') ); });

Example using options:

class Payment extends ActiveRecord\Model { static $belongs_to = array( array('person'), array('order') ); }

class Order extends ActiveRecord\Model { static $has_many = array( array('people', 'through' => 'payments', 'select' => 'people.*, payments.amount', 'conditions' => 'payments.amount < 200') ); }

Properties

string $attribute_name Name to be used that will trigger call to the relationship. from AbstractRelationship
string $class_name Class name of the associated model. from AbstractRelationship
string $foreign_key Name of the foreign key. from AbstractRelationship
protected array $options Options of the relationship. from AbstractRelationship
protected boolean $poly_relationship Is the relationship single or multi. from AbstractRelationship
static protected array $valid_association_options Valid options to use for a {@link HasMany} relationship.
protected $primary_key

Methods

__construct($options = array())

Constructs a {@link HasMany} relationship.

get_table()

No description

bool
is_poly()

What is this relationship's cardinality?

void
query_and_attach_related_models_eagerly(Table $table, $models, $attributes, $includes = array(), $query_keys = array(), $model_values_keys = array())

Eagerly loads relationships for $models.

build_association(Model $model, $attributes = array(), $guard_attributes = true)

Creates a new instance of specified {@link Model} with the attributes pre-loaded.

create_association(Model $model, $attributes = array(), $guard_attributes = true)

Creates a new instance of {@link Model} and invokes save.

append_record_to_associate(Model $associate, Model $record)

No description

merge_association_options($options)

No description

unset_non_finder_options($options)

No description

void
set_inferred_class_name()

Infers the $this->class_name based on $this->attribute_name.

set_class_name($class_name)

No description

create_conditions_from_keys(Model $model, $condition_keys = array(), $value_keys = array())

No description

string
construct_inner_join_sql(Table $from_table, bool $using_through = false, string $alias = null)

Creates INNER JOIN SQL for associations.

load(Model $model)

This will load the related model data.

set_keys($model_class_name, $override = false)

No description

load_eagerly($models = array(), $attributes = array(), $includes, Table $table)

No description

Details

__construct($options = array())

Constructs a {@link HasMany} relationship.

Parameters

$options

protected get_table()

bool is_poly()

What is this relationship's cardinality?

Return Value

bool

Eagerly loads relationships for $models.

This method takes an array of models, collects PK or FK (whichever is needed for relationship), then queries the related table by PK/FK and attaches the array of returned relationships to the appropriately named relationship on $models.

Parameters

Table $table
$models array of model objects
$attributes array of attributes from $models
$includes array of eager load directives
$query_keys -> key(s) to be queried for on included/related table
$model_values_keys -> key(s)/value(s) to be used in query from model which is including

Return Value

void

build_association(Model $model, $attributes = array(), $guard_attributes = true)

Creates a new instance of specified {@link Model} with the attributes pre-loaded.

Parameters

Model $model
$attributes
$guard_attributes

create_association(Model $model, $attributes = array(), $guard_attributes = true)

Creates a new instance of {@link Model} and invokes save.

Parameters

Model $model
$attributes
$guard_attributes

protected append_record_to_associate(Model $associate, Model $record)

Parameters

Model $associate
Model $record

protected merge_association_options($options)

Parameters

$options

protected unset_non_finder_options($options)

Parameters

$options

protected void set_inferred_class_name()

Infers the $this->class_name based on $this->attribute_name.

Will try to guess the appropriate class by singularizing and uppercasing $this->attribute_name.

Return Value

void

See also

attribute_name

protected set_class_name($class_name)

Parameters

$class_name

protected create_conditions_from_keys(Model $model, $condition_keys = array(), $value_keys = array())

Parameters

Model $model
$condition_keys
$value_keys

string construct_inner_join_sql(Table $from_table, bool $using_through = false, string $alias = null)

Creates INNER JOIN SQL for associations.

Parameters

Table $from_table the table used for the FROM SQL statement
bool $using_through is this a THROUGH relationship?
string $alias a table alias for when a table is being joined twice

Return Value

string SQL INNER JOIN fragment

load(Model $model)

This will load the related model data.

Parameters

Model $model The model this relationship belongs to

protected set_keys($model_class_name, $override = false)

Parameters

$model_class_name
$override

load_eagerly($models = array(), $attributes = array(), $includes, Table $table)

Parameters

$models
$attributes
$includes
Table $table