12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php namespace System\DB\Eloquent;
- class Relate {
-
- public static function has_one($model, $foreign_key, $eloquent)
- {
- $eloquent->relating = __FUNCTION__;
- return static::has_one_or_many($model, $foreign_key, $eloquent);
- }
-
- public static function has_many($model, $foreign_key, $eloquent)
- {
- $eloquent->relating = __FUNCTION__;
- return static::has_one_or_many($model, $foreign_key, $eloquent);
- }
-
- private static function has_one_or_many($model, $foreign_key, $eloquent)
- {
- $eloquent->relating_key = (is_null($foreign_key)) ? \System\Str::lower(get_class($eloquent)).'_id' : $foreign_key;
- return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id);
- }
-
- public static function belongs_to($caller, $model, $eloquent)
- {
- $eloquent->relating = __FUNCTION__;
- $eloquent->relating_key = $caller['function'].'_id';
- return Factory::make($model)->where('id', '=', $eloquent->attributes[$eloquent->relating_key]);
- }
-
- public static function has_many_and_belongs_to($model, $table, $eloquent)
- {
-
-
-
- if (is_null($table))
- {
- $models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent)));
- sort($models);
- $eloquent->relating_table = implode('_', $models);
- }
- else
- {
- $eloquent->relating_table = $table;
- }
- $eloquent->relating = __FUNCTION__;
- $eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id';
- return Factory::make($model)
- ->select(Meta::table($model).'.*')
- ->join($eloquent->relating_table, Meta::table($model).'.id', '=', $eloquent->relating_table.'.'.\System\Str::lower($model).'_id')
- ->where($eloquent->relating_key, '=', $eloquent->id);
- }
- }
|