Magento SQL query: Get all simple products that are "not visible individually" - sql

I am looking to write an SQL query directly on the Magento database that gets all Simple products that have the visibility attribute of "not visible individually", for which the value is 1 i believe.
Can anyone help with this? So far I have discovered that the visibility value is set in the table
catalog_product_entity_int
but have been unable to progress further. Thanks

There are many reasons to not do this without the ORM, all of which may (or may not) apply to your needs (store filters, reading data from the correct table, etc). At the very least, you can use the product collection object to build the query which you would run:
$coll = Mage::getModel('catalog/product')->getCollection();
$coll->addAttributeToFilter('visibility' , Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
echo $coll->getSelect();
The resulting query will look like this:
SELECT `e`.*, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default`
ON (`at_visibility_default`.`entity_id` = `e`.`entity_id`)
AND (`at_visibility_default`.`attribute_id` = '526')
AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`)
AND (`at_visibility`.`attribute_id` = '526')
AND (`at_visibility`.`store_id` = 1)
WHERE (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '1')

I hope this might work not tested.
$sql = "SELECT * FROM catalog_product_entity_int WHERE visibility=1";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arraytest) {
echo $arraytest['name'];
}

Related

CodeIgniter query from sql

How can I transform this sql to codeigniter query? I have screenshot of an EER diagram to help you understand better. So there are three tables to join. I guess, one is "pjesma", then "izvodjac", and "izvodi_pjesmu". This sql works when I run it and gives me results I want. I am also using pagination so I need limit and offset somehow included.
SELECT pjesma_id, naslov, naziv
FROM pjesma p, izvodjac i, izvodi_pjesmu ip
WHERE p.pjesma_id = ip.pjesma_pjesma_id
AND i.izvodjac_id = ip.izvodjac_izvodjac_id
in model:
public function paginacija_pjesme($limit, $offset) {
$this->db->select('pjesma_id', 'naslov', 'naziv');
$this->db->from('pjesma p');
$this->db->join('izvodi_pjesmu ip', 'p.pjesma_id=ip.pjesma_pjesma_id');
$this->db->join('izvodjac i', 'i.izvodjac_id=ip.izvodjac_izvodjac_id');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
EDIT:
So in select I used wrong syntax, this line:
$this->db->select('pjesma_id', 'naslov', 'naziv');
should be like this:
$this->db->select('pjesma_id, naslov, naziv');
A few things, as you are dealing with objects within the query builder class, you can method-chain and make things a little less cluttered.
You are maybe getting an ambiguous column error, joining and not prefixing your select. Hard to tell without the error being posted you are facing.
Always best to (just in case) set defaults to your method arguments as well, in case you pass a blank variable (or don't need to for whatever reason).
When you are using multiple tables in the FROM statement you are actually doing a CROSS JOIN, and your new code is giving INNER JOIN,trying the join syntax direct to your SQL server might help as you are not actually doing the same thing between your first written query, and the codeigniter one..
Try the below with LEFT join just to see... If there are NULL joins using INNER they will be omitted
public function paginacija_pjesme($limit = 10, $offset = 0) {
$query = $this->db->select('p.pjesma_id, p.naslov, i.naziv')
->from('pjesma p')
->join('izvodi_pjesmu ip', 'p.pjesma_id = ip.pjesma_pjesma_id', 'left')
->join('izvodjac i', 'i.izvodjac_id = ip.izvodjac_izvodjac_id', 'left')
->limit($limit, $offset)
->get();
return $query->result();
}
Are you getting any errors? what do you see if you print_r($query->result()) ?
try also spitting out $this->db->last_query() - this will output the SQL codeigniter has build. Then running this directly to your database, and see the results from there. You might even see the issue without needing to run it.
This is an example of what your current codeigniter code is generating (as you can see, different to your original query):
SELECT pjesma_id, naslov, naziv
FROM pjesma p
INNER JOIN izvodi_pjesmu ip ON p.pjesma_id = ip.pjesma_pjesma_id
INNER JOIN izvodjac i ON i.izvodjac_id = ip.izvodjac_izvodjac_id
# LIMIT 10,0

How to perform multiple AND/OR meta_query using using $wpdb

SCENARIO: I have a custom post type with start and end dates saved as post meta. This is to allow the admin to schedule dates to show AND hide a post. There is also the option to leave these dates blank, in which case the post just shows by default. As meta_query doesn't appear to allow you to perform an AND/OR comparison, I've dabbled with creating a custom SQL query using $wpdb. SQL is new territory for me. Currently, this works for the start date:
$today = date('Y-m-d');
$results = $wpdb->get_results($wpdb->prepare("
SELECT
$wpdb->posts.post_title,
$wpdb->posts.ID,
$wpdb->posts.post_content
FROM
$wpdb->posts,
$wpdb->postmeta
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_type = 'announcements'
AND ($wpdb->postmeta.meta_key = 'sap_start_date'
AND ($wpdb->postmeta.meta_value <= '$today' OR $wpdb->postmeta.meta_value = ''))
"));
However, when I try and amend the above to query the end date, I get nothing back. I tried changing the WHERE section to:
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_type = 'announcements'
AND ($wpdb->postmeta.meta_key = 'sap_start_date'
AND ($wpdb->postmeta.meta_value <= '$today' OR $wpdb->postmeta.meta_value = ''))
AND ($wpdb->postmeta.meta_key = 'sap_end_date'
AND ($wpdb->postmeta.meta_value >= '$today' OR $wpdb->postmeta.meta_value = ''))
This returns an empty array.
QUESTION: How would I write this query to check both the sap_start_date and sap_end_date post meta values?
I figured out my problem was that I was using a logical AND against the same meta key object twice; how can postmeta.meta_key be equal to start_date and end_date? Using an answer from this SO question I managed to re-write the query to use aliases so that I could get the postmeta twice and query the two different keys:
SELECT
$wpdb->posts.post_title,
$wpdb->posts.ID,
$wpdb->posts.post_content
FROM
$wpdb->posts,
$wpdb->postmeta AS postmeta1,
$wpdb->postmeta AS postmeta2
WHERE
$wpdb->posts.ID = postmeta1.post_id
AND $wpdb->posts.ID = postmeta2.post_id
AND $wpdb->posts.post_type = 'announcements'
AND (postmeta1.meta_key = 'sap_start_date'
AND (postmeta1.meta_value <= '$today' OR postmeta1.meta_value = ''))
AND (postmeta2.meta_key = 'sap_end_date'
AND (postmeta2.meta_value >= '$today' OR postmeta2.meta_value = ''))
This works, although I'm still a little fuzzy on just how it works. If anyone could shed some further light on this purely for educational purposes it would be much appreciated.

PDO Riddle (No while loop)

I just recently learned how to work with PDO database queries, but my latest query isn't working for some reason. All my other queries include arrays and while loops, but I don't think this one requires either, so I may just need to change the query structure somehow.
$stm = $pdo->prepare("SELECT H.N, H.URL, H.Title, H.Subtitle,
H.Site, H.MetaTitle, H.MetaDesc, H.KW, H.Live, A.Article, A.Pagedex
FROM 1_home_pages H
LEFT JOIN 1_home_articles A ON A.Site = H.Site
WHERE H.URL = :MySection AND H.Site = :MySiteId AND H.Live = 1 AND A.Site = :MySiteId AND A.Section = :MySection");
// $stm->execute();
$stm->execute(array(
'MySiteId'=>$MySiteID,
'MySection'=>$MySection
));
$data = $stm->fetchAll();
I added the line $data = $stm->fetchAll();, and it now works - as an array. The foreach statement below displays EVERYTHING in one jumbled mess...
foreach($data as $row) {
print_r($row);
}
To escape the array and just display one item at a time, I tried the following:
$Content = $data['Article'];
echo $Content;
But it doesn't display anything. Can anyone tell me the solution?
I don't know if you made a typo while posting this question, or this is a typo from your source code: you have :MySection and yet while executing you write MySection (without colon). What's more, this line:
WHERE H.URL = :MySection
AND H.Site = :MySiteId
AND H.Live = 1
AND A.Site = :MySiteId
AND A.Section = :MySection");
You used multiple parameters with same name in one statement. Don't know if this is a correct way to do it, since I've never done it. I suggest you try using different names for parameters and see if that helps. Also, what do you mean 'but my latest query isn't working for some reason.' ? What does it return? Does it return anything at all? fetchAll returns multiple rows, that's why you get array.
Instead of $data = $stm->fetchAll(); use $data = $stm->fetch();. It will return next row from your query.

How to execute query with subqueries on a table and get a Rowset object as a result in Zend?

I'm currently struggling on how to execute my query on a Table object in Zend and get a Rowset in return. Reason I need particularly THIS is because I'm modifying a code for existing project and I don't have much flexibility.
Query:
SELECT *
FROM `tblname` ud
WHERE ud.user_id = some_id
AND
(
(ud.reputation_level > 1)
OR
(
(SELECT COUNT( * )
FROM `tblname` t
WHERE t.user_id = ud.user_id
AND t.category_id <=> ud.category_id
AND t.city_id <=> ud.city_id
) = 1
)
)
Is there a way to describe this query using Select object?
Previous SQL solution was very simple and consisted of one WHERE clause:
$where = $this->getAdapter()->quoteInto("user_id = ?",$user_id);
return $this->fetchAll($where);
I need to produce same type of the result (so that it could be processed by existing code) but for more complicated query.
Things I've tried
$db = Zend_Db_Table::getDefaultAdapter();
return $db->query($sql)->fetchAll();
---------------- OR ----------------------
return $this->fetchAll($select);
---------------- OR ----------------------
return $this->_db->query($sql)->fetchAll();
But they either produce arrays instead of objects or fail with Cardinality violation message.
I would appreciate any help on how to handle SQL text queries in Zend.
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
//change the fetch mode becouse you don't like the array
$dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ);
$sql = "you're long sql here";
$result = $dbAdapter->fetchAll($sql);
Zend_Debug::dump($result);
exit;
For a list of all fetch modes go to Zend_Db_Adapter
To write you're query using Zend_Db_Select instead of manual string , look at Zend_Db_Slect

Return a List of typed object via CreateSQLQuery in NHibernate

Been trying to get the following query working for a few hours now and am running out of ideas. Can anyone spot where I'm going wrong. Any pointers much appreciated.
CalEvents = (List<CalEvent>)session.CreateSQLQuery(#"
SELECT *
FROM dbo.tb_calendar_calEvents
INNER JOIN dbo.tb_calEvents
ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id)
WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
)
.AddEntity(typeof(CalEvent))
.SetInt64("theCalID", cal.id);
Error:
Kanpeki.NUnit.CalUserTest.Should_return_logged_in_user:
System.ArgumentException : Parameter theCalID does not exist as a
named parameter in [SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN
dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID =
dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID =
'theCalID']
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
should be
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = :theCalID"
= 'theCalID' should be written as = :theCalId; :theCalId is how you use named parameters even in Native SQL Queries.
You should remove the query.ExecuteUpdate() call.
Doing the query.List() is enough to issue the query on the session and return the result set.