We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48272
    • 24 Posts
    I have a BLOB field called "file". I want to create a link in a chunk when the file is not empty. I tried this:

    [[!+file:notempty=`<a href="[[~67? &reqId=`[[+id]]`]]">Download</a>`]]


    It seemed to work Ok, but when a user uploaded a large file I got an error message: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1084409 bytes) in ...

    Is there an other way to create a link only when the blobfield is not empty?!

    Thanks in advance.

    (I am using MODX Revolution 2.3.3-pl (traditional))
      • 3749
      • 24,544 Posts
      I think you'd need a custom modifier or (better) a custom snippet. Something like this:

      [[!CheckFile? &disk_file=`[[+file]]` &doc_id=`[[+id]] ]]


      $file = $modx->getOption('disk_file', $scriptProperties, null, true);
      $docId = $modx->getOption('doc_id', $scriptProperties, null, true);
      clearstatcache();
      if (empty($file) || empty($docId) || (!file_exists($file)) {
         return '';
      }
      
      return '<a href="[[~67? &reqId=`' . $docId . '`]]">Download</a>';


      Depending on the nesting levels involved, you could run into problems with the parsing order.

      I'm assuming that the file won't exist. If it might exist but be empty, you'd need to add a call to filesize().
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • It's a BLOB in the database. The parent post's placeholder is trying to load it into memory. You'll need a custom SELECT query to determine in the query's SQL itself if the field is empty.

        SELECT file 
        FROM tablename
        WHERE file <>''
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 48272
          • 24 Posts
          Quote from: BobRay at Apr 14, 2015, 07:47 PM
          I think you'd need a custom modifier or (better) a custom snippet. Something like this:

          [[!CheckFile? &disk_file=`[[+file]]` &doc_id=`[[+id]] ]]


          $file = $modx->getOption('disk_file', $scriptProperties, null, true);
          $docId = $modx->getOption('doc_id', $scriptProperties, null, true);
          clearstatcache();
          if (empty($file) || empty($docId) || (!file_exists($file)) {
             return '';
          }
          
          return '<a href="[[~67? &reqId=`' . $docId . '`]]">Download</a>';


          Depending on the nesting levels involved, you could run into problems with the parsing order.

          I'm assuming that the file won't exist. If it might exist but be empty, you'd need to add a call to filesize().

          It is not a file on disk, but I have to check if the BLOB-field is NULL. "notempty" seems to load the BLOB-field into memory. Is there an outputmodifier isnull? Or do I have to use a snippet approach like http://www.sepiariver.ca/blog/modx-web/wrap-%28notempty%29-modx-fields/?
          • A placeholder of any kind is going to be loading the BLOB field into memory, then determining if the result is an empty string or NULL or whatever. You won't be able to use output modifiers or placeholders.

            I presume you are trying to generate links for streaming or downloading these files? If that is the case, you need to run a query to return something besides the BLOB field if the BLOB field is empty or not.

            SELECT CASE WHEN file IS NULL OR file = '' 
                   THEN 'nofile' 
                   ELSE 'isfile' END AS file
            FROM tablename.


            This query should return "nofile" as the result if the field is empty, and "sifile" if it's not empty. This would be the return value of the query, not the value in the field if it is not empty. Now you could use a placeholder and an output modifier to check the value without trying to load the whole BLOB into memory.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 3749
              • 24,544 Posts
              This should also do it if you're sure the field is null when empty:

              select isnull(file) from mytable


              This will return true if the field contains null.

              This would be more reliable, but might read the field into memory:

              WHERE file > ''
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 48272
                • 24 Posts
                I forgot to mention that I use this chunk in a bloX-call. The output modifier is in "tplRequest".

                [!bloX?
                &packagename=`Clients`
                &classname=`Request`
                &where=`{"state:=":"1"}`
                &tpls=`bloxouter:tplRequestOuter||row:tplRequest`
                &limit=`30`,
                &debug=`0`
                ]]

                I don't know if it is possible and wise to create my own query to add an extra field "nofile".
                  • 3749
                  • 24,544 Posts
                  I think this might work. I'm not sure how bloX handles empty results and it still might give you the memory error, but it's worth a try:

                  &where=`{"state:=":"1","file:>":"" }`


                  You're just reading from the DB so I don't think you can hurt anything. If you're worried, though, you could do a DB backup first.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting