bind() public static method

Creates a relationship binding between this model and another.
See also: lithium\data\model\Relationship
public static bind ( string $type, string $name, array $config = [] ) : object
$type string The type of relationship to create. Must be one of `'hasOne'`, `'hasMany'` or `'belongsTo'`.
$name string The name of the relationship. If this is also the name of the model, the model must be in the same namespace as this model. Otherwise, the fully-namespaced path to the model class must be specified in `$config`.
$config array Any other configuration that should be specified in the relationship. See the `Relationship` class for more information.
return object Returns an instance of the `Relationship` class that defines the connection.
Example #1
0
 /**
  * Creates a relationship binding between this model and another. Overwritten to allow model to model relations seperate of data source relations.
  *
  * @see lithium\data\model\Relationship
  * @param string $type The type of relationship to create. Must be one of `'hasOne'`,
  *               `'hasMany'` or `'belongsTo'`.
  * @param string $name The name of the relationship. If this is also the name of the model,
  *               the model must be in the same namespace as this model. Otherwise, the
  *               fully-namespaced path to the model class must be specified in `$config`.
  * @param array $config Any other configuration that should be specified in the relationship.
  *              See the `Relationship` class for more information.
  * @return object Returns an instance of the `Relationship` class that defines the connection.
  */
 public static function bind($type, $name, array $config = array())
 {
     $defaults = array('default' => false);
     // li3_embedded catch to make embedding easier
     if (isset($config['embedded']) && !isset($config['default'])) {
         if (!empty($config['embedded'])) {
             $config['default'] = true;
         } else {
             $config['default'] = false;
         }
     }
     $config += $defaults;
     $self = static::_object();
     if (!isset($config['to']) && isset($config['class'])) {
         $config['to'] = $config['class'];
     }
     if (!isset($config['to'])) {
         $config['to'] = $name;
     }
     $config['to'] = Libraries::locate('models', $config['to']);
     $targetModel = $config['to'];
     //TODO, add general exception option & add mongo exception for non embedded
     if (!empty($targetModel) && $config['default'] === false) {
         // continue on if default lithium relationship will not work
         if (isset($config['fieldName'])) {
             $fieldName = $config['fieldName'];
         } else {
             $fieldName = $name;
             if ($type == 'hasMany') {
                 $fieldName = Inflector::pluralize($name);
             } else {
                 $fieldName = Inflector::singularize($name);
             }
             $fieldName = Inflector::underscore($fieldName);
         }
         $key = "{$fieldName}_id";
         $from = get_called_class();
         $config += compact('type', 'name', 'key', 'from', 'fieldName');
         $connection = static::connection();
         $relationship = $connection->invokeMethod('_instance', array('relationship', $config));
         if (!empty($relationship)) {
             $self->_alternateRelations[$name] = $relationship;
             return null;
         }
     }
     return parent::bind($type, $name, $config);
 }