Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM unexpected results when adding
  • I have a model that tracks views. It's a very basic model as you can see below. There are currently only 2 methods. One queries the database, and one to add to the views table.

    The function that checks the table returns either a view object, or null. It's a static method called inside of the save_view method. If the save_view method returns null, a new view is built, but if it returns an object, the view property is pulled, and a 1 is added to it, then saved to the database.

    Here is where it gets weird.

    The new build works fine. It just initializes and saves a row with 1 view. If I use the view object that's already been initialized, and add 1 to the view, it is saving as 2. I have no idea what is going on.

    If I interrupt the flow with Debug::dump($view); it saves by 1 as expected.

    So, for example, if the row currently has 3 views, and I do $view->set('views', ($view->views + 1));, you would think that it would save as views = 4, but it's skipping ahead to views = 5. When interrupted it saves as views = 4.

    I've tried this a whole variety of ways, nothing seems to be working. I can get them all to save, but I'm still getting the same unexpected results. Am I missing something basic here, is it possible the code is flowing too fast, or have I discovered a bug? Never seen anything like this before. Thanks for taking a look.

    I was having a really hard time trying to post this here. I tried a couple times and ended up taking it over to stack overflow. 

    The code in question: http://stackoverflow.com/q/30541286/758554

  • HarroHarro
    Accepted Answer
    Lets keep the discussion in one place, see my reply on StackOverflow.
  • My apologies, Harro. Thank you for answering my question.  
  • Okay, it's back, and i'm going completely mad. I have grepped my entire project, and I cannot find where this is coming from! For some reason, the call to Model::find is happening twice, or the call to the controller action I am not quite sure. If I use Debug::backtrace() or Debug::dump() I don't have an issue.I originally thought it might be a relational call, so I've ripped out all of my class and it's still occurring.
    Here is the stripped down version of my controller action: 


    public function action_view($id = null) {

                   $joke = Model_Joke::find($id);
                   Log::info('Joke: ' . serialize($joke));
                   Debug::backtrace();
                   $this->template->title = 'joke';

        $this->template->content = View::forge($view, $data);

    }
    with debug commented out in my log, i get 2 calls, the first is a serialized array, the second is just N. 


    INFO - 2015-05-30 17:59:27 --> Joke: N;


    My MySQL database is getting hit twice as well. And this is even more confusing. For some unbeknownst reason, There is a query being called for 'favicon'. I have absolutely no idea why this is occurring. As you can see, the first call grabs the joke and is perfect, the second call is looking for a joke with an id of 'favicon'?


    150530 17:59:26  951 Connect dmotivat_casper@localhost on dmotivat_dmotivated
     951 Query SET NAMES 'utf8'
     951 Query SELECT `t0`.`id` AS `t0_c0`, `t0`.`user_id` AS `t0_c1`, `t0`.`title` AS `t0_c2`, `t0`.`body` AS `t0_c3`, `t0`.`url_id` AS `t0_c4`, `t0`.`ip` AS `t0_c5`, `t0`.`created_at` AS `t0_c6`, `t0`.`updated_at` AS `t0_c7`, `t0`.`active` AS `t0_c8` FROM `jokes` AS `t0` WHERE `t0`.`id` = '14' LIMIT 1
     951 Query SELECT * FROM `users` WHERE `username` = 'casper'
     951 Quit
    150530 17:59:27  952 Connect dmotivat_casper@localhost on dmotivat_dmotivated
     952 Query SET NAMES 'utf8'
     952 Query SELECT `t0`.`id` AS `t0_c0`, `t0`.`user_id` AS `t0_c1`, `t0`.`title` AS `t0_c2`, `t0`.`body` AS `t0_c3`, `t0`.`url_id` AS `t0_c4`, `t0`.`ip` AS `t0_c5`, `t0`.`created_at` AS `t0_c6`, `t0`.`updated_at` AS `t0_c7`, `t0`.`active` AS `t0_c8` FROM `jokes` AS `t0` WHERE `t0`.`id` = 'favicon' LIMIT 1


    I have grepped my whole project, and cannot find where this call is coming from, or why it's happening. 

    If i use the Debug methods, only 1 call to the DB occurs. I believe the only reason I even discovered this situation is because of the counter I put in. 

    I don't know if it matters or not, i'm using version 1.7

    I would deeply appreciate any thoughts, I'm going mad! Also, I tried to adjust the format, sorry. 

  • If you have a controller being called twice, your first port of call is a missing asset.

    If you use the started .htaccess ruleset, it checks first if you request an existing file or directory, for example http://example.org/assets/img/someimage.jpg. If this file does not exist, it falls through and the request is rewritten to index.php, causing a second request.

    I always change these rules to exclude the assets folder, so you get a standard 404 when you have a missing asset:

        # Send request via index.php if not a real file or directory
        RewriteCond %{REQUEST_URI} !^/assets
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d

  • Hey Harro,
    I was using the standard .haccess ruleset, but I've updated it to what you have shown above. Thanks for the tip. But, unfortunately, that did not resolve the situation. The favicon is a static request in my Html, not an Asset:: call. 
    Here is my head block:
     <head>
            <meta charset="utf-8">
            <title><?= '[D]Motivated | ' . $title; ?></title>
            <link rel="icon" href="favicon.ico" type="image/x-icon" /> 
            <?= Asset::css('bootstrap.css'); ?>
            <?= Asset::css('main.css'); ?>
            <?= Asset::js('jquery.min.js'); ?>
            <?= Asset::js('functions.js'); ?>
            <?= Asset::js('jquery.lightbox-0.5.js'); ?>
        </head>

    I also don't really understand why 'favicon' is being passed to the controller. That is probably the most perplexing thing to me. There is not a single call to it anywhere in my code. And as you can see, it's a static element. 

  • I went ahead and set my logging threshold down to L_ALL, this is the response in my log:

    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::__construct - Creating a new main Request with URI = "jokes/view/14"
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Called
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Setting main Request
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::__construct - Creating a new main Request with URI = "jokes/view/favicon"
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Called
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Setting main Request
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::__construct - Creating a new main Request with URI = "jokes"
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Called
    INFO - 2015-05-31 09:51:34 --> Fuel\Core\Request::execute - Setting main Request

    All I did was change the threshold, and refresh my browser. It looks like it's being called 3 times, or am I reading this incorrectly? It's weird, but I have a user's section set up too, and sometimes it'll bring up a user, but will also have a session message saying that it could not locate the user, which is only set if is_null($user).
  • Check your webserver log to see what is exactly requested, or use network tools like those present in Chrome or Firefox (tools, web developer, network).

    Favicons are tricky business, there are several ways of defining one in the header, and then still some browsers just request it assuming it's in the root, no matter what you have defined in the HTML header.
  • Yeah, I've never had an issue with something like this before. I've always read that they are assumed to be in the root, so that's where you put them. I went ahead and defined an absolute path, so that should remedy the situation. I guess it was a coincidence that changing those properties to static seemed to have an effect. I didn't think it would have mattered either, but figured I'd give it a shot. As always, thanks for your help, patience, and guidance. 

    For anyone else with the same or similar issue, I changed:
    <link rel="icon" href="favicon.ico" type="image/x-icon" />
    to:
    <link rel="icon" href="<?= echo Uri::base(false); ?>favicon.ico" type="image/x-icon" />
  • For a really depressing read on the subject: http://www.jonathantneal.com/blog/understand-the-favicon/ ;-)
  • Your right, that is a depressing read.... Thanks for the link!

Howdy, Stranger!

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

In this Discussion