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()
Related
I'm trying to get values from my linking table sfees with columns student_id and mfee_id. Here, there might be multiple student_id with different mfee_id. The thing is that, i want to retrieve all mfee_id with same student_id.
I have used following syntax, but it is only returning single value:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->value('mfee_id');//trying to get only mfee_id
return $sfees;
}
How can i solve this problem?
//edited
My table looks like:
You need to do a groupBy -
$sfees = sfee::where('student_id', '=',$sid)->groupBy('student_id')->get();
UPDATE
Try something like this -
$sfees = sfee::where('student_id', '=',$sid)->lists('mfee_id');
Or you can use the Schema Builder like this -
DB::table('sfees')->where('student_id', '=', $id)->lists('mfee_id');
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 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.
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)
I have a Doctrine query;
$q = Doctrine_Query::create()->select('s.monthly_volume')
->from('SearchVolume s')
->innerJoin('s.Keywords k')
->where('k.group_id = ?',array($group_id));
I just want it to return the monthly_volume value in the result array. It currently returns monthly_volume and id, I don't want it to return the id in result.
Doctrine automatically adds the primary key field to the results in almost every type of hydration mode.
In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:
$q = Doctrine_Query::create()->select('s.monthly_volume')
->from('SearchVolume s')
->innerJoin('s.Keywords k')
->where('k.group_id = ?');
$monthly_volumes = $q->execute(array($group_id), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
You should find that $monthly_volumes is a simple one-dimensional array containing only the value(s) you wanted.