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

Turn OFF getMyProperty() method during DB save. #1646

Closed
nowackipawel opened this issue Jan 2, 2019 · 3 comments
Closed

Turn OFF getMyProperty() method during DB save. #1646

nowackipawel opened this issue Jan 2, 2019 · 3 comments

Comments

@nowackipawel
Copy link
Contributor

My entity:

<?php namespace App\Entities\Question;

use CodeIgniter\Entity;

class QuestionStatMonth extends Entity
{
	protected $qsm_que_id;
	protected $qsm_year;
	protected $qsm_month;
	protected $qsm_ok;
	protected $qsm_nok;

	protected $_options = [
		'casts'   => [
			'qsm_que_id'  	=> 'integer',
			'qsm_year' 		=> 'integer',
			'qsm_month'   	=> 'integer',
			'qsm_ok'    	=> 'integer',
			'qsm_nok'    	=> 'integer',
		],
		'dates'   => [],
		'datamap' => [],
	];

	public function setQsmYear($value)
	{
		$this->qsm_year = ($value - 2000 > 0) ? $value - 2000 : null; 
print $this->qsm_year;
	}

	public function getQsmYear()
	{
		return ! is_null($this->qsm_year) ? $this->qsm_year + 2000 : null;
	}

}

when I set my $my_entity->qsm_year = 2017 I've got printed out 17 (what is correct), but when I am trying to save it in database I've got 2017 in DB. I think we should use similar approach as is in #1373 .

I will PR in a while cuz I'd like to see if it won't break any tests.

@nowackipawel nowackipawel changed the title I've noticed strage behaviour in Entity Turn OFF getMyProperty() method during DB save. Jan 2, 2019
@nowackipawel
Copy link
Contributor Author

I think we should do this cuz methods like getMyProperty() and setMyProperty() should change property value outside the database. Am I wrong?

@nowackipawel
Copy link
Contributor Author

Already know it is more complicated even every test in travis were passed.

But it works ok as far as we don't have to validation rules like this:

'qsm_year' => 'required|greater_than_equal_to[2000]|less_than_equal_to[2100]',
According to #1647 database value is as excepted (17 for 2017) but validation is breaking.
We could deal with this in model classToArray method and update/insert/etc. but I am not sure if I should check what we will be able to do with it.

Or we just should assume that in my case this rule should looks like:
'qsm_year' => 'required|greater_than_equal_to[0]|less_than_equal_to[100]',
because is related to value in database ?

@lonnieezell
Copy link
Member

I think a better solution might be to create a new method, toRawArray() that simply returns all of the values as they currently are, ignoring casts and no using _get(). Then that can be used by the insert/save methods.

Should take care of it, right?

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

No branches or pull requests

2 participants