Yii1 - count before limit and offset with query all - yii

At the end of the query conditions (the query is a bit long, and the rest of the query is not that important), I have this part:
$command->limit($this->pageSize, $this->getOffset());
$records = $command->queryAll();
Is it possible to perform count before $records = $command->queryAll(); without using SQL_CALC_FOUND_ROWS?

Yes, you can reuse existing command and fetch count using queryScalar('COUNT(*)'):
$count = (clone $command)->queryScalar('COUNT(*)');
$command->limit($this->pageSize, $this->getOffset());
$records = $command->queryAll();

Related

Last insert ID with ModX PDO?

I am trying to get the ID of a last row I've inserted with a php/mysql query:
$createjob = $modx->query($createjob);
$lastId = $modx->lastInsertId();
but this does not seem to be working.
Does any one know the correct way of doing this with ModX PDO?
Try this:
$createJob = $modx->newObject('CreateJob');
$createJob->set( 'value', 1234 );
// try saving
if( $createJob->save() ){
echo $modx->lastInsertId();
}
Read more here
Try simple sql query which gives the max id for record with specified class.
$q = $modx->newQuery('modResource');
$q->select(array(
"max(id)",
));
$id = $modx->getValue($q->prepare());

Issue with getting a single row in zf2 pdo

I am trying to fetch the single row using the pdo statement, but i am getting the error like ..
Fatal error: call to undefined method fetch()
$sql = new Sql( $this->adapter );
$select = $sql->select();
$select->from('users');
$where = new Where();
$where->equalTo('user_id',$userId);
$select->where($where);
//echo $select->getSqlString($this->adapter->getPlatform());
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$row = $statement->fetch();
//getting the result set for the below, but not the above statement fetch
$rows = array_values(iterator_to_array($result));
There's no "fetch" method in the statement object. If you want to get a single row then get the current entry of the result iterator, like so:
$row = $result->current();

ColdFusion - how do I execute an SQL "IN" clause in cfscript?

How would I construct an IN clause in cfscript? Here's what I have:
var tagList = "301,302,303,304";
var q = new Query ();
q.setDatasource ("mydatasource");
var sqlStmt = "SELECT * FROM Tags WHERE tagID IN (:tagList)";
// I know the next line is not correct!
q.addParam (name="tagList", value="#tagList#", cfsqltype="??? WHAT SHOULD IT BE ???");
Is there an easy way to do this, or do I need to iterate through the list of tags, adding each one separately?
There is a list attribute on <cfqueryparam> which ought to be supported:
q.addParam (name="tagList", value="#tagList#", cfsqltype="CF_SQL_INTEGER", list="yes");

how to get the second batch and 3rd batch in the same query result in oracle sql + yii framework?

let' say i have 20 results in the sql query. if am gonna use the limit in the yii active record, I'll obviously get the first four from the result, but what if i wanna get the 2nd four and then 3rd four in the same query result ? how to query that via sql ?
e.g
$criteria2 = new CDbCriteria();
$criteria2->select = 'USERID, ADID ,ADTYPE, ADTITLE, ADDESC, PAGEVIEW, DISPPUBLISHDATE';
$criteria2->addCondition("STATUS = 1");
$criteria2->order = '"t".PAGEVIEW DESC,"t".PUBLISHDATE DESC';
$criteria2->limit = 4;
$criteria2->with = array('subcat','adimages');
$result = $this->findAll($criteria2);
return $result;
Sorry :)
See here, how to paginate with Oracle (Are you use 11g?)
Alternatives to LIMIT and OFFSET for paging in Oracle
Well and in Yii just set offset, OCIConnector will set rownum automaticly for sql
$criteria2 = new CDbCriteria();
$criteria2->select = 'USERID, ADID ,ADTYPE, ADTITLE, ADDESC, PAGEVIEW, DISPPUBLISHDATE';
$criteria2->addCondition("STATUS = 1");
$criteria2->order = '"t".PAGEVIEW DESC,"t".PUBLISHDATE DESC';
$criteria2->limit = 4;
$criteria2->offset = 0; //4, 8 - COciCommandBuiled applyLimit use it
$criteria2->with = array('subcat','adimages');
$result = $this->findAll($criteria2);
return $result;

Kohana ORM count_all() working but find_all() is not

I'm having an issue where I'm building an ORM query based on several conditions from a $_POST. The final query looks fine and returns records in a direct SQL query (phpmyadmin) but in my application does not return any records. here is the code...
$records = ORM::factory('record')->where(array('date >='=>$_POST['fromdate'],'date <='=>$_POST['todate']));
if ($_POST['agent'] != '0') $records->where(array('ccp_id'=>$_POST['agent']));
if ($_POST['supervisor'] != '0') {
$ccps = ORM::factory('employee')->where(array('supervisor_id'=>$_POST['supervisor'],'active'=>'1'))->find_all();
foreach ($ccps as $ccp) {
$agents[] = $ccp->id;
}
// echo kohana::debug($agents);
$records->in('ccp_id',$agents);
}
if ($_POST['lead'] != '0') $records->where(array('lead_id'=>$_POST['lead']));
if ($_POST['reasons'] != '[]') {
$reasons = explode(',',str_replace(array('[',']','"'),'',$_POST['reasons']));
$records->in('reason_id',$reasons);
}
$records->find_all();
$records->loaded is false. If I change out the find_all() with count_all() I get an accurate count.
With sample data in the $_POST I have this query in $records->last_query()
SELECT `records`.*
FROM (`records`)
WHERE `date` >= '2010-10-10'
AND `date` <= '2010-11-09'
AND `ccp_id` IN ('E128092','E128093','E124874','E124414','E129056','E137678','E078952','E112701','E084457','E098047','E099221','E001131','E120892')
AND `lead_id` = 'E110873'
AND `reason_id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
ORDER BY `records`.`id` ASC
this returns 4 records in phpmyadmin and (4) for count_all().
I do not understand why this is happening. Any insights would be helpful. Thank you.
In your last line you should have
$records = $records->find_all();
instead of
// this actually returns you the resultset and resets the query builder object
$records->find_all()
$records is a Database_Result and has no loaded property. Use count($records) or iterate it with foreach statement to get ORM objects.
Just a note: It's probably better not to wipe out the ORM object ( $results = $records->find_all() instead of $records = $records->find_all()) if you wish to use $records->count_all() or other calls later in your code. Just an issue I ran into.