I am working on one module in yii2 and please count me as a noob in this. I usually work in Laravel.
My query looks as below.
$query = SomeModel::find()
->joinWith(['relation1 s', 'relation2 r','relation3','relation4 c'])
->where(some where contions)
->where(some where contions);
The issue is in the view the yii2 debugger (Database Queries) shows "187 duplicated queries found."
My model doesn't have any loop around the query.
I have tried with removing joinWith relations but it will increase the duplicate queries to 220.
The query which gets repeated is one SELECT query
SELECT * FROM `relation4` WHERE `id`= 2159
I have checked the "relation4" model but it has nothing suspicious in it.
What should I do or where should I look to resolve this issue??
Related
I'm trying to use distinct with pagination but my pagination seems to ignore the total records of my distinct and it makes my pagination all messed up.
I have the following query:
$log_data_list = DB::table('logs')->leftjoin('users','logs.user_id', 'users.id')
->select(DB::raw("distinct on (logs.action,logs.action_table)logs.user_id,users.username as username,logs.created_at as tanggal,logs.ip_client as ip_client,logs.action as tindakan,logs.action_table as tabel,logs.no_laka as no_laka,logs.change_id as id"))
->paginate(5);
Can anyone help me with a solution? Thanks in advance
Instead of using a distinct in this fashion, why not have the log actions stored uniquely in a relational table using methods firstOrCreate / firstOrNew on storing the actual log.
You then would be able to use a proper eloquent model to paginate log actions table that would already be a unique list and use the ID of selection action to get all related logs.
table.logs id,action_id,users_id,...
table.logs_actions id,action_name
This would be less costly than a select distinct especially on a huge table.
I am completely new to Laravel Framework. And I am using 4.2 version.
I am trying to run below sql statements through function calling from routes.php.
The page shows no error.
But there is no 'truncate' or 'insert' happend in Database.
Here is the code in Controller funtion:
DB::statement('TRUNCATE TABLE calc2');
DB::statement(
"Insert into calc2
Select groups, members, date(timestamp) as Date, count(id) as Total, sum(order) as Order
from Order
where order > 0 and date(timestamp) >= '$lastday'
group by groups, members, date(timestamp)
");
I checked DB connections. I also run these query directly in MySQL. It is working fine.
I tried DB::select instead of DB::statement and I tried with Eloquent/raw too.
But still Database having 0 records only.
Thanks in advance if anyone can suggest me where I am doing mistake.
Are you commiting your queries?
If not, just use DB::commit();
Doc: http://laravel.com/docs/4.2/database#database-transactions
I'm encountering a strange issue with Doctrine.
I need to query a simple table with only 1 inner join, which I something I have already done many times. But in this case something's odd: there are a lot of rows missing.
I have an entity called Policy. It is linked to a table on my Oracle database. There are 81k+ rows in this table. When querying this entity with the Doctrine query builder, I only get 5k results. I made this query as simple as possible for testing :
$qb = $em->createQueryBuilder();
$qb->select('p')->from('ErwMonitoringExtranetBundle:Policy', 'p');
$query = $qb->getQuery();
$policiesFull = $query->getResult();
The $policiesFull variable only contains 5k elements. There are no duplicates in the table.
The SQL query that is generated by Doctrine looks like this :
SELECT
r0_.node_group_name AS NODE_GROUP_NAME0,
r0_.policy_name AS POLICY_NAME1,
r0_.policy_description AS POLICY_DESCRIPTION2,
r0_.policy_group_name AS POLICY_GROUP_NAME3,
r0_.policy_type_name AS POLICY_TYPE_NAME4,
r0_.policy_name_on_agent AS POLICY_NAME_ON_AGENT5,
r0_.date_last_maj AS DATE_LAST_MAJ6,
r0_.om_name AS OM_NAME7,
r0_.id_node AS ID_NODE8
FROM
ewintranet.ref_monitored_ci;
Running the same exact query on Oracle returns the full table content.
Counting results through a doctrine query returns the correct number of rows :
$qb = $em->createQueryBuilder();
$qb->select('count(p)')->from('ErwMonitoringExtranetBundle:Policy', 'p');
$query = $qb->getQuery();
echo $query->getSingleScalarResult();
This returns 81k.
Does anybody know why all these rows disappear after using getResult() ?
This is what I would do:
Check the result using createQuery or createNativeQuery
Run the query from simple php script outside of symfony
If with all 3 methods you get the same issue then it might be problem with the size of data limitation.
I would start with max_execution_time and memory_limit settings in php.ini
I found also some oracle limitations that might be set at:
oci8.statement_cache_size in php.ini or in file oraaccess.xml in Oracle directory. It is also significant if you use APC for query caching.
Anyway what does app_dev.php say?
Okay, I found out what was causing my issue. The primary ID was wrong in my Entity declaration.
The oracle table had a composed primary key while in my entity the ID was only on one column. GetResult was making a DISTINCT on this column.
I am having an odd problem in CakePHP where
$this->something->find('count');
works perfectly, yet
$this->something->find('all');
returns nothing (not even an empty array, any errors, or anything).
edit: turns out I am getting an sql error: "SQL Error: 1054: Unknown column" - for a column that does indeed exist. (users.display_name in the sql query below):
SELECT item.id, item.name, item.description, item.user_id, users.display_name FROM item LEFT JOIN users ON (item.user_id = users.id);
I also tried using findAllBy as well as paginate (paginate is actually what I am trying to do - although from what I've gathered, paginate and find('all') are pretty similar in functionality).
The odd thing is that find('all') works everywhere else - it's just in this specific controller that it is acting odd. I am not getting any errors, simply an empty result.
I'm thinking that I may be overlooking something quite simple, but any help is appreciated. Thanks!
So, as per our discussion, the problem you're having is with the virtual fields. Have a look at the documentation, more specifically at the virtual fields and model aliases and limitation of virtualFields sections.
From your description above, it looks like you have a join specified your virtual field which would be causing the error you're seeing because it'll add the JOIN before the FROM. If you insist on using the virtual field, I'd suggest you rewrite it to use a subquery. Make sure your subquery only returns 1 column.
Example: (http://web-development-blog.co.uk/2011/03/08/cakephp-virtual-field-count-another-modeltable/)
public $virtualFields = array(
'count' => 'SELECT COUNT(*) FROM stacks Stack'
);
Alternatively, you can use the Model::beforeFind to bind the necessary models (if necessary) and change the query parameters.
If you can't figure it out, please post your model and I'll help you.
The specific problem you're having with the difference in behaviour is that find('count') will run a basic COUNT(*) query on your database to determine the number of rows.
find('all'), however, runs a different query, and if the SQL you've provided is what it's trying to use, it's invalid:
SELECT item.id, item.name, item.description, item.user_id, users.display_name LEFT JOIN users ON (item.user_id = users.id);
There's no FROM declaration (SELECT from what, exactly?), and if you've not customised your Item or User or App models, (setting $useTable = false maybe?) you're dealing with an unusual error.
One thing to look out for, if these models work fine in other controllers, is any alteration to the properties of each model in each controller. They won't act differently on a per-controller basis unless you have code in each controller that tells it to.
I am trying to implement pagination in my search results with Yii. I have pagination working on my browse record pages, but for some reason, am having trouble with getting it working in search.
So far, I have the following SQL to produce the search results:
SELECT SQL_CALC_FOUND_ROWS DISTINCT user.id FROM user, personal_info WHERE (personal_info.bio LIKE '%a%' ) AND personal_info.user_id = user.id AND user.role = 'F' LIMIT 0, 8;
Which is passed to ActiveRecord as follows:
$results = $this->findAllBySql($sql);
Immediately afterwards I run this code:
$rows = $this->findBySql("SELECT FOUND_ROWS() as row_count;");
echo $rows->row_count;
Strangely, I am receiving the following error when I try and execute the above code:
Property "Search.row_count" is not defined.
For some reason, Yii is not able to retrieve the FOUND_ROWS value from MySQL.
I have pretty much exactly the same PHP code in my browse records page (but different SQL), and it works perfectly. Not sure why in this situation Yii is unable to retrieve the FOUND_ROWS value. I've tried running this code directly inside MySQL to see if there was something wrong with my SQL, but it retrieves the FOUND_ROWS value with no problems - this problem only happens when I try to do it inside Yii.
Any idea what I maybe doing wrong?
Many thanks!
Turns out that I needed to define $row_count inside the model before it would recognise it as a variable I can refer to.