Auth assignment database call yii2 - php

I am using Yii2 and using the yii\rbac\DbManager for auth assignment.
I was looking at the logs to see where all the database calls are coming from and this query
SELECT `b`.* FROM `auth_assignment` `a`, `auth_item` `b` WHERE
((`a`.`item_name`=`b`.`name`) AND (`a`.`user_id`='91')) AND (`b`.`type`=1)
Keeps running again and again, sometimes 10/15 times in succession.
I have added
'authManager' => [
'class' => 'yii\rbac\DbManager',
'cache' => 'cache'
],
As the docs say that will cache the auth assignments (I am using Memcached). But it doesnt seem to work...
Anyone have any idea? Either how to cache it or why it keeps getting called so many times?
Cheers

Add caching in
vendor/yiisoft/yii2/rbac/DbManager.php
(Also in all places you need caching)
this code:
$all_data = $this->db->cache(function ($db) use ($query) {
return $query->all($db);
},360);
public function getAssignments($userId)
{
if (empty($userId)) {
return [];
}
$query = (new Query)
->from($this->assignmentTable)
->where(['user_id' => (string) $userId]);
$all_data = $this->db->cache(function ($db) use ($query) {
return $query->all($db);
},360);
$assignments = [];
foreach ($all_data as $row) {
$assignments[$row['item_name']] = new Assignment([
'userId' => $row['user_id'],
'roleName' => $row['item_name'],
'createdAt' => $row['created_at'],
]);
}
return $assignments;
}

https://github.com/yiisoft/yii2/issues/3168
Only cache auth_item, auth_rule and auth_item_child data. All these
data are cached as a single entry in cache. Note that
auth_assignment is too big to be cached (imagine a system with
millions of users).

Related

Eloquent update all column fields

I want to update all the tables in the Article migration to a specific Boolean value which is set by the user.
I have written this code:
public function changeComVote() {
$data = request()->validate([
'status' => 'required'
]);
Article::query()->update(['isOnly' => $data['status']]);
event(new changesMade);
}
Although $data['status'] doesn't get passed inside the query and nothing happens, when i set it manually it works like a charm, what could be the problem?
Using $data['status'] from request will give you a string as a result, not a boolean.
Try this way
Article::query()->update(['isOnly' => $data['status'] == 'true']);
You can delete that "query()" thing and use this instead
Article::update(['isOnly' => $data['status']]);
or
Article::update(['isOnly' => ($data['status']] === "true"));
You can also specify where the row by using id
Article::find($id)->update(['isOnly' => ($data['status']] === "true"));

Laravel Bulk Update for multiple record ids

I want to mass update my records in Laravel but the records not getting updated. I have a different record for each Id. Below is what I am trying.
$ids = [5,6,8,9],
$updated_array = [
['name' => 'tarun'],
['name' => 'Akash'],
['name' => 'Soniya'],
['name' => 'Shalu'],
];
Model::whereIn('id', $ids)->update($updated_array);
Mass updates are used when you're trying to update multiple rows to the same values. You cannot mass update with different values.
Therefore, this would work, but would update all matching records to name of 'tarun':
Model::whereIn('id', $ids)->update(['name' => 'tarun']);
For your example, you could do:
foreach($ids as $key => $id) {
Model::where('id', $id)->update($updated_array[$key]);
}
But as far as I know, there's no way to do this without running 4 queries in Laravel and writing a raw SQL statement to accomplish this would even be messy.
You can use Laravel Upsert for mass update.
For example :
User::query()->upsert([
['id' => 1, 'email' => 'dadan#example.com'],
['id' => 2, 'email' => 'satria#example.com'],
], 'email');
This feature available in Laravel 8 or newer
Some good solutions for this issue are on the following post: https://github.com/laravel/ideas/issues/575
1) Can create custom function to do this, which uses raw SQL, as per barryvdh comment in the post.
public static function updateValues(array $values)
{
$table = MyModel::getModel()->getTable();
$cases = [];
$ids = [];
$params = [];
foreach ($values as $id => $value) {
$id = (int) $id;
$cases[] = "WHEN {$id} then ?";
$params[] = $value;
$ids[] = $id;
}
$ids = implode(',', $ids);
$cases = implode(' ', $cases);
$params[] = Carbon::now();
return \DB::update("UPDATE `{$table}` SET `value` = CASE `id` {$cases} END, `updated_at` = ? WHERE `id` in ({$ids})", $params);
}
This apparently increase performance by 13x
2) As per another comment, another idea is to do it on a per record basis, but do the whole thing as a single transaction, which makes it much faster.
DB::beginTransaction();
// your loop and updates;
if( !$user )
{
rollbackTransaction();
} else {
// Else commit the queries
commitTransaction();
}
3) There is a laravel library also that appears to try and solve this issue. https://github.com/mavinoo/laravelBatch
Note: I have not tried or tested any of the above solutions.
I think the only way to do this without n queries would be to
(optional) backup table
grab the records and create an array with all the updated data
bulk delete the records
bulk insert from array
That's 3 queries.
Iterating through n records is not practical for my application either; I was hoping there was an alternative but it looks like I'm going to have to implement this.
We can Insert and update batch (bulk) in laravel using this Package
Laravel Batch
Please try the below code:
$data_to_be_updated = [ ['id'=>5,'name' => 'tarun'], ['id'=>6, 'name' => 'Akash'],
['id'=>8, 'name' => 'Soniya'], ['id'=>9,'name' => 'Shalu']
];
foreach ($data_to_be_updated as $key => $value) {
$data = Model::where('id',$value['id'])->first();
if ($data) {
$data->name = $value['name'];
$data->save();
}
}
you can do it like this above mentioned:-
foreach($ids as $key => $id) {
Model::where('id', $id)->update($updated_array[$key]);
}

How to filter a result returned by a function get_entries() of a stream entries driver in pyrocms?

I have a stream/table named profiles. All of its column are stream-fields. I am trying to restrict the result returned by the the function, get_entries() depending on some criteria. Below is my code:
$data = [
'stream' => 'profiles',
'namespace' => 'users',
'where' => 'user_id = 3' // lets say, this is my criteria
];
$row = $this->streams->entries->get_entries($data); // returns empty
The varaible, $row resulted in empty array. Although there is one row in table, profiles where user_id is 3. I have read the documentation of pyrocms and it pretty much says the exact way to use the where clause (just like above).
NOTE: I have also tried writing like
'where' => 'profiles.user_id = 3'`
joy !to avoid table conflict. Still no
But when I write the code like this:
$row = $this->streams->entries->get_entries($query);
$query = [
'stream' => 'profiles',
'namespace' => 'users'
];
// No where clause this time
$row = $this->streams->entries->get_entries($query);
This time $row returns all rows including the row with user id 3.
I am unable to use the where clause in get_entries in a right way. I might have done some mistake. Help me out guyz
NOTE: I am using community edition.
I think this might be due to a bug (well, not a bug, but a feature that doesn't work as intended).
If I'm intentionally issue a wrong query, the sql query output is
SELECT [ ... ] LEFT JOIN `default_profiles` as `profiles` ON `profiles`.`user_id`=`default_profiles`.`created_by` WHERE (user_id` = 1) ORDER BY `default_profiles`.`created` DESC
Here you see that PyroCMS tries to lookup the data for the "created_by" field. And that doesn't work in this case.
If you disable the 'created_by' field, you should get the correct row:
$this->streams->entries->get_entries(
array(
'stream' => 'profiles',
'namespace' => 'users',
'where' => 'user_id = 3',
'disable' => 'created_by'
)
);
It would be great if you could file an issue on the pyrocms github page. If you won't I'll do it in the next few days.
Model
public function get_entries($table, $where) {
$this->db->select('*');
$this->db->from($table);
foreach ($where as $key => $value) {
$this->db->where($key, $value);
}
$this->query = $this->db->get();
foreach ($this->query->result_array() as $row) {
$array1[] = $row;
}
if ($this->query->num_rows() == 0)
return false;
else
return $array1;
}
call this model function as
$row = $this->streams->entries->get_entries('profiles',array('user_id '=>3));

Log the actual SQL query using ActiveRecord with Yii2?

I'm doing this:
$students = Student::find()->all();
return $this->render('process', array('students' => $students));
and then this in the view:
foreach($students as $student)
{
echo $student->name . ', ';
echo $student->getQuizActivitiesCount(); ?> <br /> <?php
}
i would like to see the sql query being performed. a student "has many" quiz activities, and the query performs perfectly, but i need to see the raw SQL. is this possible?
Method 1
With relations that return yii\db\ActiveQuery instance it's possible to extract the raw SQL query directly in code for example with var_dump().
For example if we have user relation:
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
You can then var_dump() the raw SQL like that:
var_dump($model->getUser()->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
exit();
Note that you should call it like that and not $model->user->... (the latter returns User instance).
But in your case it's not possible because count() immediately returns int. You can var_dump() partial query without count(), but I think it's not convenient.
Note that you can use this method for dumping generated SQL of any ActiveQuery instances (not only those that were returned by relation), for example:
$query = User::find()->where(['status' => User::STATUS_ACTIVE]);
var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
exit();
Method 2
This is much simpler in my opinion and I personally prefer this one when debugging SQL queries.
Yii 2 has built-in debug module. Just add this to your config:
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
],
Make sure you only have it locally and not on production. If needed, also change allowedIPs property.
This gives you functional panel at the bottom of the page. Find the DB word and click on either count or time. On this page you can view all executed queries and filter them.
I usually don't filter them in Grid and use standard browser search to quickly navigate through and find the necessary query (using the table name as keyword for example).
Method 3
Just make an error in query, for example in column name - cityy instead of city. This will result as database exception and then you can instantly see the generated query in error message.
If you want to log all relational queries of ActiveRecord in console application all proposed methods don't help. They show only main SQL on active record's table, \yii\debug\Module works only in browser.
Alternative method to get all executed SQL queries is to log them by adding specific FileTarget to configuration:
'log' => [
'targets' => [[
...
], [
'class' => 'yii\log\FileTarget',
'logFile' => '#runtime/logs/profile.log',
'logVars' => [],
'levels' => ['profile'],
'categories' => ['yii\db\Command::query'],
'prefix' => function($message) {
return '';
}
]]
]
UPDATE
In order to log insert/update/delete queries one should also add yii\db\Command::execute category:
'categories' => ['yii\db\Command::query', 'yii\db\Command::execute']
you can try this, assume you have a query given like:
$query = new Books::find()->where('author=2');
echo $query->createCommand()->sql;
or to get the SQL with all parameters included try:
$query->createCommand()->getRawSql()
In addition to arogachev answer, when you already work with an ActiveQuery object, here is the line I search to view the rawsql.
/* #var $studentQuery ActiveQuery */
$studentQuery = Student::Find();
// Construct the query as you want it
$studentQuery->where("status=3")->orderBy("grade ASC");
// Get the rawsql
var_dump($studentQuery->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
// Run the query
$studentQuery->all();
when you have a query object you can also use
$query->createCommand()->getRawSql()
to return the Raw SQL with the parameters included or
$query->createCommand()->sql
which will output the Sql with parameters separately.
In order to log/track every/all queries:
extend \yii\db\Connection and override createCommand method, like below:
namespace app\base;
class Connection extends \yii\db\Connection {
public function createCommand($sql = null, $params = array()) {
$createCommand = parent::createCommand($sql, $params);
$rawSql = $createCommand->getRawSql();
// ########### $rawSql -> LOG IT / OR DO ANYTHING YOU WANT WITH IT
return $createCommand;
}
}
Then, simply change your db connection in your db config like below:
'db' => [
'class' => 'app\base\Connection', // #### HERE
'dsn' => 'pgsql:host=localhost;dbname=dbname',
'username' => 'uname',
'password' => 'pwd',
'charset' => 'utf8',
],
Now, you can track/read/... all queries executed by db connection.
Try like,
$query = Yii::$app->db->createCommand()
->update('table_name', ['title' => 'MyTitle'],['id' => '1']);
var_dump($query->getRawSql()); die();
$query->execute();
Output:
string 'UPDATE `table_name`
SET `title`='MyTitle' WHERE `id`='1'
' (length=204)

cakephp save data array with foreach()

i am trying to SAVE data of Sizes which is coming from with multiple same name input.
Problem is i am not able to save data. Only last value "sizes" is saving in database.
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$data[] = array('EquipmentTypesSize' => array('sizes' => $val));
}
$this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
$this->EquipmentTypesSize->set($this->request->data);
$this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
I want to save this as
equipment_type_id | size1
equipment_type_id | size2
equipment_type_id | size3
There are several issues - the call to create that Kai mentioned is one of them.
First, here's your code, but I've added comments in places where I see obvious issues (I many not have caught all of them)
<?php
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$data[] = array('EquipmentTypesSize' => array('sizes' => $val));
// WHERE IS THIS $data VARIABLE USED? It seems it's never used?
}
$this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
$this->EquipmentTypesSize->set($this->request->data); // NO NEED TO CALL SET HERE - YOU ALREADY PASS DATA IN AS A PARAM TO SAVE
// YOU MUST CALL CREATE BEFORE ADDING A NEW RECORD
$this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
// I DON'T THINK $this->request->data['EquipmentTypesSize'] HOLDS THE VALUE YOU THINK IT DOES AT THIS POINT
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Now, it's hard to tell exactly what you're wanting to do, but here's my attempt to write what you intended. If it doesn't work, it should hopefully set you on the right track.
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$this->EquipmentTypesSize->create();
$this->EquipmentTypesSize->save(array(
'sizes' => $val,
'equipment_type_id' => $id,
));
}
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Lastly, two things:
Really, you should push as much of that logic into your EquipmentType model as possible. So you might create a saveWithSizes method in your EquipmentType model that holds most of that code.
You should look into Cake's saveAll method (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array). Ideally, you'd set your form data up so that you could just call saveAll and have Cake handle it all automatically. That may or may not be possible for your situation though.

Categories