I want to add a "CONVERT" stored procedure like this:
SELECT id, value, CONVERT(value, DECIMAL) AS ordred_value FROM test ORDER BY ordred_value;
to this collection query :
$collection = $this->getAssociatedProductCollection($product)
->addAttributeToSelect('*')
->addFilterByRequiredOptions()
->setPositionOrder()
->addStoreFilter($this->getStoreFilter($product))
->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)))
->addAttributeToSort('my_attribute', 'DESC');
for the purpose of ordering the associated products by my custom attribute "my_attribute" that have numeric values in text fields.
Thanks for help.
I'm not sure it's the bast solution, but you can do it like this:
<?php
$collection->getSelct()->columns(array('converted_value' => 'CONVERT(e.my_attribute, DECIMAL)'));
It will add a new column to result. Next sort the collection using it's select as above and the order method.
Related
I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()
I am getting an array of category id eg. ['1','2','3']
and in my database table it is stored as 1_abc eg : category : 1_Sameer
Now it is easy if i get one value
ie. WHERE category like 1_%;
But since this is an array how can i use like or regex to compare with the database values
In this case you can do as many queries as you have array elements and merge these results together if needed to get single collection of elements.
$results = new Collection();
foreach($ids as $id) {
$categories=Category::where("id","like","$id_%")->get();
$results=$results->merge($categories);
}
you have to do looping for that
for($i=0;$i<=count($array_result)-1;$i++)
{
$result=mysql_query("select * from table_name where category like '{$array_result[$i]}_%' ");
}
My database (mysql) tables use TIMESTAMP columns, and whenever I want them returned in a query, I want them to be queried as "UNIX_TIMESTAMP(columnname)".
How do you easily modify queries in zend framework to achieve this?
For example, the current code is:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
This eventually becomes:
select * from tablename where user_id = 42;
I want something that automatically finds the TIMESTAMP column and changes the resulting query to:
select user_id,name,unix_timestamp(created) where user_id = 42;
I know I can use a MySQL view to achieve this, but I'd rather avoid that.
Thanks.
RR
You should be able to specify the fields you want in the select using the $select->from() object.
Zend_Db_Select
You should end up with something like this.
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);
If you wanted to run an expression that doesn't have parenthese in the function, Use the Zend_Db_Expr() method to escape the query properly.
I'm new in mysql language and can not understand difference between procedures and functions, can anyone answer in which case should be use this routines ?
Also have some example => I've table named "data" and columns named "id"(primary key) , "local". this local includes multiple exactly same data. I want to search every id of this "data" (and after result manipulate with it) table which local equal to (for instance) 'something'
Please answer in this question ... Thanks
Functions:
select <function>(column) from table where <condition>;
Procedure:
call <procedure>( param0, param1 );
To get your result:
select * from <table> where data like "%something%";
I suppose that you use php with mysql.
your query should be
$result = mysql_query("SELECT * FROM data WHERE local='something'");
while($row = mysql_fetch_array($result))
{
// here you manipulate with your data
// for example:
echo $row['id'];
}
I cant figure out why when I try to use FORMAT function to limit number of decimal places in the results of MySQL query it doesn't work. Here is how my code looks like:
...some other options to join tables with some conditions....
$options['fields'] = array(
'MetricSim.sim_id',
'MetricSim.metric_id',
'FORMAT(MetricSim.value,3) AS value'
);
$metrics_sims = $this->Sim->find('all', $options);
If I don't use the FORMAT function I get all of the results as expected. But when I try to use it I just don't get value field in my results (the rest of the fields are in place).
Why do you want to use FORMAT in your query? You can use a Helper to format your data in your view.
For example, in your controller class you add:
var $helpers = array('Number');
and in your view, you can format the value like:
$number->format($metric_sims['MetricSim']['value']);
(See NumberHelper class)