I have one to many relationship model on my database. For example, one property has more than one price:
property 1 => $200, $300
property 2 => $200, $350
etc.
This how to create property :
$prices = array();
$property = new Property();
$property->setName('property 3');
$data_prices = array(200,300,400);
foreach ($data_prices as $price {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
#codes above create new records on table property and prices (related) and property id = 1
But when I try to update :
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
#create new prices
$data_prices = array(10,20,30);
foreach ($data_prices as $price) {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
instead of deleting old price records or updating them, Propel creates new records. I want to delete old price records because I don't need them, or, if I can update the old with the new price it would be great.
How to do this in Propel?
Declaring the Price objects as new does just that—creates new Price objects. You need to either call the Price objects from the Property object or load the Price objects inside your foreach loop. Since I don't know the structure of your Price table, I can only give you the former solution:
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
$prices = $property->getPrices();
$data_prices = array(10,20,30);
for ($i = 0; $i < count($data_prices); $i++) {
if (!isset($prices[$i])) {
$prc = new Price();
$prc->setPrice($price);
$prc->save();
$prices[] = $prc;
} else {
$prc = $prices[$i];
$prc->setPrice($data_prices[$i]);
$prc->save();
}
}
$property->save();
Related
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->save();
$category->category_id = $category->id;
$category->save();
I have this table, only the id is set to not null. I need to insert the column WITHOUT using auto increment, what do I add to my function?
I was thinking to count the id and just to add +1 so that the numbers are in squence:
$category->id = id()->count()+1;
This line is not counting though
fetch last id from db
$categoryId=HmsBbrCategory::orderByDesc('id')->first();
$autoIncId=$categoryId->id+1;
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->id=$autoIncId;
$category->category_id =$autoIncId;
$category->save();
How to get a list of skus whiout using the looping in magento.
Example: I am using below code with my conditons.
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku');
Now I want to result as array('A001','A002'....) etc.
I don't want to iterate (loop) the product collection.
Please suggest.
If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.
$skus = array();
foreach ($productsCollection as $product) {
$skus[] = $product->getSku();
}
If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');
Retrieve the read connection you should use ->getConnection('core_read') not ->getConnection('core_write'). Whole codes is below which runs faster.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$skus = $readConnection->fetchCol('SELECT sku FROM `catalog_product_entity`');
foreach ($skus as $sku) {
echo $sku;
}
I have a custom module with change the default CATALOG/RESOURCE/PRODUCT/COMPARE/ITEM/collection.php, but the line below:
->order('ai.sort_order ASC');
Change the order for position with attributes position, but a few of is (about 10 attributes) is just a text (not Dropdown, Select or Price) and cannot allow to orders correctly position iquals a product page order. (i need to leave the page to compare products in the same order from the product page of the attribute list)
how i can make this?
To create sort by position write below code after getting product collection in app/design/frontend/<theme_name>/default/template/catalog/product/list.phtml
$_productCollection = new Varien_Data_Collection();
$sortedCollection = array();
foreach ($_defaultProductCollection as $key => $_product) {
if(!isset($sortedCollection[$positions[$_product->getId()]])){
$sortedCollection[$positions[$_product->getId()]] = array();
}
$sortedCollection[$positions[$_product->getId()]][] = $_product;
}
ksort($sortedCollection);
foreach ($sortedCollection as $_products) {
foreach ($_products as $_product) {
$_productCollection->addItem($_product);
}
}
Hope it will work for you.
Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}
I am trying to get product price and description for different stores in magento, I can achieve this as follows:-
foreach ($productObj->getStoreIds() as $_storeId) {
$tempStoreObj = new Mage_Core_Model_Store();
$tempStoreObj->load($_storeId);
$tempProductObj = new Mage_Catalog_Model_Product();
$tempProductObj->setStoreId($_storeId);
$tempProductObj->load($productObj->getId());
$tempPriceArray[] = array(
'websiteId' => $tempStoreObj->getWebsiteId(),
'price' => $tempProductObj->getPrice(),
'baseCurrency' => $tempStoreObj->getBaseCurrencyCode(),
);
$tempDescArray[]=array(
'descprition' => $tempProductObj->getData('description'),
'shortDescription' => $tempProductObj->getData('short_description'),
);
}
Now In the above code there, I have first fetched stores for particular product, then loaded the stores then again created an object for product and loaded w.r.t product id and store id , in this way i have achieved the required task.
Now my problems start here when there are many products and many stores performance issue comes in and the loading process makes this slow.
Is there any other way achieve the same?
One thing you can do to speed things up when you want to get this from several products is to use the product collection, so there is only one DB query which fetches the information per store for all products.
This would then look something like this:
$storeId = 1; // Current Store you want to look at
$productIds = array(10,15,26); // Enter your Ids
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()
->addStoreFilter($storeId)
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSelect('price')
->addAttributeToSelect('description');
And then loop over the products:
$currPrices = array();
foreach ($products as $prod) {
$currPrices[$prod->getId()] = $prod->getPrice();
}
To make it clear for several stores:
$currPrices = array();
$currDescriptions = array();
foreach ($productObj->getStoreIds() as $_storeId) {
$productIds = array(10,15,26); // Enter your Ids
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()
->addStoreFilter($_storeId)
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSelect('price')
->addAttributeToSelect('description');
foreach ($products as $prod) {
$currPrices[$_storeId][$prod->getId()] = $prod->getPrice();
$currDescriptions[$_storeId][$prod->getId()] = $prod->getDescription();
}
}
The fastest way is to make direct queries (of course, wrapped to Zend_Db_Select) to store and product tables.
base_currency can be got from core_config_data
product prices can be found in catalog_category_product_index
description in catalog_product_entity_text
Look to
Mage_Reports_Model_Resource_Product_Index_Abstract
Mage_Reports_Model_Resource_Report_Product_Viewed_Collection
Mage_Reports_Model_Resource_Report_Product_Viewed
Mage_Reports_Model_Resource_Quote_Collection
Magento uses Zend_Db_Select, even fetch methods of the adapter, not collections, in case of increasing speed of queries.