AND where queries in Codeigniter - php

I am trying to get my head around this. I have a query as such:
$this->db->select("*");
$where = array(
'category_id' => $category_id,
'active' => 1
);
$this->db->where($where);
$blogs_query= $this->db->get('blog');
I was hoping that would retrieve all the active blogs which matches the category_id I supplied. However it is retrieving all the blogs that matches the category and all the blogs that are active and do not necessarily matches the category.
Help please.

$this->db-query("SELECT * FROM blog WHERE category_id = ? AND active = ?",array($category_id,1));

You can try this simple query :
$where = array(
'category_id' => $category_id,
'active' => '1'
);
$blogs_query= $this->db->select("*")
->from('blog')
->where($where)
->get();
Here the main change is 'active'=> '1' instead of 'active' => 1
It should work, I haven't tested it.

Related

How to write the AND OR condition codeigniter

I have written in one SQL query and it's working fine. How do I write this in codeigniter?
$sql = "SELECT *
FROM single_message
WHERE
( school_id='$schoolId' AND
classId='$classId' AND
sectionId='$sectionId' AND
sender_id='$senderId' AND
receiver_id ='$receiverId'
) OR
( chool_id='$schoolId' AND
classId='$classId' AND
sectionId='$sectionId' AND
sender_id='$receiverId' AND
receiver_id ='$senderId'
)
ORDER BY messageId DESC"`;
This is what I've tried:
$condition = array(
'school_id' => $schoolId,
'classId' => $classId,
'sectionId' => $sectionId,
'sender_id' => $senderId,
'receiver_id' => $receiverId
);
$this->db->select('*');
$this->db->where($condition);
return $this->db->get('single_message')->result_array();
As per your SQL query:
$sql = "SELECT * FROM single_message WHERE (school_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$senderId' AND receiver_id ='$receiverId') OR (chool_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$receiverId' AND receiver_id ='$senderId') ORDER BY messageId DESC";
There are two ways to write queries. I will explain both way one by one:
Solution 1st:
You can simply just put your condition in where clause like explained below->
$condition = "(school_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$senderId' AND receiver_id ='$receiverId') OR (chool_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$receiverId' AND receiver_id ='$senderId')";
$this->db->select('*');
$this->db->where($condition);
return $this->db->get('single_message')->result_array();
Here you can see that I have passes complete where condition in a string format. This is the fisrt solution. Another method to write this query is.
Solution 2nd:
Query grouping-> It allows you to create groups of WHERE clauses by enclosing them in parentheses. So query would be like:
$condition['AND'] = array(
'school_id' => $schoolId,
'classId' => $classId,
'sectionId' => $sectionId,
'sender_id' => $senderId,
'receiver_id' => $receiverId
);
$condition['OR'] = array(
'school_id' => $schoolId,
'classId' => $classId,
'sectionId' => $sectionId,
'sender_id' => $receiverId,
'receiver_id' => $senderId
);
$this->db->select('*');
// Starts first group
$this->db->group_start();
// AND condition placed in below line
$this->db->where($condition['AND']);
// First group ends here
$this->db->group_end();
// Another group has been started here for OR clause
$this->db->or_group_start();
// Here we placed our OR codition
$this->db->where($condition['OR']);
// Second group ends here
$this->db->group_end();
return $this->db->get('single_message')->result_array();
It will produce exact result you needed. Let me know if you have any query. For more details you can read Query Builder explaination here: Query Grouping
Passing second parameters as null and third parameter as false will result is not escaping your query.
Try this code:
$this->db->select('*');
$this->db->where("school_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$senderId' AND receiver_id ='$receiverId') OR (chool_id='$schoolId' AND classId='$classId' AND sectionId='$sectionId' AND sender_id='$receiverId' AND receiver_id ='$senderId'", null, false);
$this->db->order_by("messageId", "desc");
$this->db->get();
Here is Where reference from codeigniter documentation.

data escaping remove for specific filed in cakephp

I am using subquery for id field.
$db = $this->AccountRequest->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('MAX(id)'),
'table' => $db->fullTableName($this->AccountRequest),
'alias' => 'MaxRecord',
'limit' => null,
'offset' => null,
'order' => null,
'group' => array("user_id")
),
$this->AccountRequest
);
$searching_parameters = array(
#"AccountRequest.id IN " => "(SELECT MAX( id ) FROM `account_requests` GROUP BY user_id)"
"AccountRequest.id IN " => "(".$subQuery.")"
);
$this->Paginator->settings = array(
#'fields' => array('AccountRequest.*'),
'conditions' => $searching_parameters,
'limit' => $limit,
'page' => $page_number,
#'group' => array("AccountRequest.user_id"),
'order' => array(
'AccountRequest.id' => 'DESC'
)
);
$data = $this->Paginator->paginate('AccountRequest');
This structure is producing a query is:
SELECT
`AccountRequest`.`id`,
`AccountRequest`.`user_id`,
`AccountRequest`.`email`,
`AccountRequest`.`emailchange`,
`AccountRequest`.`email_previously_changed`,
`AccountRequest`.`first_name`,
`AccountRequest`.`first_namechange`,
`AccountRequest`.`f_name_previously_changed`,
`AccountRequest`.`last_name`,
`AccountRequest`.`last_namechange`,
`AccountRequest`.`l_name_previously_changed`,
`AccountRequest`.`reason`,
`AccountRequest`.`status`,
`AccountRequest`.`created`,
`AccountRequest`.`modified`
FROM
`syonserv_meetauto`.`account_requests` AS `AccountRequest`
WHERE
`AccountRequest`.`id` IN '(SELECT MAX(id) FROM `syonserv_meetauto`.`account_requests` AS `MaxRecord` WHERE 1 = 1 GROUP BY user_id)'
ORDER BY
`AccountRequest`.`id` DESC
LIMIT 25
In the subquery, its add an extra single quote so it's producing an error.
So, How can I remove these single quotes from this subquery?
Thanks
What are you trying to achieve with the sub query?
The MAX(id) just means it will pull the id with the largest value AKA the most recent insert. The sub query is completely redundant when you can just ORDER BY id DESC.
using MAX() will return only one record, if this is what you want to achieve you can replicate by adding LIMIT 1
If the sub query is just an example and is meant to be from another table I would just run the query that gets the most recent id before running the main query. Getting the last inserted id in a separate query is very quick and I cant see much of a performance loss. I think it will result in cleaner code that`s easier to follow to.
edit 1: From the comments it sounds like all your trying to get is a particular users latest account_requests.
You dont need the sub query at all. My query below will get the most recent account record for the user id you choose.
$this->Paginator->settings = array(
'fields' => array('AccountRequest.*'),
'conditions' => array(
'AccountRequest.user_id' => $userID // you need to set the $userID
)
'page' => $page_number,
'order' => array(
'AccountRequest.id DESC' //shows most recent first
),
'limit' => 1 // set however many you want the maximum to be
);
The other thing you cold be meaning is to get multiple entries from multiple users and display them in order of user first and then the order of recent to old for that user. MYSQL lets you order by more than one field, in that case try:
$this->Paginator->settings = array(
'conditions' => array(
'AccountRequest.user_id' => $userID // you need to set the $userID
)
'page' => $page_number,
'order' => array(
'AccountRequest.user_id', //order by the users first
'AccountRequest.id DESC' //then order there requests by recent to old
)
);
If the example data you have added into the question is irrelevant and you are only concerned about how to do nested subqueries it has already been answered here
CakePHP nesting two select queries
However I still think based on the data in the question you can avoid using a nested query.

How to order joined results in Phalcon Framework

Let's pretend I'm working on a magazine, where a Category (like "sport", "art" an so on) can contain several Articles. Therefore I want to extract all articles for a specific category. In Phalcon I usually do:
$category = \Models\Category::findFirst(array(
'conditions' => 'id = ?1',
'bind' => array(1 => $id)
));
Then:
foreach ($category->Article as $article) {
// do something with $article
}
It works great, but I would like to sort those Articles - say - date wise, ascending. How could I accomplish that?
You should use get prefix in your for..loop statement, so your code should like this :
foreach ($category->getArticle(array('order' => 'date DESC')) as $article) {
// do something with $article
}
The main Docs explains more examples.
Try it & get your results.
M2sh answer is all important and actual, I just will post a secondary way, just using model designed for articles:
$page = 5; // eg. 5th page of results
$limit = 100; // eg. 100 results per page
$articles = \Models\Articles::find(array(
'conditions' => 'category_id = :cid:',
'bind' => array(
'cid' => $id
),
'order' => 'date DESC, id DESC',
'limit' => $limit,
'offset' => $page * $limit
));
It's possible to use such set of parameters in M2sh way aswell.
One more for completeness. I will borrow from yergo to illustrate the differences:
$page = 5;
$limit = 100;
$articles = \Models\Articles::query()
->where('category_id= :cid:', array('cid' => $id)
->orderBy('date DESC, id DESC')
->limit($limit)
->offset($page * $limit)
->query();

Include single item from related table

I have a table called items and a table called item_pics.
item_pics has an item_id, file_name and a rank field (among others).
What I'm looking for is for each item my index page's $items array to contain the file_name from the item_pics matching the item's item_id with the lowest rank. So I can access like (or something like) this in my Items/index.ctp:
foreach ($items as $item):
$img = $item['Item']['ItemPic']['file_name'];
...
I'm pretty new to CakePHP, this is my first project. I thought that this within the Item model would cause item_pics data to be pulled (although I figured all related item_pics for each item would get pulled rather than just the one with the lowest rank):
public $hasMany = array(
'ItemPic' => array(
'className' => 'ItemPic',
'foreignKey' => 'item_id',
'dependent' => false
)
}
but I can see that no item_pics data is loaded (at the bottom of items/index):
SELECT `Item`.`id`, `Item`.`title`, `Item`.`description`, `Item`.`created`, `Item`.`modified`, `Item`.`type`, `Project`.`id`, `Project`.`item_id`, `Project`.`title`, `Project`.`description`, `Project`.`rank`, `Project`.`created`, `Project`.`modified`
FROM `laurensabc`.`items` AS `Item`
LEFT JOIN `laurensabc`.`projects`
AS `Project`
ON (`Project`.`item_id` = `Item`.`id`)
WHERE `Item`.`type` IN (1, 2)
LIMIT 20
also, while I would like projects to be joined in the view pages, I don't really need them in the index page.
I've done some searching and haven't been able to find exactly what I'm looking for. I suppose I could do a query within the index view item loop, but I'm trying to make sure I do things the right way... the CakePHP way. I assume I need to change something about my model relationships but I haven't had any luck.
CakePHP - Associations - HasMany, this makes it seem like I could order by rank and limit 1. But this didn't work... and even if it did, I wouldn't want that to affect the view pages but rather just the index page.
My Controller looks like this:
public function index($type = null) {
$this->Item->recursive = 0;
$conditions = array();
if ($type == "sale") {
$conditions = array(
"Item.type" => array(self::FOR_SALE, self::FOR_SALE_OR_RENT)
);
} else if ($type == "rent" ) {
$conditions = array(
"Item.type" => array(self::FOR_RENT, self::FOR_SALE_OR_RENT)
);
} else {
$conditions = array("Item.type !=" => self::HIDDEN);
}
$paginated = $this->Paginator->paginate($conditions);
debug($paginated);
$this->set('items', $paginated);
$this->set('title', ($type == null ? "Items for Sale or Rent" : "Items for " . ucwords($type)));
}
I have also tried this on my controller, but it doesn't seem to do anything either:
$this->paginate = array(
'conditions' => $conditions,
'joins' => array(
array(
'alias' => 'ItemPic',
'table' => 'item_pics',
'type' => 'left',
'conditions' => array('ItemPic.item_id' => 'Item.id'),
'order' => array('ItemPic.rank' => 'asc'),
'limit' => 1
)
)
);
$paginated = $this->paginate($this->Item);
First, set containable behavior in AppModel (or if you don't want it on each model, put it on Item model):
public $actsAs = array('Containable');
Then, on your find query:
$items = $this->Item->find('all', array(
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
));
Then the result array you can access it like:
foreach ($items as $item):
$img = $item['ItemPic']['file_name'];
Edit: Then you should put it on the paginate query:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array(
'ItemPic' => array(
'fields' => array('file_name'),
'order' => 'rank',
'limit' => 1
)
)
);
In this case, I would probably order by rank and limit 1 as you said, and make that a dynamic association just for the index page (See http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly). So use $this->Item->bindModel(array('hasMany' => array('ItemPic' => $options))); (which I believe should replace your current settings for HasMany ItemPic, but you may have to unbindmodel first)
Associations created through bindModel will go through for the next query only, then it'll revert to your normal settings, unless you specifically set an option to keep using the new association.
As for why it's not getting ItemPics with Items, or why trying to order by rank and limit 1 didn't work for you, I can't really say without seeing more of your code.

CakePHP: How to retrieve data by multiple condition

Please help me to retrieve data from a table by multiple condition in Cakephp
I have one table name: article; I have tried to retrieve data with the code below
I want to get specific id as given in the parameter; article_price > 0 and article_status > 1
public function getArticle($artID = ''){
return $this->find('all', array(
'condition' => array(
'article_id =' => $artID,
'article_price' => '> 0',
'article_status = ' => '1'),
'order' => 'article_id DESC'
));
}
// the out put was selected all data without condition that I want.
What was the problem with my code?
What I found out is I print: echo $this->element ('sql_dump'); and I got the following sql statement:
SELECT `article`.`article_id`, `article`.`name`, `article`.`article_price`, `article`.`article_status` FROM `db_1stcakephp`.`article` AS `article` WHERE 1 = 1 ORDER BY `article_id` DESC
Please help me.
Thank!
If your model name is Article:
public function getArticle($art_id) {
return $this->find('first', array(
'conditions' => array(
'Article.article_id' => $art_id,
'Article.article_price >' => 0,
'Article.article_status >' => 1,
),
));
}
Using 'Model.field' syntax is optional, until your models have relationship and have the same names - for example Article.status and Author.status.
Moving comparison sign into array's key part allows you to do:
'Article.price >' => $minPrice,
'Article.price <=' => $maxPrice,
And I didn't really notice typo in 'conditions'.

Categories