I would like to use an IN clause in findFirst but it doesn't seem to work?
Expected code, or something similar:
$item = Item::findFirst([
'conditions' => 'categories IN :cats: AND released < :now:',
'order' => 'id ASC',
'bind' => [
'cats' => $this->categories,
'released' => time()
],
]);
I tried using bindTypes but there's no such "list" or "array" type (also, that would get a lot more verbose than expected)...
I know I can do it through the query builder, but I was looking to keep it a bit more idiomatic:
$item = Item::query()
->inWhere('categories', $this->categories)
->andWhere('released < :now:', ['now' => time()])
->orderBy('id ASC')
->limit(1)
->execute()
->getFirst();
You can bind the array and IN clause like this:
$result = ModelName::findFirst([
'conditions' => 'id in ({cats:array})',
'bind' => array('cats' => [3, 5, 8])
]);
Note that the above example will get the first record with id 3. However if you use find() you will get the items with 3, 5 and 8.
More examples in the docs (at the bottom of this section).
Related
I have a comparative set of arguments for WP_Query involving a custom field.
On a page I need to say "Are there going to be results?, if so display a link to another page that displays these results, if not ignore" There are between 500 and 1200 posts of this type but could be more in the future. Is there a more efficient or direct way of returning a yes/no to this query?
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
The link is not made from data returned, we already have that information.
Perhaps directly in the database. My SQL is not up to phrasing the query which in English is SELECT * from wp_posts WHERE post_type = 'product'} AND (JOIN??) post_meta meta_key =
partner AND post_id = a post_id that matches the first part of the query.
And if I did this, would this be more efficient that the WP_Query method?
Use 'posts_per_page' => 1 and add 'no_found_rows' => true and 'fields' => 'ids'. This will return the ID of a matching post, and at the same time avoid the overhead of counting all the matching posts and fetching the entire post contents. Getting just one matching post id is far less work than counting the matching posts. And it's all you need.
Like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'partner',
'value' => $partner,
'compare' => 'LIKE',
),
),
);
$partner_query = new WP_Query($args);
if ($partner_query->have_posts() ) { [MAKE LINK] }
no_found_rows means "don't count the found rows", not "don't return any found rows". It's only in the code, not the documentation. Sigh.
I have a belongsTo array in my gal_provider model as below:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => 'User.name'
),
)
And most of the functions inluded in the model gal_provider requires to sort the results according to the user names. But there is a function called recevtProviders() does not require this order. So I tried
$order = "";
$gal_providers = $this->find("all",array("conditions"=>$conditions,"recursive"=>$recursive,
"fields"=>$fields,"limit"=>$limit,"order"=>$order));
BUt the query generated still shows ORDER BY "User.name". How can I disable this order only from this function?
I believe the order parameter you're passing is only going to affect the gal_provider model. Try this just before your find call to remove the order from the associated model:
$this->belongsTo['User']['order'] = '';
Also, if you're not using it, Containable is a very useful behavior. Using containable, the order on the User model could be disabled like this:
$this->find("all",array("conditions"=>$conditions,"recursive"=>$recursive,
"fields"=>$fields,"limit"=>$limit,"order"=>$order,"contain" => array("User" => array("order" => ""))));
I have a query as follows
$criteria1 = new CDbCriteria();
$criteria1->condition = 'id = 1';
$modelA=Table1::model()->find($criteria1);
I can pass it to a view and return the title and entry
$this->widget('bootstrap.widgets.TbBox', array(
title' => $modelA['title'],
'content' => $modelA['entry'] ));
Now I'd like to return a range of entries
$criteria2 = new CDbCriteria();
$criteria2->condition = 'id > 7';
$modelB=Table1::model()->findAll($criteria2);
(btw : I'm following a form as laid out here). I was expecting to be able to read the resulting array of values out as below, but ['title'] is now being seen as a undefined index (obviously I'm expecting to read this out in a loop but you get the point)
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $modelB['title'][0],
'content' => $modelB['entry'][0]));
Where am I going wrong?
Thanks
No, the indexes should be specified in the different order: the number of a specific element first, then the name of the property. Additionally, it's better (=cleaner) to name the result of findAll so it'll show you (and any other reader) that it's a collection, not a single model:
$models = Table1::model()->findAll($criteria2);
// ...
$this->widget('bootstrap.widgets.TbBox', array(
'title' => $models[0]['title']
//...
));
But even that's not necessary if you use foreach (and you probably will):
foreach ($models as $model):
// ...
$this->widget('some.name', array(
'title' => $model['title']
);
endforeach;
i am new in cakephp so i dont know i to how write this query in cakephp . at times now i have this query
$count = $this->User->find('count', array(
'conditions' => array('User.mobileNo' => $mobileNo)));
this query is checking that if the mobile number in database is equal to the one the user has given .. i want to add another condition which is
mobile number is equal to the one the user has given the mobile number and email is equal to the one the user has given the email for example
$count = $this->User->find('count', array(
'conditions' => array('User.mobileNo' => $mobileNo))) And
'conditions' => array('User.email' => $email)))
You need only add to your conditions array:
$count = $this->User->find('count', array(
'conditions' => array(
'User.mobileNo' => $mobileNo,
'User.email' => $email
)
));
There are many examples like this in the documentation e.g..
$conditions = array("Post.title" => "This is a post", "Post.author_id" => 1);
// Example usage with a model:
$this->Post->find('first', array('conditions' => $conditions));
Explanation for the use of $condition and $param in findByAttributes in Yii
In most case, this is how I use findByAttributes
Person::model()->findByAttributes(array('first_name'=>$firstName,'last_name'=>$lastName));
Copy from this thread http://www.yiiframework.com/forum/index.php/topic/21178-findbyattributes-example/
Explain what you want to do and where is your errors.
Try to read documentation http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findByAttributes-detail
When you do a find in Yii's CActiveRecord using the attributes of the model, you would most likely find by some equality.
Person::model()->findByAttributes(array(
'first_name' => $firstName,
'last_name' => $lastName,
));
In English, this translates to "Find me a person whose last name is $lastname and whose first name is $firstname". But this is a bad example. Consider the following.
Person::model()->findByAttributes(array(
'age' => 18,
));
This is a better example which translates, in English, to "Fetch me all eighteen year olds" (that is, "where age = 18). This can be re-written with conditions like so
Person::model()->findByAttributes(array(
'condition' => 'age = :age',
'params' => array(
':age' => 18,
),
));
So far, there may seem to be little benefits. But such expressions make us able to use more boolean operators. For example...
Person::model()->findByAttributes(array(
'condition' => 'age < :age',
'params' => array(
':age' => 18,
),
));
...can now help us find all those below a certain age.
The query can grow as complex as you wish with $conditions and $params, but with your normal method, you may only use assignment queries.