Find, or insert new in DB

Now I’ve been struggling for quite some time, and I do not get it.
I’ve got this

$email = "email@domain.com";
// email@domain.com does not exist in the database
$db = new Database(array(
// db config
));
$users = $db->table("users");
$user = $users->where(array("email" => $email))->first();
if (!$user) {
	// Should return an ID, eg. 3
	return $users->insert(array(
		"email" => $email
	));
} else {
	return $user->id();
}

The table is simple, two fields: id and email.
It returns the id of the user if it finds the email in the database, but if it doesn’t, I want it to insert the email to the database and return the new id

I’ve been going at this for some time today, and I simply cannot find a solution.
I tried with both SQLite and MySQL, with no luck. So it’s probably my doing that’s wrong.
Though I reckon a fresh mind of you guys will find a solution… :joy:

Well, looks like I managed it myself… :smile:

$users = $db->table("users");
$user = $users->where(array("email" => $email))->first();
$db->table("users")->insert(

instead of

$users = $db->table("users");
$user = $users->where(array("email" => $email))->first();
$users->insert(

Would anyone please elaborate why I can’t reuse $users?

1 Like

Looks like a bug to me, I will look into this.

1 Like

I have checked this and it seems like the resetting was incomplete. Could you please check if reusing the query works after applying the fix from this PR? Thanks for your help in advance.

1 Like

Ok, give me 10-20 minutes and I’ll test it

Perfect, it works :heart_eyes:

Great, thanks for testing. :slight_smile:

i also have lots of trouble with my code.

for php 5,4 i had everything working perfectly, but since transitioning to php 7, i encounter errors (no error output) but the code seems not to be working, (with updated toolkit as well)

i tried many diffrent combinations but it just seems like a few toolkit functions do not seem to work like before.

        if($email = get('email')){        
            if(!v::email($email)){
                echo "<div class='bold center p2 mb2 white bg-yellow rounded'>Bitte geben Sie eine gültige eMail-Adresse ein.</div>";
            } else {
                if($users->findBy('email', $email)->email()){
                    echo "<div class='bold center p2 mb2 white bg-red rounded'>E-Mail $email bereits eingetragen.</div>";
                } else {
                
                    echo "bla bla bla";
                         
                    }
                    
                }
                
            }    

while it outputs the email already exists error, it does not seem to work if the email doesn’t exist, the page will just stay blank, no error whatsoever. i am already reducing my code to just simple trial and error to see where the problem lies.

i have updated to 2.3.1 github toolkit which was stated above.

But as far as I can tell your code doesn’t use the database classes, does it?

$db = new Database(array(
  'type'     => 'sqlite',
  'database' => 'db/file.sqlite'
));
$users = $db->table('users');
$logs = $db->table('logs');

inside another file which is included, they seem to be working generally, as it reports that if email inside my database is the same as the form input, it’ll show the error. as i said. it all has been working fine before with 5.4. (and older toolkit?)

Hm, without specific code that broke and a detailed error message we can’t really help you unfortunately. :slight_frown:

indeed, it seems like when i just code all diffrent kinds of operation like delete, add and update, it works like a charm. i guess i have to investigate more…

Just for reference: You will no longer need the workaround as this fix will be part of Kirby 2.3.1. :slight_smile:

1 Like

I just liked this everywhere you posted it. Loving it :slight_smile:

Bugfixes are always great, aren’t they? :wink:
But no need to like this everywhere. :smiley:

1 Like