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.
Related
Here is my correct sql query:
SELECT * FROM `messages` WHERE ( (sender_id=3 AND user_id=40) OR (sender_id=40 AND user_id=3)) AND offer_id=1
I want to use this in Cakephp syntax:
$this->Message->find('all',array('conditions'=>array(
'AND'=>array(
'OR'=>array(
'Message.offer_id'=>$offer_id,
'Message.sender_id'=>$sender_id,
'Message.user_id'=>$this->Auth->user('id'),
),
'OR'=>array(
'Message.offer_id'=>$offer_id,
'Message.user_id'=>$sender_id,
'Message.sender_id'=>$this->Auth->user('id')
)
)
),
'recursive'=>2
));
Is there anyone who can help me to figure out the issue. Basically I want to get all the messages whether I sent or received for an particular offer.
You should move $offer_id out of or conditions and move it to and conditions.
Why? Lets look at your first or array:
That conditions will return true if:
sender_id is 3
OR user_id is 40
OR offer_id is 1
So, that condition may return true event if offer_id != 1
That should be written this way (as precisely as possible according to original query):
$query = $this
->Messages
->find('all' , [
'conditions' => [
'or' => [
[
'sender_id' => $sender_id,
'user_id' => $this->Auth->user('id')
], [
'sender_id' => $this->Auth->user('id'),
'user_id' => $sender_id
]
],
'offer_id' => $offer_id,
]
]);
dump($query);
In dump we can see something like this:
"SELECT * FROM messages Messages WHERE (((sender_id = :c0 AND user_id = :c1) OR (sender_id = :c2 AND user_id = :c3)) AND offer_id = :c4)
asterisk in sql query dump for more readability
You have the AND and OR operators reversed.
'OR'=>array(
'AND'=>array(
'Message.offer_id'=>$offer_id,
'Message.sender_id'=>$sender_id,
'Message.user_id'=>$this->Auth->user('id'),
),
'AND'=>array(
'Message.offer_id'=>$offer_id,
'Message.user_id'=>$sender_id,
'Message.sender_id'=>$this->Auth->user('id')
)
)
I have this query:
select count(*) as total_count, sum(total) as total_value, status from invoice group by status;
when I execute it in the database I get the following:
I want to achieve the same using Doctrine2 but the following code only returns one row:
$rsm = new ResultSetMapping();
$rsm->addScalarResult('total_count', 'totalCount');
$rsm->addScalarResult('total_value', 'totalValue');
$rsm->addScalarResult('status', 'status');
$query = $this->getObjectManager()->createNativeQuery('
select count(*) as total_count, sum(total) as total_value, status from invoice group by status', $rsm);
$result = $query->getResult();
return $result;
This is the result of the above:
array (size=1)
0 =>
array (size=3)
'totalCount' => string '1432' (length=1)
'totalValue' => string '55447.80999999999' (length=2)
'status' => string 'paid' (length=4)
You should use:
$result = $query->getScalarResult();
Instead of :
$result = $query->getResult();
Hope this help
Because you are using getResult doctrine will try to fetch all matching rows for you. You need to use Doctrine's getSingleResult if you don't want the result to be an array of arrays.
So for you simply:
$result = $query->getSingleResult();
You can also limit the number of results to throw a QueryException if more or none are returned:
$query->setMaxResults(1);
$phql = 'SELECT id,username,email FROM Users WHERE active = :active: LIMIT :offset:, :limit:';
$users = $this->modelsManager->executeQuery($phql, array('active' => 'Y', 'offset' => 100, 'limit' => 10));
//But Generates the error SQL statement
//SELECT `users`.`id` AS `id`, `users`.`username` AS `username`, `users`.`email` AS `email` FROM `users` WHERE `users`.`active` = 'Y' LIMIT '10' OFFSET '100'
ANY one help me ? just using $this->modelsManager...,whith $phql to bind numeric parameters.
I haven't had the chance to test it out, but from the PHQL, modelsManager, and Query documentation, it seems that you can enforce it by creating a query rather than executing it straight away, and then executing it with the specific types you want:
// Create the query
$phql = 'SELECT id,username,email FROM Users WHERE active = :active: LIMIT :offset:, :limit:';
$query = $this->modelsManager->createQuery($phql);
// Setup the values to bind
$values = array(
'active' => 'Y',
'offset' => 100,
'limit' => 10
);
// Setup the type for each value
$types = array(
'active' => Phalcon\Db\Column::BIND_PARAM_STR,
'offset' => Phalcon\Db\Column::BIND_PARAM_INT,
'limit' => Phalcon\Db\Column::BIND_PARAM_INT
);
// Execute the query
$users = $query->execute($values, $types);
I have a codeigniter query to get a list of questions from my database. I'm using some joins to get the category name and the answer type. Its working good to output everything, but when i try and output the id of the row from the "questions" table, it displays as "2" which is the answer_type_id.
Maybe I'm doing this wrong, i'm fairly new to joins. Any help is appreciated:
function get_questions($category_id) {
$this->db->select('*');
$this->db->from('questions');
$this->db->where('category_id', $category_id);
$this->db->join('categories', 'questions.category_id = categories.id');
$this->db->join('answer_type', 'questions.answer_type_id = answer_type.id');
$this->db->order_by('priority', 'desc');
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'category' => $row->category,
'type' => $row->type,
'question' => $row->question,
'answer' => $row->answer_type,
'priority' => $row->priority,
);
}
return $data;
}
Update===============
I have added "left" for my join type but the problem persists. The question id's should be 2 and 3. But when i print my array returned, they are both 2 (which is the answer_type_id).
Here is the updated code (only left join changed)...
function get_questions($category_id) {
$this->db->select('*');
$this->db->from('questions');
$this->db->where('category_id', $category_id);
$this->db->join('categories', 'questions.category_id = categories.id', 'left');
$this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
$this->db->order_by('priority', 'desc');
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'category' => $row->category,
'type' => $row->type,
'question' => $row->question,
'answer' => $row->answer_type,
'priority' => $row->priority,
);
}
return $data;
}
And here is the output it returns:
Array (
[0] => Array (
[id] => 2
[category] => Herbs
[type] => 2
[question] => What Type of Vehicle do You own?
[answer] => Drop down list [priority] => 0
)
[1] => Array (
[id] => 2
[category] => Herbs
[type] => 3
[question] => What is Your Favorite Herb
[answer] => Drop down list [priority] => 0
)
)
The reason you are not getting the proper ids is you are selecting *.
Use aliases so that give them separate names and you will be access them
as you want like this.
function get_questions($category_id) {
$this->db->select('questions.*');
$this->db->select('answer_type.answer_type_id');
$this->db->select('answer_type.other_column');
$this->db->from('questions');
$this->db->where('category_id', $category_id);
$this->db->join('categories', 'questions.category_id = categories.id', 'left');
$this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
$this->db->order_by('priority', 'desc');
return $this->db->get()->result_array();
}
Also Codeigniter database class provide a method call result_array()
Which is already alternate of the loop you are using. So use it instead of
extra loop code.
Just change it to this:
'id' => $row->category_id
The problem is that the id column is ambiguous. In cases where your selecting columns across tables with columns that have the same name, you can also use the AS keyword to rename a column.
For example:
$this->db->select("questions.*, categories.id AS cat_id, answer_type.id AS ans_id");
By chance are you (maybe) only getting one row as a result, where all the ids match?
You should specify your join type, or be more specific in your query, so that you keep the question.id value.
For this question, you could specify the join as a left join, to keep all the data from questions table and tack on from categories and answer_types where its found.
see CodeIgniter ActiveRecord docs
The questions id needs to be defined as its own name. To pull ALL the data from the other tables, follow up with asterisks for the other tables, like so...
function get_questions($category_id) {
$this->db->select('questions.id as questions_id, questions.*, categories.*, answer_type.*');
$this->db->from('questions');
$this->db->where('category_id', $category_id);
$this->db->join('categories', 'questions.category_id = categories.id', 'left');
$this->db->join('answer_type', 'questions.answer_type_id = answer_type.id', 'left');
$this->db->order_by('priority', 'desc');
$query = $this->db->get();
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->questions_id,
'category' => $row->category,
'type' => $row->type,
'question' => $row->question,
'answer' => $row->answer_type,
'priority' => $row->priority,
);
}
return $data;
}
Use alias for your id columns like the above example.
...
$this->db->select("questions.*, categories.id AS cat_id, answer_type.id AS ans_id");
...
...
Now you get your data because only have one id column.
Here you have an article: http://chrissilich.com/blog/codeigniter-active-record-aliasing-column-names-to-prevent-overwriting-especially-columns-named-id/
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.