I want to know how to use arregate sql functions for when making a Magento module. I currently have this code inside my mode;
$rake = Mage::getModel('showdown/votes')->getCollection();
$rake->addFieldToFilter('votedfor', $productid);
//$collection->groupByFilter('matchid'); //<-- what do i put here?
$sticks = count($rake);
I'm not sure exactly what you want to achieve as the title of you question conflicts with the code example provided.
The title asks how to count a collection, and your code example asks how to perform a group by.
So,
Collection Count
count($collection), $collection->count() or simply $collection->getSize() which will avoid the collection load (ideal if you simply require the count and nothing else)
Group By
EAV Based Collection:
$collection->groupByAttribute('matchid');
Non EAV Based Collection:
$collection->getSelect()->group('matchid');
Related
I currently have the following code to find all the unique IDs of comments placed by a user. This works "fine". However, it's really slow for users with a lot of comments and I'm trying to figure out if there is a more elegant way of handling this as it doesn't seem to be the best solution.
def find_unique_user_grades
#comments = []
#environment.users.includes(:comments).map(&:comments).select do |comments|
comments.select { |comment| #comments.push(comment.id) }
end
#comments.uniq!
end
I'm hoping someone can help me with this.
You should always prefer to do this in the database, not in Ruby. The code you've posted will load all users from the database, and then convert the raw row data to ActiveRecord objects, which is (comparatively) extremely expensive. You don't need any of that data to join through to comments. Then, you'll do the same for every user's comments (query and create ActiveRecord objects) and again, you don't need any of that to get at the comment's id column.
What you're after (assuming I've guessed correctly at the shape your schema) is a simply join followed by a pluck. This will run a single query and return a single array of numbers, without any of the cost of loading users, creating objects, loading comments, creating objects, or iterating in Ruby.
Finally, it will also perform the distinct query in the database, where it can take advantage of any relevant indexes, rather than uniqing in Ruby.
The correct query is something near to:
#environment.users.joins(:comments).distinct.pluck('comments.id')
I'm able to fetch information for whatever table I need the problem here is the update, insert, and delete records, is not working..
I have read the Joomla doc's but even doing the simplest update queries are not working... so here is my code:
UPDATE:
// I'm getting the data from an array
if (!empty($_POST['data'])) {
$getData = json_decode($_POST['data'], true);
}
// after this line I have a foreach for the array
// in the foreach I have a few IF's
// ether if the result from IF is True or False
// in both I have similar queries
// So let say IF return true;
// Lets prepare the Data using OBJECT's
$object = new stdClass();
$object->product_id = $v['product_id'];
$object->product_name = $v['product_name'];
$object->product_price = $v['product_price'];
$object->product_number = $v['product_number'];
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
// that should Update my tables but it doesn't ... now my table has about 21 columns
// but I only need to update 4 columns base on the column product_number
You might have notice the $v['product_id'] where $v is from the foreach, the foreach and the first query are working fine, I did a few echo's before moving to the Update part just to make sure I'm getting the correct data in a format that it should be... any way... the Update part is not working... so I thought it may be because of my table I when to use one table from the regular instalation of joomla... but it still the same no result on the update...
Does any one know how to "update" a table using Joomla CMS as framework?...
Remember I want to use the updateObject() method...
Why don't I use Joomla Framework instead of the CMS's library?
Well, I can give you a few examples, let say:
a business man want a simple basic report, he doesn't care about managing the site he has people to do that, he also gets a sales report, but he doesn't trust the crew just yet and he want to able to see and compare the result, and he needs and standalone without all the fancy tools that joomla has and for that he need an standalone app that can give that kind of report... ok I might have gone a bit far, but the idea is to have basic, simple, easy to read reports and updates, and that is why I have started this project.
Again, I already went to joomla and read the docs but following their examples it just not working... now, am I have to declear all of the columns even if they don't need to be update? or am I missing an execution for the query which in joomla doesn't mention any executions when using $object() only when using regular queries SQL... the idea is to use $object() ...
Thank you for taking the time.
You have defined the updateobject as a variable, and are not calling it. try changing this:
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
to this:
JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
Also, on a side note, you should not use $_POST and instead should use JInput
I am trying to figure out how I might be able to do this - possibly in the mysql database, but I'm not sure of all the tables involved, so daren't go much further?
I have added an attribute called "condition" to the defaul attribute set.
I need to populate that attribute value for every product, but the admin system requires me to fill in many other attributes if I try to bulk update them using the admin form. This can't happen as "description" is different for every product, obviously.
So, can someone tell me how I might populate this attribute value with the value "new" for every product in my database?
Realistically, I can change this attribute value for the number of products that need a different value "used", but if the update could be filtered on SKU, I can make it work right first time. I think!
The programming approach is to save the following script in your Magento folder and execute it by visiting the address directly. It might be slow but that doesn't matter if it's only going to be run once and then deleted afterwards.
<?php
require 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getResourceModel('catalog/product_collection');
foreach ($products as $product) {
// replace IS_NEW with a test perhaps using $product->getSku()
$product->setCondition(IS_NEW ? 'new' : 'used')
->save();
}
Not a programming answer so perhaps not suitable for Stack Overflow but there are a number of workarounds.
You could export all products to Excel, bulk update many rows at once then re-inport. Use Magmi if you have many products.
There are some extensions which let you update many attributes at once. This Mass Actions is expensive but gives you an idea what might be available if you search.
As much as I loathe desktop applications for web services there is Store Manager which specialises in bulk actions.
I need some guidance. Forgive me for a lengthy post but I need to explain myself. In my world, there is no one who understands what I am talking about so I have to go online for assistance.
I am an architect doing my own website because work has dried up and I plan to use an improved website for a marketing campaign.
I have done what I'd call a "lash-up" of this site which functions OK. But it's nowhere near ready to publish. I am trying to get it reorganised to do this and am moving the whole thing over to Codeigniter. My puzzle relates to views in Codeigniter.
One of the main pages for potential clients is the projects page showing work done. It uses nested tabbing. As I have said, I've made it work OK in ordinary procedural PHP.
Note that the projects are organised by category i.e housing, commercial etc In each category there are projects.
Actually the tabs are dynamically produced with some assistance from jQuery. I mean by this that my homespun php creates markup based on what's returned from the database.
The tab markup is the usual one of an unordered list whose li elements contain anchors whose hrefs reference divs arranged below. To achieve nesting, these divs then contain another ul with a further set of divs related to it.
The top tabs correspond to a category e.g housing, commercial. The lower tabs correspond to projects within a category.
I've made this work with four queries before. I think at least one may be redundant but I said it was a "lash-up".
Query 1: "select distinct pcat, pcatid from pcategory inner join projects on pcatid = projcat order by pcat desc"
From this query I get hold of the id used in the href.
Query 2 : same as above but this time the id is used for div id.
The next query is the source of my puzzlement because I don't see how to replicate it with CI.
Query3 :
$jobcat=$row2['pcatid'];
$queryall3 = "select projid, projtit, projcost, projdate from projects where projcat= '$jobcat'";
This query uses the category id - $jobcat - returned by each iteration of the while clause used to expand the results from query 2. In other words, it runs inside the query 2 while loop so it can get the category id and then get all the projects related to it.
The results of query 3 are used to form the lower tabs, and their href value is the id of the project.
Query4: same as query 3 and used to populate the lower divs with data from the database relating to a specific project.
So, finally my question: it seems to me that query 3 is difficult to manage using the Codeigniter set up. I can imagine an array of results looped over in a view. What I can't conceive is how to make a model call within that loop.
Apologies for a long-winded question and any maladroit coding assumptions exhibited. Assistance would be a blessing.
Tom
I don't really see what you're asking, but it seems that you want to know how to perform queries in CI?
In which case I suggest you take a good read of the docs
$this->db->select('pcat, pcatid');
$this->db->distinct();
$this->db->from('pcategory');
$this->db->join('projects', 'pcategory.pcatid = projects.pid', 'inner');
$this->db->order_by('pcat', 'DESC');
$result = $this->db->get();
I very much doubt this will work as I do not know your table structure but may give you an idea of how to use the active record class in CI.
You can of course just use the query method:
$results = $this->db->query('YOUR QUERY HERE');
the active record class can do a lot of the work for you, however.
As for about being difficult to do in CI, this is simply untrue - you just need a clearer picture and understanding of what you want to achieve.
Edit
$jobcat=$row2['pcatid'];
$queryall3 = "select projid, projtit, projcost, projdate from projects where projcat= '$jobcat'";
$results=$this->db->query($queryall3);
$data = $results->result_array(); // get the results of the 3rd query as an array
// new query
$this->db->select('query_4_select'); // select whatever you need
$this->db->from('whatever_table');
// this probably isn't the most efficient way, but for examples sake:
foreach($data as $row) // using the result_array from above
{
$this->db->or_where('query_4_id', $row['id']); // the magic !!
}
$new_results = $this->db->get();
So essentially, you get the ID's from query 3, run it through a foreach and build a where x=x or x=y or x=b type query, which will then (hopefully) return the desired results.
This is one way, you will need to tweak it.
But it sounds like you can just use a join? Perhaps if you could post your entire tables structure.
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('modellijn')
->addAttributeToFilter('brand', 114)
->getFirstItem();
Hi there. I would like to know the right syntax for the following:
Select the attribute modellijn where the attribute brand=114. The above syntax gives back an error. I have been searching for 2 days now for the right syntax but no result so far unfortunately.
I am hoping someone here is willing to help me!
So the first problem I see here is... that you are attempting to select the attribute for a particular brand. Attributes do not in fact have brands (nor modellijns).
Can you please clarify the use case? Are you trying to get the modellijn (that's fun to type) value of all products with brand 114? What is your expected output?
Or, if you're more comfortable as such, what would be the SQL query you'd expect to see generated?
Thanks,
Joseph Mastey
Okay, based on your update, I just wanted to clarify a few things.
Attribute sets have attributes
Attributes have options (sometimes)
Products have attribute values
Categories have products
If you just need to find the modellijin of a particular product, then you just need to ask for it. If you have a single product, this should do the trick:
$product = Mage::getModel("catalog/product")->load($id); // Magento does this for you in some cases
$product->getModellijn(); // this will return your value
$product->getAttributeText('modellijn'); // IIRC, this works for <select> type attributes
If you're loading products from a collection, you need to make sure to tell Magento that you want to load that attribute too. Selecting everything from EAV is even more expensive than selecting everything from a standard, normalized database. Therefore, Magento expects you to tell it what you need.
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("brand", 114); // limit for brand
$collection->addAttributeToSelect("modellijn"); // * also works but is slow
foreach($collection as $product) {
$product->getModellijn(); // just as above
$product->getAttributeText('modellijn');
}
Let me know if that does it for you. If it doesn't, please amend your post above to include a more complete SQL statement, and if possible more information about where you are using this data. That will help me understand the context within which you're executing the code.