cakephp use "OR" "AND" in find query - sql

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));

Related

Existence of posts from complex WP_Query

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.

Converting SQL query to CakePHP

I have this SQL query that I need to convert to CakePHP. I used this website [http://dogmatic69.com/sql-to-cakephp-find-converter][1] that converts the code but I doesn't work in the way that I want. There is no results.
I think it is because it creates an empty array
here is the SQL code :
SELECT shops.phone1 FROM galleries , albums , shops
WHERE galleries.album_id = albums.id and albums.shop_id = shops.id and galleries.id = 210
and this is the result the website gives me :
$options = array(
'fields' => array(
'shops.phone1',
),
'joins' => array(
array(
),
),
'conditions' => array(
'Gallery.album_id = albums.id',
'albums.shop_id = shops.id',
'Gallery.id' => '210',
),
);
$data = $this->find('all', $options);
but the code doesn't work.
Any Ideas what could be wrong ?
Thanks
There are many ways to achieve this.
If you have defined your associations correctly then you can do this:
$data = $this->Shops->find('all')
->contain(['Galleries','Albums'])
->fields(['Shops.phone1'])
->where(['Galleries.id' => 210]);
Otherwise you can use custom join to generate your query:
$data = $this->Shops->find('all')
->join([
'albums' => [
'table' => 'albums',
'type' => 'INNER', // define your join type here
'conditions' => 'albums.shop_id = Shops.id',
],
'galleries' => [
'table' => 'galleries',
'type' => 'INNER', // define your join type here
'conditions' => 'galleries.album_id=albums.id',
]
])
->select(['Shops.phone1'])
->where(['galleries.id' => 210]);
Further Reading: Cakephp -> Query Builder -> Adding Joins

Update Post status when post is expire

I want to update post status when a post is expiring.
I have saved expiry date in the WordPress post meta (post_price_plan_expiration_date).
I know how to get an expired post with wp_query,
But I want to use SQL query to update post status.
$todayDate = strtotime(date('m/d/Y h:i:s'));
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
),
array(
'key' => 'post_price_plan_expiration_date',
'value' => $todayDate,
'compare' => '<='
),
)
);
$wp_query = new WP_Query($args);
print_r($wp_query);
This code returns me correct posts which I need, But I need to write the same query in SQL, And run that with wp_schedule_event
Any help???
You can always do the following out of a WP_Query
$wp_query = new WP_Query( $args );
echo $results->request;
Which should display the generated SQL Query.
Hope this helps.

Sorting in yii 1.1

I have question regarding yii 1.1 sort.
I have three tables ticket, repairLogs and part table .
Following relations have been defined in Ticket model.
'repairLogs' => array(self::HAS_MANY, 'RepairLog', 'ticket_id', 'order'=>'ts DESC'),
and in repairLogs Table
'part' => array(self::BELONGS_TO, 'Part', 'part_id'),
Part table has a column 'number' and I want to sort the data based on "number". Can anyone guide me how to do this since I am new to the yii 1.1 framework.
You can do this during the find()
Ticket::model()->with(array('part', 'repairLogs'))->findAll(
array(
// your conditions here
'order' => 'part.number DESC', // "part" is the alias defined in your relation array (in the Ticket model file)
'limit' => 10,
)
);
If you are using A dataProvider, you can set it as a default order:
new CActiveDataProvider('Ticket', array(
'criteria' => $criteria, // you criteria that should include the "with" part
'sort' => array(
'defaultOrder' => 'part.number DESC',
)
));

like OR operator in Cakephp

I am new to cakephp and I want to add or, and, and like to my existing query.
I want to make a condition like this
WHERE 'Message.user_id = Contact.user_id' AND 'Message.mobileNo LIKE'=>"%Contact.mobileNo" OR LIKE'=>"%Contact.homeNo" OR LIKE'=>"%Contact.workNo"
My query is
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner'
)
)
), false);
$this->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
you can use like below in your existing query.
$this->find('all', array(
'conditions' => array(
'OR' => array(
array(
"Message.mobileNo LIKE" => "%Contact.mobileNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.homeNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.workNo",
)
)
),
'fields' => array('DISTINCT mobileNo')
));
And you can also refer Detail Document for simple search with like
you can use: for "like"
$this->Post->find("all",array('condition'=>array('Author
LIKE'=>"ad%")));
above query will give You the data from table posts where author name starts with "ad".
for "OR"
$this->Post->find("all",array('condition'=>array("OR"=>array('Author
LIKE'=>"ad%",'Author LIKE'=>"bo%"))));
above query will give You the data from table posts where author name starts with "ad" OR "bo"
This blog post can b useful to u as well!