Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Debug CRUD app

edited February 2013 in Apps

I used elefant crud-app to create an app, but I then tweaked and fiddled so much that adding a record no longer works. However, I'm not sure why. What's the best way to debug this? I have debugging turned on in config.php, but I suspect that this is an SQL issue, not a php one. Is there a way to see the SQL that's being sent to the database and/or the response from the DB server?

Comments

  • There's no SQL logging beyond MySQL's general log, but the DB class does store the last statement and parameters so you can access them like this:

    <?php
    
    $p = new blog\Post;
    
    $posts = $p->headlines ();
    
    info (DB::$last_sql);
    info (DB::$last_args);
    
    ?>
    

    The above shows this output:

    select `id`, `ts`, `title` from `elefant_blog_post` where `published` = ? order by ts desc limit 10 offset 0
    Array
    (
        [0] => yes
    )
    
  • I'm using sqlite but maybe I should switch to mysql for logging.

    I added the info() commands to the add handler for the app (apps/properties/handlers/add.php) but all I got was select * from elefant_user where session_id = ? and expires > ?, so I commented out $this->require_admin ();, but now the info() response is empty.

    So I guess the form $_POST is not being sent to the db, but I'm not sure why not.

  • Do you still have a put() call in the form handling? That's where the actual database call is triggered. You can also see SQL errors in DB::error () or in the model object's error property.

  • Yes. Here is the handler currently:

    $this->require_admin ();
    
    $page->layout = 'admin';
    $page->title = __ ('Add Property');
    
    $form = new Form ('post', $this);
    
    echo $form->handle (function ($form) {
        // Create and save a new property 
        $properties = new properties\Properties (array (
            'name' => $_POST['name'], 
            'type' => $_POST['type'], 
            'title' => $_POST['title'], 
            'headline' => $_POST['headline'], 
            'image' => $_POST['image'], 
            'teaser' => $_POST['teaser'], 
            'description' => $_POST['description'], 
            'directions' => $_POST['directions'], 
            'area' => $_POST['area'], 
            'price' => $_POST['price'], 
            'status' => $_POST['status'], 
            'brochure' => $_POST['brochure'], 
            'plat' => $_POST['plat'], 
            'google_map' => $_POST['google_map'], 
            'on_hold' => $_POST['on_hold'] 
        ));
        $properties->put ();
    
        if ($properties->error) {
            // Failed to save
            $form->controller->add_notification (__ ('Unable to save properties.'));
            return false;
        }
    
        // Save a version of the properties 
        Versions::add ($properties);
    
        // Notify the user and redirect on success
        $form->controller->add_notification (__ ('Property added.'));
        $form->controller->redirect ('/properties/admin');
    });
    
  • Hmm, it looks like as long as the form is submitted and passes validation, then it should reach your put() statement. If you add the info() calls right after the put() like this, does it output anything?

    $properties->put ();
    info ($properties->error);
    info (DB::$last_sql);
    info (DB::$last_args);
    return;
    

    The return should prevent it from re-rendering the form, or redirecting to /properties/admin, so you can see the output.

  • No, it just reloads the add screen.

  • Sounds like the input validation might be getting tripped up. What do you have in your apps/properties/forms/add.php file?

  • ; <?php /*
    
    [name]
    not empty = 1
    
    [title]
    not empty = 1                                                                                        
    
    [headline]
    not empty = 1
    
    [teaser]                                                                                             
    not empty = 1                                                                                        
    
    [description]
    not empty = 1
    
    [price]
    not empty = 1
    
    ; */ ?>
    
  • You're right -- that was it. I took out everything but the first and last lines and now the info() commands show up.

  • edited February 2013

    D'oh! I had changed body_text to description but not updated the name of the field or the error notice id.

  • Glad it's working :)

  • Given that I'm sure I made a mistake (since the app works just created) I wonder why I do not receive any validation/error messages.

    debug is on

    display_errors is on

    I tryied also

    info ($contact->error);
    info (DB::$last_sql);
    info (DB::$last_args);
    return;

    after $contact->put (); with no luck.

    Thanks.

  • What is it outputting, if anything? And is there anything in the web server error log? And can you post a sample of the code?

  • ?!?! I realize just now that, for many days, I do not receive any email notification of replies. The problem is now solved and the error forgot, sorry. Can we, please, check why I no longer receive notifications?

  • I'm not sure... and I'm guessing they're not in your spam folder ;)

    I only seem to get email notifications for threads that I started, which as a forum admin kind of bugs me. I've looked for a 'notifications' add-on to Vanilla Forums to add more options, but haven't found one yet.

    I've considered finding a different forum app such as Discourse, or writing one for Elefant. But the last option would probably take quite a while before I could even start...

    But I should really ask what you guys think!

  • I only seem to get email notifications for threads that I started, which as a forum admin kind of bugs me. I've looked for a 'notifications' add-on to Vanilla Forums to add more options, but haven't found one yet.

    Would it be helpful if we tagged you when we post and/or start a new thread?

  • ?!?! I realize just now that, for many days, I do not receive any email notification of replies. The problem is now solved and the error forgot, sorry. Can we, please, check why I no longer receive notifications?

    The work-around is to bookmark the thread. Then you'll get notifications even if you didn't start it.

  • Haha somehow I missed that... :P

  • Sorry for having realized only later.

    I'll try to remember to bookmark the thread.

Sign In or Register to comment.