CakePHP converts MySQL integers to strings ... messing up rand () Function

I want to get a random selection of data from my database using CakePHP. Here is my function:

function categories_list() 
{   
    $this->paginate['limit'] = 6;
    $this->paginate['order'] = '';
    $this->paginate['conditions'] = '';

    // Sort Randomly Start 
    if ($this->Session->check('Category.randomSeed')) 
    { 
        $seed = $this->Session->read('Category.randomSeed'); 
    } else { 
        $seed = mt_rand(); 
        $this->Session->write('Category.randomSeed', $seed); 
    } 
    $this->paginate['order'] = sprintf('RAND(%d)', $seed); 
    // Sort Randomly End 

    $this->set('cat_ajax_items', $this->paginate('Category')); 
}

The problem is that the request that Cake sends to the database always does this with the RAND () part, sending MySQL in a flexible form:

ORDER BY RAND(`1235123412341`)

Testing by manual request, it works very well and returns a sample when it is formatted as follows:

ORDER BY RAND(1235123412341)

Is there a way to make Cake refuse autoformatting? Everything that I put into this RAND () function is reset to string quotes.

+3
source share
2 answers

Everything that I put into this RAND () function is reset to string quotes.

, . , , . , CakePHP , . :

"RAND('%d')"

SQL:

ORDER BY RAND('1235123412341')

, .

+4

,

:)

- PHP typecasting, : is_int ('01234'), , - "" , 0 - ( , , )

CakePHP,

CakePHP, CodeIgniter escape():

if (is_string($str))

... :

if (is_string($str) && (mb_strlen((int) $str) != strlen($str)))

... :)

P.S.: (int) $str === $str,

0

Source: https://habr.com/ru/post/1788657/


All Articles