Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ?integer, ?double, ?string, etc. cast types :) #1575

Merged
merged 6 commits into from
Dec 5, 2018

Conversation

nowackipawel
Copy link
Contributor

Adds new cast types which are casting only if value !== null

i.e. I've got an nullable col in my dbtable which stores strings (empty or not) and I'd like to have possibility to determine if string is null or '';
Now I cannot when I define casting as a string. with this PR if I define ?string in


	protected $_options = [
		'casts' => [
			'usr_id' 			=> 'integer',
			'usr_usp_id' 		=> 'integer',
			'usr_provider_id' 	=> 'string',

			'usr_exc_id'		=> 'integer',
			'usr_drs_id'		=> 'integer',
			'usr_dlc_id'		=> '?string',
			'usr_tzo_id'		=> 'integer',

			'usr_email' 		=> 'string',
			'usr_password' 		=> 'string',

			'usr_first_name' 	=> 'string',
			'usr_last_name' 	=> 'string',
			'usr_nick_name'		=> 'string',

			'usr_is_active' 	=> 'boolean',
			'usr_is_blocked' 	=> 'boolean',
			'usr_is_picture' 	=> 'boolean',
		],
		'dates'=> ['usr_created_at', 'usr_updated_at'],
		'datamap' => []
	];

I will get NULL or '' for usr_dlc_id according to database value.

if(substr($type, 0, 1) === '?' && $value === null) return null;

Adds new cast types which are casting only if value !== null

i.e. I've got an nullable col in my dbtable which stores strings (empty or not) and I'd like to have possibility to determine if string is null or '';
Now I cannot when I define casting as a string. with this PR if I define ?string in 

```

	protected $_options = [
		'casts' => [
			'usr_id' 			=> 'integer',
			'usr_usp_id' 		=> 'integer',
			'usr_provider_id' 	=> 'string',

			'usr_exc_id'		=> 'integer',
			'usr_drs_id'		=> 'integer',
			'usr_dlc_id'		=> '?string',
			'usr_tzo_id'		=> 'integer',

			'usr_email' 		=> 'string',
			'usr_password' 		=> 'string',

			'usr_first_name' 	=> 'string',
			'usr_last_name' 	=> 'string',
			'usr_nick_name'		=> 'string',

			'usr_is_active' 	=> 'boolean',
			'usr_is_blocked' 	=> 'boolean',
			'usr_is_picture' 	=> 'boolean',
		],
		'dates'=> ['usr_created_at', 'usr_updated_at'],
		'datamap' => []
	];
```
I will get NULL or '' for usr_dlc_id according to database value.
@lonnieezell
Copy link
Member

Sounds good. Needs documented and tested.

system/Entity.php Outdated Show resolved Hide resolved
@nowackipawel
Copy link
Contributor Author

@lonnieezell : docs, tests, aliases for integer and boolean - added

integer -> int, boolean -> bool      // coments ;-)
@lonnieezell lonnieezell merged commit b6ee14e into codeigniter4:develop Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants