What parameters can table associations take?
From PHP on Trax
Below are the different parameters that the table associations can take. All the associations are defined as public class variables either as a string or an array.
<?
class Project extends ActiveRecord {
public $belongs_to = "project_manager";
}
?>
Contents |
$belongs_to
no parameters
If no parameters are needed, ie you setup the foreign keys correctly as singular table name underscore id (user_id). You need to define the association a string. If there are multiple associations of the same type then you can make it a string where the values are seperated by a comma ("asssoc1,assoc2,etc").
<? public $belongs_to = "project_manager,portfolio,etc"; ?>
with parameters
If there are parameters needed then you need to define the association an array. Below are the possible parameters.
<?
public $belongs_to = array(
# this association name 'project_manager' can be called a custom name whatever you want but
# if its custom then you must specify 'class_name' so Trax knows what class this custom
# name goes to
'project_manager' => array(
# Not using table_id you are defining your own foreign key
# this foreign key is in this table (the model you are defining this in)
'foreign_key' => 'myown_foreign_key',
# Used if you want to define a customs association name (instead of project_manager above)
# so Trax will know what class this custom name goes to
'class_name' => 'ProjectManager',
# conditions are a where clause to filter the association results on
'conditions' => 'age > 35',
# order by clause
'order' => 'last_name, first_name'
),
'portfolio' => null # if no parameters for this one then just put null
);
?>
$has_one
no parameters
If no parameters are needed, ie you setup the foreign keys correctly as singular table name underscore id (user_id). You need to define the association a string. If there are multiple associations of the same type then you can make it a string where the values are seperated by a comma ("asssoc1,assoc2,etc").
<? public $has_one = "project,portfolio,etc"; ?>
with parameters
If there are parameters needed then you need to define the association an array. Below are the possible parameters.
<?
public $has_one = array(
# this association name 'project' can be called a custom name whatever you want but
# if its custom then you must specify 'class_name' so Trax knows what class this custom
# name goes to
'project' => array(
# Not using table_id you are defining your own foreign key
# this foreign key is in the other table (the model for the association above 'project' or 'projects' table)
'foreign_key' => 'myown_foreign_key',
# Used if you want to define a customs association name (instead of project above)
# so Trax will know what class this custom name goes to
'class_name' => 'Project',
# conditions are a where clause to filter the association results on
'conditions' => 'project_manager_id = 10',
# order by clause
'order' => 'name DESC'
),
'portfolio' => null # if no parameters for this one then just put null
);
?>
$has_many
no parameters
If no parameters are needed, ie you setup the foreign keys correctly as singular table name underscore id (user_id). You need to define the association a string. If there are multiple associations of the same type then you can make it a string where the values are seperated by a comma ("asssoc1,assoc2,etc").
<? public $has_many = "project_managers,milestones,etc"; ?>
with parameters
If there are parameters needed then you need to define the association an array. Below are the possible parameters.
<?
public $has_many = array(
# this association name 'project_managers' can be called a custom name whatever you want but
# if its custom then you must specify 'class_name' so Trax knows what class this custom
# name goes to
'project_managers' => array(
# Not using table_id you are defining your own foreign key
# this foreign key is in the other table (the model for the association above 'project_manager' or 'project_managers' table)
'foreign_key' => 'myown_foreign_key',
# Used if you want to define a customs association name (instead of project above)
# so Trax will know what class this custom name goes to
'class_name' => 'ProjectManager',
# conditions are a where clause to filter the association results on
'conditions' => 'department_id = 10',
# order by clause
'order' => 'last_name, first_name'
# limit returned results to 10 rows
'limit' => 10,
# define your own custom query to get all the associated project_managers (any valid sql)
'finder_sql' => "SELECT * FROM project_managers WHERE last_name = 'smith'"
),
'milestones' => null # if no parameters for this one then just put null
);
?>
$has_and_belongs_to_many
no parameters
If no parameters are needed, ie you setup the foreign keys correctly as singular table name underscore id (user_id). You need to define the association a string. If there are multiple associations of the same type then you can make it a string where the values are seperated by a comma ("asssoc1,assoc2,etc").
<? public $has_and_belongs_to_many = "categories,images,etc"; ?>
with parameters
If there are parameters needed then you need to define the association an array. Below are the possible parameters.
<?
public $has_and_belongs_to_many = array(
# this association name 'categories' can be called a custom name whatever you want but
# if its custom then you must specify 'class_name' so Trax knows what class this custom
# name goes to
'categories' => array(
# set the foreign key in the join_table for this model (the model your in now)
'foreign_key' => 'product_id',
# set the foreign key in the join table for the association above 'categories'
'association_foreign_key' => 'category_id'
# Used if you want to define a customs association name (instead of project above)
# so Trax will know what class this custom name goes to
'class_name' => 'Category',
# Not using standard table1_table2 (categories_products) join table
'join_table' => 'my_own_join_table_name'
# conditions are a where clause to filter the association results on
'conditions' => "categories.name LIKE '%fruit'",
# order by clause
'order' => 'last_name, first_name'
# limit returned results to 10 rows
'limit' => 10,
# define your own custom query to get all the associated categories (any valid sql)
'finder_sql' => "SELECT * FROM categories WHERE name LIKE '%fruit'"
),
'images' => null # if no parameters for this one then just put null
);
?>
