1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Core class that all associations must extend |
5
|
|
|
* |
6
|
|
|
* @package Jam |
7
|
|
|
* @category Associations |
8
|
|
|
* @author Ivan Kerin |
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
10
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
11
|
|
|
*/ |
12
|
|
|
abstract class Kohana_Jam_Association extends Jam_Attribute { |
13
|
|
|
|
14
|
|
|
const NULLIFY = 'nullify'; |
15
|
|
|
const ERASE = 'erase'; |
16
|
|
|
const DELETE = 'delete'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the primary key from whatever value you have |
20
|
|
|
* |
21
|
|
|
* @param string $model_name The name of the model |
22
|
|
|
* @param string|integer|Jam_Validated|array $value the value or a container of the value |
23
|
|
|
* @return string|integer|NULL NULL when no value is provided or could be extracted. |
24
|
|
|
*/ |
25
|
46 |
|
public static function primary_key($model_name, $value) |
26
|
|
|
{ |
27
|
46 |
|
if ( ! $value) |
28
|
4 |
|
return NULL; |
29
|
|
|
|
30
|
42 |
|
if ($value instanceof Jam_Validated) |
31
|
18 |
|
return $value->id(); |
32
|
|
|
|
33
|
26 |
|
if (is_integer($value) OR is_numeric($value)) |
34
|
13 |
|
return (int) $value; |
35
|
|
|
|
36
|
14 |
|
if (is_string($value)) |
37
|
5 |
|
return $value; |
38
|
|
|
|
39
|
9 |
|
if (is_array($value)) |
40
|
9 |
|
return Arr::get($value, Jam::meta($model_name)->primary_key()); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
17 |
|
public static function is_changed($value) |
44
|
|
|
{ |
45
|
17 |
|
if ($value instanceof Jam_Model AND ( ! $value->loaded() OR $value->changed())) |
46
|
1 |
|
return TRUE; |
47
|
|
|
|
48
|
16 |
|
return is_array($value); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public $foreign_model = NULL; |
52
|
|
|
|
53
|
|
|
public $readonly = NULL; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* If set to true, will delete the association object when this one gets deleted |
57
|
|
|
* possible values are Jam_Association::DELETE and Jam_Association::ERASE and Jam_Association::NULLIFY |
58
|
|
|
* Jam_Association::DELETE will run the delete event of the associated model, Jam_Association::ERASE will not |
59
|
|
|
* |
60
|
|
|
* @var boolean|string |
61
|
|
|
*/ |
62
|
|
|
public $dependent = FALSE; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* See if the association is polymorphic. |
66
|
|
|
* This is overloaded in the associations themselves |
67
|
|
|
* |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
5 |
|
public function is_polymorphic() |
71
|
|
|
{ |
72
|
5 |
|
return FALSE; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
abstract public function join($table, $type = NULL); |
76
|
|
|
|
77
|
|
|
public function set(Jam_Validated $model, $value, $is_changed) |
78
|
|
|
{ |
79
|
|
|
return $value; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.