I have an array of Ids which has already been ordered based on some criterias
Example:
$array = array(5,4,10,3,1);
and i am using yii to get all these records from database:
$records = TableName::model()->findAllByPk($array);
this is returning me the correct results but its being sorted by the primary key in ascending order.
i even tried using CDbCriteria like this:
$criteria = new CDbCriteria();
$criteria->addInCondition('primaryKeyColumnName',$array);
$records = TableName::model()->findAll($criteria);
and its still being sorted.. can anyone help me plz
Add This line and try I think It will work as you want.
$criteria->order = "FIELD(id, '5,4,10,3,1')";
Related
I've been sitting for hours trying to find how to get values from table's iab_categories column category_name. I've found only the way to echo all table names:
$connection = Yii::app()->db;//get connection
$dbSchema = $connection->schema;
//or $connection->getSchema();
$tableNames = $dbSchema->getTableNames();//returns array of tbl schema's
var_export($tableNames);
Can anyone help me?
You can use query builder to do that:
$categories = (new \yii\db\Query())
->select(['category_name'])
->from('iab_categories')
->column();
The select() method sets what columns should be included in result.
The from() method sets what table should be queried.
And the column() method executes the query and return first column from result set as array.
EDIT: now, I've realized that even though you've mentioned Yii 2 in title the code you've included in question looks more like Yii 1.x.
So there is query builder version for Yii 1.x:
$categories = Yii::app()->db->createCommand()
->select('category_name')
->from('iab_categories')
->queryColumn();
I am trying to display buildings and circuits in list (only 3 in total, sorted by the column "updated_at"). The problem is that they are not in the same table. I have a table circuits and a table buildings.
I tried to do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits);
$this->data['buildingsAndCircuits'] = $merged->all();
I get everything right when I'm doing my var_dump and I know how to access the data when I do a foreach. But that does not do what I want.
I would like to sort them (by updated_at) and have only three and not six.
Is there a way to do that?
I tried to make conditions like with the QueryBuilder on $merge but it does not work
$this->data['buildingsAndCircuits'] = $merged->orderBy('updated_at', 'DESC')->limit(3)->all();
thank you very much
Once you've called get on each query then the query is executed and the result is returned. orderBy will not longer work after that since what you have in $buildings, $circuits and $merged is a collection.
You can however do this:
$buildings = Building::published()->limit(3)->get();
$circuits = Circuit::published()->limit(3)->get();
$merged = $buildings->merge($circuits)->sortByDesc('updated_at');
$this->data['buildingsAndCircuits'] = $merged->all();
Check what else you can do on collections in the documentation
How to run 'OR' condition in model()->findByAttributes()?
This is my code:
$criteria->condition='url_title_en=:x OR url_title_da=:x';
$criteria->params=array(':x'=>$_GET['purl_title']);
$parentcat = Category::model()->findByAttributes($criteria);
But I'm getting this error:
Table "tbl_category" does not have a column named "select".
You can use OR like this
$criteria=new CDbCriteria();
$criteria->select='category_id';
$criteria->condition='url_title_en=:x';
$criteria->addCondition('url_title_da=:x','OR');
$criteria->params=array(':x'=>$_GET['purl_title']);
In above code addCondition appends the extra condition with OR operator. Now If you expect multiple records then you can use
$parentcat = Category::model()->findAll($criteria);
If you expect single record the you can use
$parentcat = Category::model()->find($criteria);
What you are using here that you are passing criteria to function FindByAttributes() which finds records with respect to attributes passed to it. Now you are passing it criteria and it finds first word as "select" because criteria translates your query like "select * where category_id=3". Now findByAttributes() take it as an attribute and tries to find it. thats why it shows error that tble_category cant find select.Here is official documentation of yii http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail
The only problem with your code was that you were using findByAttributes instead of findAll.
$criteria = new CDbCriteria;
$criteria->condition = '`url_title_en` = :uri OR `url_title_da` = :uri';
$criteria->params = array(':uri' => $_GET['purl_title']);
$records = Category::model()->findAll($criteria);
// print all records fetched.
print_r($records);
I want to get all rows , certain columns using CActive record
I heard about findAll(); but it returns all rows, colums
$models = Users::model()->findAll();
How to get certain columns for example, only username, password for all users?
You can do with CDbCriteria
$criteria = new CDbCriteria;
$criteria->select = 't.username, t.password '; // select fields which you want in output
$models = Users::model()->findAll($criteria);
If you want the result in the form of array try this
$records= Yii::app()->db->createCommand()
->select('username,password')
->from('users')
->queryAll();
If as an object then kumar_v has already given an answer
the Kumar_v 's answer is completely correct, but if you want only username and password (or any two fields),
you can also use CHtml::listData() function like this :
$users=CHtml::listData(Users::model()->findAll(array('condition'=>'*condition*',
'limit'='*limit*')),"username", "password");
the $users is an array like this :
$users = array(
'username1'=>'password1',
'username2'=>'password2',
.
.
.
);
and of course you can access to a username's password simply by $users['username1']
You could use as usual sql query but getting so would not be in yii standard
So get findAll() and get the data that you want. that is proposed and best.
I'm currently doing this:
$t_category = 781;
$res = $collection->findOne(array("_id" => intval($t_category)));
this way i am getting only one category (it was just to try). How can I get all categories (documents->_ids) in php->mongo? A way I could iterate over all the documents in the collection. I have tried the find with no arguments. No luck. Any idea?
Calling find with no arguments will return all items in the collection.
$results = $collection->find();