gabordemooij / redbean

ORM layer that creates models, config and database on the fly
https://www.redbeanphp.com
2.31k stars 280 forks source link

Data is not being stored in DB table #938

Closed BelleNottelling closed 9 months ago

BelleNottelling commented 9 months ago

Hi there,

I am running into an issue where the store method is not writing anything to the DB and there are also no errors being produced.

This is the responsible code:

// Dispense the bean
$user = $this->di['db']->dispense('openstack_users');

// Populating the bean with some dummy values:
$user->client_id = 999;
$user->password = "byv399nc95zf3gre89ygmmkgbywi3forgj48smduxtpq5hwzer43yhcggae547jy";
$user->id = "ee13bd1eab274ed3ba5c766c233ad208";
$user->project_id = "2102b340ed6a48d7836eebb41e91a49e";

// Store the bean
$this->di['db']->store($user);

No data is added to the openstack_users table, no exceptions are thrown, and nothing appears in the error log. I am able to manually add this info to the database via tools like phpMyAdmin, however programmatically through RedBeanPHP does not work.

I'd appreciate any insight on how to diagnose this so I can get it working, thank you!

Jemt commented 9 months ago

To my knowledge only lower case a-z letters are allowed as identifiers. So try replacing openstack_users with openstackusers, client_id with clientid, etc.

BelleNottelling commented 9 months ago

To my knowledge only lower case a-z letters are allowed as identifiers. So try replacing openstack_users with openstackusers, client_id with clientid, etc.

Thanks for the advice, I had forgotten about this. However, this is being used with an older codebase that predates those RedBeanPHP naming conventions and as such they've been disabled via RedBeanPHP\Util\DispenseHelper::setEnforceNamingPolicy(false);.

The existing code also heavily relies on such a naming scheme and doesn't have any issues.

gabordemooij commented 9 months ago

It does not get stored because you set the id. RedBeanPHP internally determines whether to use INSERT or UPDATE based on the value of the id.

The following example works over here:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'rb.php';
RedBeanPHP\Util\DispenseHelper::setEnforceNamingPolicy(false);
R::setup('mysql:host=localhost;dbname=redbean','root','root' );
$user = R::dispense('openstack_users');
$user->client_id = 999;
$user->password = "byv399nc95zf3gre89ygmmkgbywi3forgj48smduxtpq5hwzer43yhcggae547jy";
//$user->id = "ee13bd1eab274ed3ba5c766c233ad208";
$user->project_id = "2102b340ed6a48d7836eebb41e91a49e";
R::store($user);
print_r(R::getAll('select * from openstack_users'));

Gives:

Array
(
    [0] => Array
        (
            [id] => 1
            [client_id] => 999
            [password] => byv399nc95zf3gre89ygmmkgbywi3forgj48smduxtpq5hwzer43yhcggae547jy
            [project_id] => 2102b340ed6a48d7836eebb41e91a49e
        )

)
BelleNottelling commented 9 months ago

Ahhh that makes sense. Thank you for the clarification @gabordemooij