I created a custom module with a database table with the help of this article.
I have a program which syncs stock from my physical store to online store. Every time I sync the stock, I add the sku,name, sync data and time, also the qty to the custom database table. There can be multiple entries for the same sku in the table as the same product can be synced multiple times.
Initially I was taking a report of the unique entries from the database table using this, where I was only getting the skus.
$collections = Mage::getResourceModel('module/namespace_collection')->distinct(true)->addFieldToSelect('sku')->load();
foreach( $collections as $collection){
//add to csv
}
Now I want to get the sku and qty of the unique entries with the latest sync data and time for that particular sku. To get this I tried to use
$collections = Mage::getResourceModel('module/namespace_collection')->distinct(true)->addFieldToSelect('sku')->addFieldToSelect('qty')->addFieldToSelect('date')->load();
but I am getting all the entries in the custom table. Can some one help me out with this?
Get unique records from collection
$collection = Mage::getResourceModel('module/namespace_collection')
->getCollection()
->distinct(true)
->addAttributeToSelect('sku')
->load();
The above collection if not return desire data, try this
$collection = Mage::getResourceModel('module/namespace_collection')
->getCollection();
$collection->getSelect()->distinct(true);
But this will retrieve distinct values based on id. If you want to retrieve products using distinct sku values, you should group by "sku".
$collection->getSelect()->group('sku');
If you want to debug the query executed :
$collection->getSelect()->__toString();
Related
Hello there i have 3 tables
Companies
Coupons
Categories
Company table contains
id, name, logo
category contains
id, title, logo
coupons contains
id, title, terms, company_id, category_id
the last two are for relation ship between company , category modal and coupons modal
After that bieng said, here is the case
i have sent a request to my server using api that containes Company_id.
what i want to do is get all coupons that contain this Company_id , then from those coupons i want to take all Categories
what i tried
public function fetch_Categories($Company_id)
{
//getting all coupons that containes the current company id
$Coupons = Coupons::where('company_id' , $Company_id)->get();
//Coupons variable returns two coupons which is expected
foreach($Coupons as $Coupon){
$Categroies = Categories::find($Coupon->category_id);
}
}
ok you must have noticed the problem,
i am getting two coupons and in the future i will get more and i have to get the category from each one of them using category_id column , then return it using json as a collection of categories. but the question is how should i do that *_^
if it is not clear , i am getting two coupons and i should make foreach to store each category in my variable but when you put "=" sign then you will assign value for the variable and finally you will get one category which is the last one in the foreach , what i need is a way to store all the categories using the foreach and then return them to user using json
There are many ways to achieve this. Here is one:
$categoryIds = Coupons::where('company_id', $Company_id)->pluck('category_id')->unique();
$categories = Categories::whereIn('id', $categoryIds)->get();
This is also more efficient than the foreach loop, as it only uses a single query to fetch all categories.
We are trying to get all attributes associated with each product using a database.
We have been used following query but I think the table we are using is not the right one.
$sql = "SELECT attribute_id FROM catalog_product_index_eav WHERE entity_id=".$entityprodid;
This table is not the exact table which will get all attributes attached to a single product id.
Any suggestions how do we get attributes of a single product in Magento using database ??
Magento uses flat tables to centralize the attributes of products, so to query all the attributes by the ID, you can use the table catalog_product_flat, remembering that for this table to be updated, it is necessary to reindex the data.
I'm new to Laravel and I have a doubt, I have two tables n: n and I want to create an additional table that stores the two ids of each table. Such as shop and product.
How do I create a store by creating an product automatically in the table shop_product?
save shop data
$shop->save();
$shopid= $shop->id;
after save fetch data from shop and simillarly from product
$product->save();
$productid= $product->id;
finally use both variables in shop_product like this
$shop_product->shop_id= $shopid;
$shop_product->product_id= $productid;
Hi I am using PHP to manipulate information in my MySQL database. However I am looking for a way to update a table (all records need to be updated) based on information within another table.
For example I have a list of products lets say 10 each with a unique id stored in a products table. I have a purchases table which has the same product ID and the amount of purchases done for each product. I want to update each product in the products table to reflect the total purchases made for each product and store it in a column called instock which is part of the products table.
How can this be done?
If I understand your situation correctly, you're dealing with a stock-count. When an item is purchased (represented by a entry in the Products table) then the stock count figure should be decreased. This should happen within the same transaction as the new entry to the Products table to keep your data consistent. I would recommend using a Trigger on the table to implement this. You'll find lots of information about implementing triggers in MySQL on this site. A trigger you could use might look something like this:
CREATE TRIGGER update_stock_count
BEFORE INSERT ON Purchases
FOR EACH ROW
BEGIN
UPDATE Products SET stock_count = stock_count - NEW.quantity_ordered
WHERE product_id = NEW.product_id;
END;
This trigger doesn't take into account that there might not be enough stock of a product, nor does it handle updates or deletes on the Purchases table but it could be modified to do so.
I'm making a shopping cart for my website.
When an user add something into the cart it should insert item_id and quantity into the items column in my DB.
The item column should hold a string like this:
1:5;6:2;13:1
item_id:quantity;item_id:quantity and so on
The insert part I already got, but when the user wants to view the cart, the string needs to be split into an array and fetch the items from another table
Array example:
1 => 5,
6 => 2,
13 = 1
I'v tried using spliti(";" $raw);
But didn't get what I wanted.
And the same thing when the user updates/deletes a product from the cart..
When the user updates/deletes a product, it must search through the string inside items and find the current place of the product.
Any help would be appreciated :)
Since you are building your own cart do it correctly using a properly normalized schema. This means that you need to avoid "special" values which do more than 1 thing at the same time.
So the obvious improvement here is to have:
table users:
id
...
table products:
id
...
table cart_items:
user_id
product_id
quantity
CRUD queries will be very simple to implement then and also very fast.
First off, I don't think it's a good idea to have your items as a string in your order/cart table. You run into a lot of problems this way (you've already run into a couple).
It's better to have a hasMany relationship. Something along the likes of the design shown below is ideal.
Order Table : id, date, total, more, fields
OrderItem Table : id, order_id, item_id, value, more, fields //item_id is a ref to your Items/Product table
This allows you to link your items to your orders properly and you can do your inserts and updates without a problem.
As for your current question, you need to split it twice
$items = 1:5;6:2;13:1;
$items_arr = explode(';', $itemrs);
$new_items = array();
foreach ($iterms_arr as $item) {
$pieces = explode(':', $item);
//might want to do some error checking on whether the indices exist
$new_items[$pieces[0]] = $pieces[1];
}