How to write query to display all product? - magento-1.7

I want to update all products price. For that I want to fetch all records and then later on update the product in Magento.
$product = Mage::getModel('catalog/product');

$product_collection = Mage::getModel('catalog/product')->getCollection();
foreach($product_collection as $product) {
$new_price = $product->getPrice(); // Update price here.
$product->setPrice($new_price)
->save();
}
This will help you to update the price of all products..

Related

Trigger to auto populate field values

I have Custom object named Team__c and Fields are like, Category__c(Picklist), Close_Date__c(Date), Stage__c(Picklist) also have Standard Opportunity object. The Team__c object and Opportunity has look-up relationship with Category__c(Picklist), Close_Date__c(Date), Stage__c(Picklist) fields . In opportunity I have custom field named Type__c(Picklist), whenever the Type__c from Opportunity is equal to 'New Team' (Type__c == 'New Team') the Category__c, Close_Date__c, Stage__c fields should populate in Team__c record while it create.
Can anyone help on this?
Thanks in advance.
I have try with following trigger,
if(trigger.isAfter && trigger.isInsert)
{
List<Team__c> tt = new List<Team__c>();
Opportunity opp = new Opportunity();
if(opp.Type__c == 'New Team')
{
for(Opportunity oppty : trigger.new)
{
Team__c team = new Team__c();
team.Category__c = oppty.Category__c;
team.Close_Date__c = oppty.Close_Date__c;
team.Stage__c = oppty.Stage__c;
tt.add(team);
}
}
}

Yii2: How to do a simple join query?

I am learning how to do simple queries using the Yii2 framework. I use PostgreSQL.
I am trying to join two tables and get the data from both tables with a where condition.
The tables are called Admins and Persons.
The join use field called idadm.
The condition is idadm = 33. This works great but the result has data only from the Admins table and I need data from the other table.
Here is my example:
$query = \app\models\Admins::find()
->select('*')
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->all();
I am following the Yii2 official guide: http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
Update: Here I show the updated code that doesn't solve de problem:
You need to write all column name in select().
$query = \app\models\Admins::find()
->select('admin.*,persons.*') // make sure same column name not there in both table
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->all();
And also you need to define person table attributes in Admin model.
Second way is get records as array,so you dont need to define attributes in Admin model.
$query = \app\models\Admins::find()
->select('admin.*,persons.*') // make sure same column name not there in both table
->leftJoin('persons', 'persons.idadm = admins.idadm')
->where(['admins.idadm' => 33])
->with('persons')
->asArray()
->all();
Ensure that active record has required relations, e.g. something like follows:
class Admins extends \yii\db\ActiveRecord {
public function table() {
return "admins";
}
public function getPersons()
{
return $this->hasMany(Person::className(), ['idadm' => 'idadm']);
}
}
class Person extends \yii\db\ActiveRecord {
public function table() {
return "persons";
}
}
Then use joinWith to build query:
$query = Admins::find()
->joinWith('persons')
->limit(1);
$result = $query->createCommand()->getSql();
echo $result;
Here is produced query:
SELECT `admins`.* FROM `admins`
LEFT JOIN `person` ON `admins`.`idadm` = `person`.`idadm` LIMIT 1

Laravel Eloquent distinct values

PROBLEM SOLVED.
I'm trying to get an person's books, but there are more than one book in my book table since there are different editions of a book. When I list all books of a person, I shouldn't list duplicates.
Here's what I've done so far
Person Model
public function books() {
return $this->belongsToMany('\App\Models\Thing', 'bookxauthor', 'person_id', 'thing_id');
}
PersonController.php
$allbooks = Person::find($id)->books;
This is great, it lists all the books of an author, but I don't need duplicates.
The query below works. type_id means it's a book.
$findBooks = Person::with(array('books' => function($query)
{
$query->where('type_id',"=",3)->groupBy('original_name');
}))->find($id);
$allbooks = $findBooks->books;
You could use the groupBy function for collections
Eg.
$allbooks = Person::find($id)->books->groupBy('name')->get();
The query below works.
$findBooks = Person::with(array('books' => function($query)
{
$query->where('type_id',"=",3)->groupBy('original_name');
}))->find($id);
$allbooks = $findBooks->books;

typo3 tt_news Retrieve Category ID in template

Im stuck with a problem and hope you can help me out. I am using
tt_news and I need to retrieve the ID of the category directly
in the LATEST view. ###NEWS_CATEGORY### gives me the name of the
category, but I need the corresponding ID. Is this possible?
Thank you
I helped myself and used generic markers if anyone needs it:
plugin.tt_news.genericmarkers {
catid = RECORDS
catid {
tables = tt_news_cat
source.data = register:newsCategoryUid
conf.tt_news_cat = TEXT
conf.tt_news_cat.field = uid
}
}
It should be enough to pass the register like this:
plugin.tt_news.genericmarkers {
CATID = TEXT
CATID {
value = {register:newsCategoryUid}
insertData = 1
}
}

Display Latest Magento products without setting date

I use Magento 1.7.0.2.
I'd like to show the 8 latest added products in my homepage, but without setting a date "from" and "to". I need it to be automatically.
Does anyone know of a solution?
Product IDs are incremental. By ordering them descending and limiting the collection to 8 you will have 8 last products.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->order('entity_id desc')->limit(8);
/* just for testing
Mage::log($collection->getSelect()->assemble());
foreach ($collection as $product) {
Mage::log($product->getSku());
} */
With the collection you can do whatever you need, add visibility and status filter etc.
in order to do that you will need to use the date that the order was created. The key to display all products information as price, name, and etc. is to us ->addAttributeToSelect('*') Here is the script:
$store_id = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
->addStoreFilter($store_id)
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('status', 1)
->addAttributeToSelect('*')
->setVisibility(array(2,3,4))
->setOrder('created_at', 'desc')
->setPage(1, 9);