How to find duplicate entries in database using Propel ORM? - php

I want to look for duplicate entries in my table and show all of them. How can I find all duplicated values in one column using Propel ORM?

Well, this question suggests using counts... you could replicate that query in Propel (I think) with this:
$results = TableNameQuery::create()
->select(array("id", "field", "COUNT(*)"))
->groupBy("field")
->having("COUNT(*) > ?", 1)
->find();
Of course, this gets a little hairy, so you might just want to use straight SQL if Propel fails you.
(For reference, here's the SQL:)
SELECT field, COUNT(*)
FROM table_name
GROUP BY field
HAVING count(*) > 1

Related

PHP PDO statement with Sum and other column fails

I'm trying to run an SQL query with PDO
This works
$result = $dbo->query("SELECT sum(c) as scfus
FROM tbl
WHERE
YEAR(ondate)=YEAR('".$_POST['startdate']."')
AND MONTH(ondate)=MONTH('".$_POST['startdate']."')
AND DAY(ondate)=$i");
but this does not
$result = $dbo->query("SELECT a,b,sum(c) as scfus
FROM tbl
WHERE
YEAR(ondate)=YEAR('".$_POST['startdate']."')
AND MONTH(ondate)=MONTH('".$_POST['startdate']."')
AND DAY(ondate)=$i");
The only difference is the addition of the a,b column names to the query.
I can run this query (both of them) directly into mysql and get a single record back, as expected, but PDO does not seem to like column names AND sum in the same query?
I can't see a reason, or solution. New to PDO, so was never an issue for me before.
Thanks.
UPDATE - OK, I still think this should work fine, but as a workaround, I've run 2 sql statements, almost exactly the same. One with SELECT SUM(x), one with SELECT a,b, but without the sum. Works fine, but I really should be able to do it in one statement, unless I'm into some PDO limitation I'm not aware of yet.
you might need to GROUP BY c (or whatever column you may want to group domain aggregate function SUM() by) in order to obtain the desired result-set... and maybe take the suggestion above into account.
Try it this way for your query, worked for me for what you want to achieve :
$result = $dbo->query("SELECT a,b,(SELECT sum(c) as whateverFROM tbl),
as scfus
FROM tbl
WHERE
YEAR(ondate)=YEAR('".$_POST['startdate']."')
AND MONTH(ondate)=MONTH('".$_POST['startdate']."')
AND DAY(ondate)=$i");
SUM returns only one line so you'll never get all your results and only the ones of the first matching WHERE criterias (it registers first record found, then only count what it needs).
It will work
Oh, btw, make a timestamp of the date, make a string of the date post in the date sql format and check
WHERE date = $_POST['date']
will work
SELECT a, b , (SELECT SUM(ID)*30 FROM table) FROM table WHERE ID = SOME(SELECT ID FROM tables WHERE users_ID = idYouWant);

Id Column Overwritten by Join

Using Laravel 4.2 & MySQL.
I have an applications table with an id and a fit_score_id column, and a fit_scores table with an id column. It's a basic "belongs to" relationship.
The following code:
$query = Application::join('fit_scores', 'applications.fit_score_id', '=', 'fit_scores.id');
$collection = $query->get();
...produces a collection of Application models with the id property set to the value of the fit_score_id. What am I doing to cause this?
I should note that it is necessary to do this join rather than simply using eloquent relations, because I'm going to want to order the results by a column on the fit_scores table. I don't believe this is possible using Eloquent without an explicit join.
The best way to solve this is by chaining the join method to a select method as following:
Application::select('*', \DB::raw("applications.id as appid"))
->join('fit_scores', 'applications.fit_score_id', '=', 'fit_scores.id')
->get();
Explained: The solution simply suggest that instead of thinking to prevent the behavior of overwriting the first id with the joined id, we can hook into the primary selection query (before joining) and change the label of the id column into something else (in this case 'appid'). By doing so, we end up with both the id of the parent table being labeled 'appid' and the id of the joined table being labeled 'id' again while they lives together on the final result.
I was able to find a possible solution using this answer:
Laravel 4 - JOIN - Same column name
Basically, since Laravel does not automatically prefix column names with table_name. for joined tables, we need to manually work around it by aliasing any conflicting column names in joins. Adding this select statement to my query did it:
->select(DB::raw("applications.*, fit_scores.*, applications.id as id"))
It depends on what you need but probably you can achieve it using eager loading. In case you need to mix joins and eager loading check this out. http://www.jmilan.net/posts/eager-loading-joins-in-laravel

Missing rows when querying table with Doctrine (Symfony2)

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.

How I get comma-separated values form table in symfony2?

I have a MySQL table which contains comma-separated values like this:
first row=(A,AB)
second row=(AC, AE)
I want to select the rows which have A in their set not AC OR AE. I am using symfony2 in my code.
$query = $query->andWhere("FIND_IN_SET('".trim($tag)."',advNews.advNewsTags)");
Its Return Error: Expected known function, got 'FIND_IN_SET'
But if i use query in sqlyog like
select * from advice_news where adv_news_type = 'news' AND FIND_IN_SET ('sdf',adv_news_tags)
its work fine.
Tell me any solution i will use FIND_IN_SET in symfony2.
You need to add FunctionNode to Doctrine and Symfony. I had the same problem with SOUNDS LIKE. You can find my solution at http://piotrpasich.com/full-text-searching/ in SOUNDEX chapter.

Doctrine 2.1: Getting and assigning COUNT(t.id) from a subquery?

I have two entities in Doctrine 2.1: Category and Site each category has many sites and each site has a parent category.
I would like to make a single update query (in DQL) which will update a field called count of the Category entity with the number of related sites.
So in SQL I would do something like this:
UPDATE categories c SET c.count = (SELECT COUNT(s.id) FROM sites s WHERE s.category_id = c.id);
This would work beautifuly, in DQL it might something like this:
UPDATE PackageNameBundle:Category c SET c.count = (SELECT COUNT(s.id) FROM PackageNameBundle:Site s WHERE s.category = c)
Such attempt raises [Syntax Error] line 0, col 61: Error: Expected Literal, got 'SELECT'.
Subqueries DO work in DQL, but the problem here (as far as I see it) is that Doctrine cannot assign the returned value from the subquery, to the c.count. This is understandable since I might fetch more than 1 field in the subquery and even more than one row. It magicaly works in MySQL since it sees one row, one field and for convenience returns a single integer value. Doctrine on the other hand has to be object oriented and has to work with different engines where such convertions might not be supported.
Finally, my question is:
What is the best way to do this in Doctrine, should I go with Native SQL or it can be done with DQL and how?
Thanks in advance!
EDIT: I just found this quote in the DQL Docs:
References to related entities are only possible in the WHERE clause and using sub-selects.
So, I guess assigning anything but a scalar value is impossible?
The main question remains though..
You can use native sql queries in Doctrine also, for that kind of specific queries. DQL is powerful in its own way, but it's also limited due to performance constraints. Using native sql queries and mapping the results will achieve the same thing, and there is no disadvantage in doing that.
The documentation explains it in detail.

Categories