Hi!
I have following model:
class Users extends \Phalcon\Mvc\Model
{
public $ip;
public function getIp() { return long2ip($this->ip); }
public function setIp() { $this->ip = ip2long($this->ip); }
}
Testing:
function test($real, $need)
{
$isOk = $real == $need;
$status = $isOk ? ' OK ' : 'FAIL';
echo "$status | $real => $need\n";
}
// Data for test
$test = [
'old' => [
'str' => '127.255.255.255',
'int' => 2147483647,
],
'new' => [
'str' => '192.168.1.42',
'int' => 3232235818,
],
];
/** @type Users $user */
$user = Users::findFirst(42);
echo "=== Get ===\n";
test($user->ip , $test['old']['str']); // FAIL | 2147483647 => 127.255.255.255
test($user->getIp() , $test['old']['str']); // OK | 127.255.255.255 => 127.255.255.255
test($user->toArray()['ip'], $test['old']['str']); // FAIL | 2147483647 => 127.255.255.255
echo "=== Set ===\n";
$user->ip = $test['new']['int'];
test($user->ip , $test['new']['str']); // FAIL | 3232235818 => 192.168.1.42
test($user->getIp() , $test['new']['str']); // OK | 192.168.1.42 => 192.168.1.42
test($user->toArray()['ip'], $test['new']['str']); // FAIL | 3232235818 => 192.168.1.42
$user->save(); // PDOException: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'ip' at row 1
Why getters/setters not working?