| | 13 | === hasOne definition as models property === |
| | 14 | each ''extended'' abstractModel class has a static property named $hasOne that describe relation between the model and other models it own or belong to. let's explain how this work and how to change parameters defined by the [wiki:modelGeneration modelGenerator] to better sweet our needs. |
| | 15 | {{{ |
| | 16 | #!php |
| | 17 | <?php |
| | 18 | class posts extends BASE_POSTS{ |
| | 19 | static protected $hasOne = array( |
| | 20 | // 'relName'=> array('modelName'=>'modelName','relType'=>'ignored|dependOn|requireBy',['localField'=>'fldNameIfNotPrimaryKey','foreignField'=>'fldNameIfNotPrimaryKey','foreignDefault'=>'ForeignFieldValueOnDelete']) |
| | 21 | 'user' => array( |
| | 22 | 'modelName'=>'users', |
| | 23 | 'localField'=>'user', |
| | 24 | 'relType'=>'dependOn', |
| | 25 | ), |
| | 26 | ); |
| | 27 | } |
| | 28 | }}} |
| | 29 | so each hasOne relation is defined has an entry in this property. The key used in model::$hasOne property determine how we will access to the related model using a declared instance of this model: |
| | 30 | in other words in the previous sample this mean that we can access user of a post like this |
| | 31 | {{{ |
| | 32 | $post = posts::getInstance($instanceId); |
| | 33 | $user = $post->user; |
| | 34 | }}} |
| | 35 | |
| | 36 | |
| | 37 | each hasOne entry is defined by parameters: |
| | 38 | - modelName : the class name of the extended model related to the current model |
| | 39 | - relType : one of ignored, dependOn or requireBy see further in this page for more explanation on this. |
| | 40 | - localField : this is the name of the database field name in the table of current model that is the primaryKey of the related model. |
| | 41 | - foreignField : this is the name of the database field name in the table of related model that is referenced in the local field, this only need to be set if it's not the related model primaryKey. |
| | 42 | - foreignDefault: the default value of the foreign field. This is used at delete time to eventually set the foreignField to its default value instead of deleting it too (only used on ignored or dependOn relations). |
| | 43 | |
| | 44 | |
| | 45 | |