Skip to content

Commit 8b3cb3f

Browse files
committed
added has_and_belongs_to_many feature
1 parent b6e3c6a commit 8b3cb3f

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

core/MY_Model.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MY_Model extends CI_Model {
1919
// Relationship arrays.
2020
protected $belongs_to = array(); // Has one model that it belongs to.
2121
protected $has_many = array(); // Has many models.
22+
protected $has_and_belongs_to_many = array(); // Has many models and belongs to many models.
2223
protected $includes_values = array(); // Relationships to be included
2324

2425
// Callbacks for creation.
@@ -426,6 +427,73 @@ public function includes($relationship)
426427
return $this;
427428
}
428429

430+
/**
431+
* Perform a join.
432+
*
433+
* @param string $table
434+
* @param string $cond
435+
* @param string $type
436+
* @return MY_Model
437+
*/
438+
public function joins($table, $cond, $type = '')
439+
{
440+
$this->connection->join($table, $cond, $type);
441+
return $this;
442+
}
443+
444+
/**
445+
* Add an associated model to this model.
446+
*
447+
* @param string $association
448+
* @param MY_Model $object
449+
*/
450+
public function add($association, $object)
451+
{
452+
// Determine table and key names.
453+
$table1 = $this->table_name;
454+
$table2 = $association;
455+
$key1 = singular($table1) . '_id';
456+
$key2 = singular($table2) . '_id';
457+
if (strcmp($table1, $table2) < 0) $join_table = $table1 . '_' . $table2;
458+
else $join_table = $table2 . '_' . $table1;
459+
460+
// Insert the join table record.
461+
$this->connection->insert($join_table, array(
462+
$key1 => $this->{$this->primary_key},
463+
$key2 => $object->{$object->primary_key}
464+
));
465+
466+
// Add the object to the model association.
467+
if ( ! isset($this->{$association}))
468+
{
469+
$this->{$association} = array();
470+
}
471+
array_push($this->{$association}, $object);
472+
473+
return TRUE;
474+
}
475+
476+
public function remove($association, $object)
477+
{
478+
// Determine table and key names.
479+
$table1 = $this->table_name;
480+
$table2 = $association;
481+
$key1 = singular($table1) . '_id';
482+
$key2 = singular($table2) . '_id';
483+
if (strcmp($table1, $table2) < 0) $join_table = $table1 . '_' . $table2;
484+
else $join_table = $table2 . '_' . $table1;
485+
486+
// Delete the join table record.
487+
$this->connection->delete($join_table, array(
488+
$key1 => $this->{$this->primary_key},
489+
$key2 => $object->{$object->primary_key}
490+
));
491+
492+
// TODO: Remove object from association array.
493+
494+
return TRUE;
495+
}
496+
429497
/**
430498
* Load a relationship.
431499
*
@@ -606,6 +674,49 @@ protected function relate($record)
606674
$record->{$relationship} = $this->{$options['model']}->where(array($options['primary_key'] => $record->{$this->primary_key}));
607675
}
608676
}
677+
678+
// Connect all has and belongs to many relationships.
679+
foreach ($this->has_and_belongs_to_many as $key => $value)
680+
{
681+
if (is_string($value))
682+
{
683+
$relationship = $value;
684+
$options = array('primary_key' => singular($this->table_name) . '_id', 'model' => singular($value) . '_model');
685+
}
686+
else
687+
{
688+
$relationship = $key;
689+
$options = $value;
690+
}
691+
692+
if (in_array($relationship, $this->includes_values))
693+
{
694+
$this->load->model($options['model']);
695+
$table1 = $this->table_name;
696+
$table2 = $this->{$options['model']}->table_name;
697+
$key1 = $options['primary_key'];
698+
$key2 = singular($table2) . '_id';
699+
if (strcmp($table1, $table2) < 0) $join_table = $table1 . '_' . $table2;
700+
else $join_table = $table2 . '_' . $table1;
701+
$join_model = $this->{$options['model']};
702+
$join1_str = $join_table . '.' . $key1 . ' = ' . $table1 . '.' . $this->primary_key;
703+
$join2_str = $join_table . '.' . $key2 . ' = ' . $table2 . '.' . $join_model->primary_key;
704+
$where_str = $table1 . '.' . $this->primary_key . ' = ' . $record->{$this->primary_key};
705+
706+
//$record->{$relationship} = $join_model->joins($join_table, $join2_str)->joins($this->table_name, $join1_str)->where($where_str);
707+
708+
$sql = "SELECT $table2.* FROM $table2 JOIN $join_table ON $join2_str JOIN $this->table_name ON $join1_str WHERE $where_str";
709+
710+
$record->{$relationship} = array();
711+
$query = $this->connection->query($sql);
712+
foreach ($query->result() as $row)
713+
{
714+
$association = $join_model->parse_row($row);
715+
array_push($record->{$relationship}, $association);
716+
}
717+
718+
}
719+
}
609720
}
610721

611722
protected function validate($data)

0 commit comments

Comments
 (0)