Skip to content

Commit bfd58ca

Browse files
author
Darryl Kuhn
committed
update the hintJsonStructure() method so that it runs addHintedAttributes() allowing callers to get/set parameters when working with new models
1 parent d7b6e4d commit bfd58ca

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Photo extends Eloquent
6262
```
6363
Once you create a hint you will be able to make calls to get and set json attributes e.g. `$photo->foo = 'bar';` regardless of whether or not they are already defined in the underlying db record. Alternatly if you prefer not to hint structures then you may call `setJsonAttribute()`. For example if you defined a json column called "json_data" and wanted to set an attribute called 'fizz' so you could call:
6464
```php
65-
$referral->setJsonAttribute( 'json_data', 'fizz', 'buzz' );
65+
$photo->setJsonAttribute( 'json_data', 'fizz', 'buzz' );
6666
```
6767
### Showing/Hiding Attributes
6868
One of the aims of the project is to make json attributes "first class" citizens of the model. This means by default we add the attributes to the models appends array so that when you call `$model->toArray()` or `$model->toJson()` the attribute shows up as a part of the structure like a normal attribute. By default we also hide away the json column holding the underlying data. Both of these settings can be changed using the `showJsonColumns()` and `showJsonAttributes()` as shown below:

src/Dialect/Json.php

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public function hintJsonStructure( $column, $structure )
136136
}
137137

138138
$this->hintedJsonAttributes[$column] = $structure;
139+
140+
// Run the call to add hinted attributes to the internal json
141+
// attributes array. This allows callers to get/set parameters when
142+
// working with new models
143+
$this->addHintedAttributes();
139144
}
140145

141146
/**

tests/JsonDialectTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public function testInvalidJsonAttribute()
7373
$mock = new MockJsonDialectModel;
7474
$mock->hintJsonStructure( 'testColumn', json_encode(['foo'=>null]) );
7575

76-
// Execute the hint call
77-
$mock->addHintedAttributes();
76+
// Set testColumn to invalid json
7877
$mock->setAttribute('testColumn', '{');
7978

8079
// Try to access a property on invalid json - we should get an

0 commit comments

Comments
 (0)