Closed benmajor closed 2 years ago
Thank you for reporting this, I will look into this issue.
I cannot seem to reproduce this issue:
(careful, contains nuke())
require 'rb.php';
R::setup('mysql:host=localhost;dbname=redbean','root','root' );
R::getDatabaseAdapter()->getDatabase()->stringifyFetches(false);
R::getDatabaseAdapter()->getDatabase()->getPDO()->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
R::nuke();
$organisation = R::dispense('organisation');
$organisation->name = 'Big Ben';
$owner = R::dispense('user');
$owner->name = 'Ben';
$organisation->owner = $owner;
$id = R::store($organisation);
$organisation = R::load('organisation',$id);
$owner = $organisation->fetchAs('user')->owner;
var_dump($owner->id);
gives:
int(1)
Okay, thanks for looking into this. I will do some more investigation and see if I can figure out what's going on.
I wonder if it's related to me not calling stringifyFetches(false)
anywhere?
This might cause an issue yes. For MySQL you also need to set ATTR_EMULATE_PREPARES. Please note that RedBeanPHP and PDO cannot really enforce types, it's always up to the implementation of the database driver to honour these requests.
Sorry for the delay in coming back regarding this. Calling stringifyFetches(false)
seems to solve the problem. Can I ask why the default value for this is true
?
I think this is mainly for backward compatibility. Personally I never bother to change it since I regard PHP as a dynamically typed programming language and I am fine with the associated risks and limitations of that.
According to this all $bean->getId()
should return string|null
We have noticed some odd behaviour in RedBean, which I suspect may be related to
fetchAs
and models. For instance, in ourOrganization
model, we have the following getter defined:This correctly returns the
user
bean, and we're then able to use thegetId()
method on that bean, but it returns a string, rather than integer, so a strict equality check fails. The below is from a method inside of ouruser
Model:Which produces:
Obviously this causes the following check to fail:
Of course, we can solve this by changing to a non-strict equality check, but it would be good to try and narrow down where the behaviour is coming from, as I don't think this should be the default behaviour inside of RedBean?
In case it helps, here's our
__call()
magic method that handles the getters and setters of Models via anAbstractEntity
class, in case it may be useful in identifying the cause of this: