Using multiple two column values to access database information - magento

I am using Magento 1.7.0.2 and need to access information within my database. I have achieved this by
$params = $this->getRequest()->getParams();
$prefs = Mage::getModel('prefs/prefs')->setCurPage(1)
->setPageSize(1);
$prefs->load($params['id']);
$data = $prefs->getData();
But this is only selecting data by using a single column as a reference within my table and I need to use two columns to get a unique value.
How do I select those two columns and apply those values as the parameter required in the address bar?
In my reseearch I have looked at this and applied _query, but couldn't work out how to get it to work.

You have to query like this check following code and change as per your requirement
$Model =Mage::getModel('prefs/prefs')->getCollection();
$Model->addFieldToFilter('first_column',array('eq'=>'your_id'))
->addFieldToFilter('Second_column',array('eq'=>'Your_filter_data'));
$finalData = $Model->getData();

Related

query to get only one item from target column in Laravel

Is there any query to get only one item from target column? below didn't working?
App\Job::whereNotNull('deleted_at')->pluck('customer_name')->first()
I wish to get just one item name from 'customer_name'
Now Laravel 5.6 using...
When using eloquent, using first will give you a single item, the first record matching your query. You can then access the property on that item:
// This will give you an instance of App\Job (or null)
$job = App\Job::whereNotNull('deleted_at')->first();
// You can then access the customer_name property on the object
$job->customer_name;
If you only want to retrieve that single column when you run your query, you can pass an array of columns to first.
App\Job::whereNotNull('deleted_at')->first(['customer_name']);
Assuming you're using the SoftDeletes trait in your model, your query will automatically have an additional check added to all of your queries to ensure that deleted_at is null. So when you're doing ->whereNotNull('deleted_at') you're adding an additional clause to ensure records are not null as well, so you won't have any records being returned.
If you want to look only at deleted records, you can use onlyTrashed():
App\Job::onlyTrashed()->first();
There is a built-in method for this: value('field')
App\Job::whereNotNull('deleted_at')->value('customer_name');
Docs: https://laravel.com/docs/5.6/queries, look for "Retrieving A Single Row / Column From A Table"
If you meant only one field but still as rows, you can use ->select('field') instead.
And since you want only trashed items, you can use ->onlyTrashed() instead of the not null check.
Soft deleting documentation: https://laravel.com/docs/5.6/eloquent#deleting-models
Try like this:
$job = \App\Job::selectRaw('customer_name')
->whereNotNull('deleted_at')
->first();
And use it like $job->customer_name

Magento - Mageworx Extended Orders - rearrange columns

I have more or less inherited a Magento site, which seems to be using the Mageworx Extended Orders extension for the order grid in the admin area.
Client would like the columns to be rearranged, eg, move Email over beside Voucher. In the System Config you can select columns but there doesn't seem to be any way of ordering them. I have searched through the code and tried editing code\local\MageWorx\Adminhtml\Block\Orderspro\Sales\Order\Grid.php to no avail.
I also looked in the database but couldn't find a table which might have the columns selected so that I could order them.
Thanks.
As it happens it was that file, MageWorx\Adminhtml\Block\Orderspro\Sales\Order\Grid.php, that I needed to edit. I took out the loop and just commented out the fields I didn't want showing and put the rest in the required order
You can change it in the file: app/code/local/MageWorx/OrdersPro/Block/Adminhtml/Sales/Order/Grid.php in the _prepareColumns() method.
At the moment changing columns order is quite a challenge and can be done only this way:
$listColumns = $helper->getGridColumns();
$listColumnsChanged = $listColumns; // create a copy of $listColumns
$listColumnsChanged[0] = $listColumns[3];
$listColumnsChanged[3] = $listColumns[0]; // interchange the columns that have id 3 and 0
var_dump($listColumns); // var_dump will let you learn ID of the column you need
foreach ($listColumnsChanged as $column) { // change foreach for getting to $listColumnsChanged

Doctrine 2 Add custom field using DQL is possible?

I can't solve this problem. I have a very large code for filters using a QueryBuilder instance
$qb = new QueryBuilder();
$qb->select('item)->from->("BundleExample:Item");
$qb->andWhere("item.idProvince = {$idProvinde");
if($price)
$qb->andWhere("price betwenn..");
Ok , there is too much lines.
Now i need to add a virtual column (distance) or overwrite a current field value.
$qb->select('item, COS(..) as distance')
As i understand , i need to create a ResultSetMapping , but i have to re-do all the filtering process and this is very annoying.
Any ideas?
thanks
You don't have to use native queries nor RSM, as Doctrine is capable of returning mixed/hybrid result sets: Pure and Mixed Results.

Yii ActiveRecord Primary Key retrieval

I have looked around and have not found anything similar to what I am asking.
Short of extending the CActiveRecord class is there a way to query just the primary key values (or any column's values) of a table and have an array of those values returned instead of a collection of activerecord objects?
ie
ModelName::model()->getColumnValues('column_name');
I keep having to get a list of record that match a certain condition and then run through the results to pull out the column values I need. I would like to cut out the last step and just get the values instead of the entire record.
Any ideas?
Using a CDbCommand you can execute a query and fetch the results from one column with the queryColumn method. queryColumn returns the results from the first column.
Example:
$command = Yii::app()->db->createCommand("SELECT column FROM table WHERE ...");
$result = $command->queryColumn();

query magento limit + order by rand()

function getIdModelsSliderJuwels(){
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("attribute_set_id", 27);
$collection->addAttributeToSelect('modellijnen');
// $collection->setRandomOrder();
// $collection->getSelect()->limit( 5 );
return $collection;
}
Hi there,
I'd like to know how to set a limit to your query running in Magento because
$collection->getSelect()->limit( 5 ); doesn't work.
Also how to select randomly, $collection->setRandomOrder(); also doesn't work.
txs.
setRandomOrder does not work for collections of products, only for related products. You'll have to add it yourself with this code:
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
A shortcut for setting both page size and number at the same time is:
$collection->setPage($pageNum, $pageSize);
As clockworkgeek said, use the $collection->getSelect()->order(...) method to randomize the order. To limit it to just $n number of items you can also use
$collection->getSelect()->limit($n);
try to use
$collection->setPageSize(5)->setCurPage(1);
Using ORDER BY RAND() to return a list of items in a random order will require a full table scan and sort. It can negatively affect performance on large number of rows in the table.
There are several alternative solutions possible of how to optimize this query. Magento provides a native solution for that.
The orderRand() method of Varien_Db_Select and the database adapter allows to specify a random order and leverage index for ORDER BY. Specify a name of some integer indexed column to be used in the ORDER BY clause, for example:
$collection->getSelect()->orderRand('main_table.entity_id');
See Varien_Db_Adapter_Pdo_Mysql::orderRand() for implementation details.

Resources