Closed ipsod closed 2 years ago
Can you share your code? It's difficult to assess or debug without seeing your model's update()
method. It seems strange that update()
is not being called, and I have never encountered that, so I would lean towards the issue being somewhere else, rather than in RedBean.
It sounds as though you're perhaps struggling with the state of PHP at the moment, but to be honest, it's a much better ecosystem than it was a few years ago. If you're yearning for the 'good ol' days' where you had control over every part of a system, why not roll your own ORM and tools?
Hey @ipsod I gave it a simple try with the following code sample and everything is working as intended:
<?php
include 'rb-sqlite.php';
R::setup( 'sqlite:/tmp/sqlitetest.db' );
R::freeze( FALSE );
R::nuke();
R::usePartialBeans( TRUE );
class Model_User extends Redbean_SimpleModel {
public function update() {
echo "Update called." . PHP_EOL;
}
}
$user = R::dispense('user');
$user->isactive = true;
R::store($user); // "Update called."
$user2 = R::dispense('user');
R::store($user2); // "Update called."
$user2 = R::load('user', 2);
var_dump($user2->isactive); // NULL
$user2->isactive = false;
R::store($user2); // "Update called."
Could you please share the code that doesn't work for you ?
I can't recreate the issue (suspect it was my own fault, but can't be sure), except that the second call to R::store
, below, does not call update
(since the bean hasn't changed). This is one of the things that was surprising to me, and I checked the doc page for models when troubleshooting, but it only says "However, now we need to add this check everytime we call R::store()" (emphasis in docs, with no warnings to the contrary anywhere on the page).
<?php declare(strict_types=1);
require_once('../rb_new.php'); # https://redbeanphp.com/downloads/RedBeanPHP5_7_1RC2.tar.gz
R::setup( 'sqlite:/tmp/sqlitetest.db' );
R::freeze( FALSE );
R::nuke();
define('REDBEAN_MODEL_PREFIX', '');
class User extends RedBean_SimpleModel {
public function update() {
echo ' update()';
}
}
$user = R::dispense('user');
echo "\nsku ";
$user->sku = 5;
echo $user->box()::class;
R::store($user); // update called
echo "\nno change ";
$users = R::findAll("user");
foreach($users as $user) {
R::store($user); // update not called
}
echo "\nnull to false";
$user2 = R::dispense("user");
$user2->archived = False;
echo " setup:";
R::store($user2);
echo " now update: ";
$users = R::findAll("user");
foreach($users as $user) {
$user->archived = $user->archived ? $user->archived : False;
R::store($user); // update called twice (once per user)
}
@benmajor
It sounds as though you're perhaps struggling with the state of PHP at the moment, but to be honest, it's a much better ecosystem than it was a few years ago. If you're yearning for the 'good ol' days' where you had control over every part of a system, why not roll your own ORM and tools?
Yes, that's just about where I've ended up, but still hanging on to RedBean. I didn't write an ORM before, if I remember right, just PDO calls. I guess that's an option.
Why would R::store do anything if there is nothing to be done?
In my case, I had added something to the $model->update()
method that would make it so that there was something to be done (changing null
values to false
). I was trying to batch process them and tried R::store($model)
, which didn't work (took a little head scratching to figure out why - I read over the docs for models, and found nothing except the line I quoted above, which claims that update
is called "everytime" R::store is called), and rb.php
with 10k lines of code kills my editor's performance, so I'm reluctant to go digging there, though now I see it's time to fix that. So I tried $model->value = $model->value ? $model->value : false; R::store($model);
, which didn't work either (took a lot of head scratching to figure out why - still not sure if it was RB or me messing up). I actually got the null
values updated to false
by also adding $model->fake_column = "fake data"
to the call above.
If your editor trips over 10k lines of code you need another editor, try geany, nano or vim.
https://github.com/gabordemooij/redbean/commit/b92986996c3e640ac5010296977624674fb8a606
Added a note in manual about this lazy behavior if store()
When
$product->archived == null
, setting$product->archived = false
, then callingR::store($product)
does not actually save the changes to the database, and also does not call the update method on the model. Even had I not updated anything at all, I still feel like it's counterintuitive that callingR::store($product)
does not call$product->update()
, and it should be should be noted in the documentation that there are conditions for whether or not callingR::store
does call$model->update()
.Am I missing some sort of crucial debugging features or practices? I keep running into problems like this that seem like they should be simple, but take me an hour or more to solve.
Always between discovering the problem and finding the solution, I doubt the tool - usually around my second hour of troubleshooting - thinking I should just scrap the project and rebuild in Django, where I don't recall ever having such confounding issues. I don't mean to complain, just wish I knew more, because I am building important parts of my life on this technology, but I know so little and have so little time. I wish I knew which tech stack was the "right" choice. My primary problem with Django was that it's difficult to deploy without either learning devops or tying myself in to one of the very few PAAS (compared to how ubiquitous PHP managed hosting is). I guess I also missed my old days of PHP, where I built and understood the app from the ground up, and I feel like I'm there again, but I'm also realizing how much Django and its libraries did for me that I now have to do myself, and how easy it was to find solutions to problems with Django, and how rarely (never?) it ever did anything that surprised me in a bad way, like RedBean has done a few times now. With Django and Python, once I got going, everything was just at the tip of my fingers, because the Python ecosystem in general is just so standardized and predictable in syntax and functionality. With PHP, it feels like each little library is writing its own flavor of PHP, and even the language itself is full of little idiosyncrasies that keep you on your toes.