Laravel Eloquent sql statement after exist check is limited to 1 - laravel

I've found a really weird behaviour in the current laravel 4.2 patch. Until now whenever I want to get some datas from my eloquent model I check if results are available before i get them via the following code example:
$foo = $model->hasManyRelationFunction();
if($foo->exists())
foreach($foo->get() as $elem)
....
this results in the following sql statement:
select count(*) as aggregate from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' limit 1 <-- exist() ?
following by the real select sql statement without Limit 1, for example:
select * from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' order by "id" asc
Since i've updated laravel 4.2 to the current patch, it also limits the real sql statement to 1
`select * from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' order by "id" asc` limit 1
but ONLY when I make the if($foo->exists()) check. As soon i comment out the exist condition everything works fine. Are their any informations about this behaviour or am I just stupid? :D
Edit: It seems like they made the eloquent builder "more fluent" in patch 4.2.13. I still dont know if this is a bug or a feature, imo this shouldnt be the normal behaviour.
/**
* Determine if any rows exist for the current query.
*
* #return bool
*/
public function exists()
{
$limit = $this->limit;
$result = $this->limit(1)->count() > 0;
$this->limit($limit);
return $result;
}

In fact you don't need to check existence. You should only do:
foreach ($model->hasManyRelationFunction as $elem) {
// do whathever you want
}
and it's just enough to get the data. You don't need to check existence here or if you think you do, you should show a real example of a code what you are trying to achieve.

Related

Paginate count(*) query issue

Laravel Version: 8.0
PHP Version: 7.3.0
Database Driver & Version: MySQL 5.7.34
Describe the Bug
When I use paginate for pagination the data it call the count( * ) query every time even I pass the column name like count(['id']), and the count( * ) query scan all row in the table.
Table name is users and it has 45 column
Route
Route::get("users", "UsersController#index");
Controller
namespace App\Http\Controllers\Api\V1;
class UsersController extends Controller
{
public function index()
{
return User::paginate(10 , ['id']);
}
}
Call users route
Telescope showing me that two queries
Actual result
Expected result
Steps To Solution:
The following image shows that I had done changes as per our functionality, It will take the first column name from the passed in the paginate method array of $columns params and that query does not scan all columns of the users tables.
Final Results:
I have tired to solving this issue any other way or idea then please let me know
Its not recommended to ever touch the vendor files, you can always just override the functionality inside of your model, you can also pass in the columns to override the getCountForPagination() and you can also pass the columns to simplePaginate() that doesn't invoke any counting!
In order to optimize the query to count and paginate, you can do it like this:
//We will call the query on the model
$program = Program::query()->getQuery();
//count the query by specific columns
$thePaginationCount = $program->getCountForPagination(['id']);
//paginate the results using simplePaginate and select the columns we want:
$thePaginatedProgram = $program->simplePaginate(10, ['id', 'name']);
return 'test: '.$thePaginatedProgram;
Will result like this:
select count(`id`) as aggregate from `programs`
select `id`, `name` from `programs` limit 11 offset 0
As you can see it will only load what we specify and its very efficient!
Final Note:
If you just want to paginate without the count, you can always call Model::simplePaginate($amount, [$columns...])
https://laravel.com/docs/9.x/pagination#simple-pagination

CodeIgniter UPDATE and LIMIT

I know this question has been posted already before Update with limit 1 in codeigniter use active record
But it seems combining UPDATE and LIMIT using Active Record doesnt work on my side. It will still update all record based on the WHERE clause. I'm currently using CodeIgniter 3.0.6. Is this a bug from CodeIgniter already?
screenshot of the query
You cannot directly add limit in update query. I get an alternative way. Firstly you get one row by where query and then run the update query with using fetching row unique key like unique auto increment id.
$singleInfo = $this->db->get_where($tbl, array('user_id'=>1));
$this->db->update($tbl,array('username'=>'FredSmith'),array('user_id'=>$singleInfo->id));
Well I fired up CI 3.0.6 and used this as a demonstration, as we cannot see your setup.
public function db_update_test_with_limit(){
$tbl = 'users';
// Use Chaining - Works
$this->db
->where('user_id',1)
->limit(1)
->update($tbl,array('username'=>'FredSmith'));
echo $this->db->last_query();
// Use discrete calls - Works
$this->db->where('user_id',1);
$this->db->limit(1);
$this->db->update($tbl,array('username'=>'FredSmith'));
echo '<br>';
echo $this->db->last_query();
}
And the results for both are...
UPDATE `users` SET `username` = 'FredSmith' WHERE `user_id` = 1 LIMIT 1
UPDATE `users` SET `username` = 'FredSmith' WHERE `user_id` = 1 LIMIT 1
So if you are doing the same thing and getting different results, then something funky is going on.
If you use this as a test and massage it to suit your situation, does it work or not?

Codeigniter HMVC + ion_auth trouble loading the config items

I have been banging my head for 5 hours and I finally solved the problem but I just cannot go to sleep without knowing the reason. Let me explain the issue first.
I have used codeigniter HMVC extension and installed ion_auth as a separate module.
|-modules
|--auth
|---config
|-----ion_auth.php
|---controllers
|-----auth.php
|---models
|-----ion_auth_model.php
|---views
When I was trying to get a user's group I started to get wired SQL errors. Then I narrowed the issue and figured out that the items in config/ion_auth.php were not loaded in the ion_auth_model.php file.
ERROR - 2016-02-24 20:09:26 --> Query error: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'as id, .name,
.description JOIN ON .=.id WHERE . = '2'' at line 1 - Invalid
query: SELECT . as id, .name, .description JOIN ON .=.id
WHERE . = '2'
Then I tried couple of stuffs and when I remove the index 'ion_auth' from
couple of method calls in ion_auth_model.php everything started to work.
I changed
$this->tables = $this->config->item('tables', 'ion_auth');
$this->join = $this->config->item('join', 'ion_auth);
to
$this->tables = $this->config->item('tables');
$this->join = $this->config->item('join');
Can anyone tell me why it worked?
This is the inner implementation of the CodeIgniter function config->item() found in the file system\core\Config.php
/**
* Fetch a config file item
*
* #param string $item Config item name
* #param string $index Index name
* #return string|null The configuration item or NULL if the item doesn't exist
*/
public function item($item, $index = '')
{
if ($index == '')
{
return isset($this->config[$item]) ? $this->config[$item] : NULL;
}
return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
}
When you pass the $index parameter, the function checks if both parameters are initialized in the config and returns config[$index] of the CI instance; or null if any of them is not initialized.
If config[$item] is not set in the CI instance, the function returns always null. Let's assume this is not the case, as your call don't crash when avoiding $index.
So when you pass $index as the second parameter, your code crashes because the function returns null, and that means that the config[$index] of the CI instance is not set. Now the question is why it's not set, and I can't help you here, but it looks like you are missing to load some modules.
Best regards

Get permission data from Database

I'm having problems to do a simple permission system on my Webapp. My DB has a table called "usuario" that has informations about the users of the system. One of these columns is called "privilegio" that has value '0' for administrators and 1 for regular users. An administrator has the power to Add and edit users on the system. Im trying to take this behavior querying my database with the cod of the logged user and getting its permission. If the user is not on the administrator group (privilegio=1) then the add/edit/delete buttons will be unset.
public function usuario() {
if($this->session->userdata('logged')){
$crud = new grocery_CRUD();
$crud->set_subject("Usuário");
$crud->set_theme('datatables');
$crud->set_table("usuario");
(...)
$crud->field_type('privilegio','dropdown',array('0'=>'Administrador','1'=>'Usuario'));
(...)
$this->db->select('privilegio');
$this->db->get('usuario');
$result = $this->db->where('cod_func',$this->session->userdata('cod_func'));
if(!$result){
$crud->unset_add();
$crud->unset_edit();
$crud->unset_delete();
}
(...)
The problem (and the question) is that this code only list the user that is logged on, not the others already registered on the system and stored on "usuario" table. I wonder that the list is been made by my query (what is not the behavior I would like) I hope you could undestand my doubt. Sorry for my bad english.
Thank you!
you're having trouble with the active record functions...
When you use the function
$this->db->get('usuario');
This translates to the query:
SELECT * FROM usuario
So try changing your code to something like this:
$this->db->select('privilegio');
$this->db->from('usuario');
$this->db->where('cod_func',$this->session->userdata('cod_func'));
$this->db->limit(1); //You're only expecting one result
$result = $this->db->get(); // Save the result to the variable result
//Edited for the comment, something like
$result = $result->first_row('array'); //Here I'm fetching only the first row into an array
$privilegio = $result['privilegio']; //Im saving the result from the query in the variable $privilegio
This translates to:
SELECT priviliegio FROM usuario WHERE cod_func = 'some_value' LIMIT 1;
Then you can do whatever you want with the $result variable, please refer to documentation to see what you can do...
Generating Query Results

Preventing Doctrine's query cache in Symfony

In my Symfony/Doctrine app, I have a query that orders by RANDOM(). I call this same method several times, but it looks like the query's result is being cached.
Here's my relevant code:
$query = $table->createQuery('p')
->select('p.*, RANDOM() as rnd')
->orderBy('rnd')
->limit(1)
->useQueryCache(null)
->useResultCache(null);
$result = $query->fetchOne();
Unfortunately, the same record is returned every time, regardless of me passing null to both useQueryCache and useResultCache. I tried using false instead of null, but that didn't work either. Lastly, I also tried calling both setResultCacheLifeSpan(0) and setResultCacheLifeSpan(-1), but neither call made a difference.
Any insight on how to prevent caching since I want a different random row to be selected each time I call this method?
Edit: I also tried calling clearResultCache(), but that just ended up causing an error stating: "Result Cache driver not initialized".
Edit 2: As requested, here's the SQL generated by calling $query->getSqlQuery():
SELECT c.id AS c__id, c.name AS c__name, c.image_url AS c__image_url,
c.level AS c__level, c.created_at AS c__created_at, c.updated_at
AS c__updated_at, RANDOM() AS c__0 FROM cards c ORDER BY c__0 LIMIT 1
It turns out I'm a moron. I tried to simplify my query for this question, and in doing so, I didn't capture the true cause. I had a where() and andWhere() call, and the combination of conditions resulted in only one possible record being matched. Thanks for taking the time to respond, everyone, sorry to have wasted your time!
Doctrine also caches entities you created in the same request/script run.
For instance:
$order = new Order();
$order->save();
sleep(10); // Edit this record in de DB in another procces.
$q = new Doctrine_Query();
$result = $q->select()
->from('Order o')
->where('o.id = '.$order->id);
$order = $result->getFirst();
print_r($order->toArray());
The print_r will not contain the changes you made during the sleep.
The following code will remove that kind of memory cache:
$manager = Doctrine_Manager::getInstance();
$connection = $manager->getCurrentConnection();
$tables = $connection->getTables();
foreach ( $tables as $table ) {
$table->clear();
}
PS: Added this answer because I found this topic trying to resolve above issue.

Resources