/**
  * One-to-many relationship, e.g. a blog post has many comments.
  * @param string $foreignTableName
  * @param Table $table
  */
 private function hasMany($table, $foreignTableName)
 {
     $foreignTableName = strtolower($foreignTableName);
     assert('!empty($table->primaryKey); // table needs to have primary key defined for table join');
     // can we load the foreign table?
     if (class_exists(ucfirst($foreignTableName))) {
         // capitalize
         $foreignTableName = ucfirst($foreignTableName);
         // note the foreign table might have relationships of its own...
         $foreignTable = new $foreignTableName();
         assert('!empty($foreignTable->primaryKey); // foreign table needs to have primary key defined');
         // create the join where "ourTable.ourId=foreignTable.ourTable_ourId"
         $table->join($foreignTable->tableName, array("{$table->tableName}.{$table->primaryKey}" => "{$foreignTable->tableName}.{$table->tableName}_{$table->primaryKey}"));
         // as an added bonus, we might have a relationship in the foreign table, add it...
         $table->join .= $foreignTable->join;
     } else {
         // we will have to rely on our primary key
         // create the join where "ourTable.ourId=foreignTable.ourTable_ourId"
         $table->join($foreignTableName->tableName, array("{$table->tableName}.{$table->primaryKey}" => "{$foreignTableName->tableName}.{$table->tableName}_{$table->primaryKey}"));
     }
 }
Example #2
0
 public function join($ref, $alias, $cond, $params = null)
 {
     $this->from->join($ref, $alias, $cond, $params);
     return $this;
 }