CI and in the join on - codeigniter

$this->default->join('db D', 'C.col1 = D.col1 AND D.col2 = "MAIN"', 'LEFT');
I am getting 500 error on this join in CI but when i only use
$this->default->join('db D', 'C.col1 = D.col1', 'LEFT');
query is ok.
How to do join in CI with and in the ON part of the join?
FYI
Tried the entire query in MS SQL Server Management Studio and it runs ok.
Putting D.col2 = "MAIN" in where clause also works

What you have done should work but there are two things you could try to see if the outcome is better.
Test1: Which just exchanges where double and single quotes are used.
$this->default->join('db D', "C.col1 = D.col1 AND D.col2 = 'MAIN'", 'LEFT');
Test 2: Which turns off escaping the values and identifiers for the join call
this->default->join('db D', 'C.col1 = D.col1 AND D.col2 = "MAIN"', 'LEFT', FALSE);

Related

i am confused make this query to codeigniter - sum, distinct, join multiple conditions

I have a correct mysql query but I am really confused to convert this to CI.
The result i want is just like this
query result
select sum(distinct(tns.nilai)),tns.nis
from trs_nilai_sikap tns inner join trs_riwayat_nilai_sikap trns
on trns.id_tahun_ajar = 2
and tns.nis = 1800217
and trns.tipe_nilai = 1
and trns.id_riwayat_nilai_sikap = tns.id_riwayat_nilai_sikap
Your query seems quite confusing in inner join.
Still i have tried in CI. Hope you get your answer.
$this->db->select('SUM(distinct(tns.nilai)) AS total_nilai_sikap, tns.nis');
$this->db->from('trs_nilai_sikap tns');
$this->db->join('trs_riwayat_nilai_sikap trns', 'trns.id_riwayat_nilai_sikap = tns.id_riwayat_nilai_sikap', 'inner');
$this->db->where('trns.id_tahun_ajar', 2);
$this->db->where('tns.nis', 1800217);
$this->db->where('trns.tipe_nilai', 1);
$query = $this->db->get();
$query->result_array();

CodeIgniter ActiveRecord join() method wrapping numbers with backticks

This is my active record code in CodeIgniter:
$this->db->...
$this->db->join('post_likes', 'post_likes.user_id="'.$this->db->escape($online_user).'" AND post_likes.post_id=post.id', 'left');
And this is how it is interpreted:
LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
it gives the error:
`user_id`="`3"`
How to write a direct number in active record?
Update:
removing escape
to test it on your computer you dont need to have a database. Just trying this code shows the error:
$this->db->select('*')
->from('mytable')
->join('post_likes', 'post_likes.user_id="3" AND post_likes.post_id=post.id', 'left');
$query=$this->db->get('');
var_dump($this->db->last_query());
exit(0);
result:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` AND post_likes.post_id=post.id' at line 3
SELECT * FROM (`mytable`) LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id
You SHOULD not use the double quotes in SQL query:
$this->db->join('post_likes', "post_likes.user_id = $online_user AND post_likes.post_id=post.id", 'left');
Update:
This is a bug in the current CI stable version (fixed in v3.0-DEV), CI ActiveRecord methods (which doesn't implement really ActiveRecord) are prepared for simple usages.
I fixed this issue before by hacking the core files (by adding a parameter to join method to disable _protect_identifires).
There we go:
In system/database/DB_active_rec.php line #310, add $escape as 4th parameter:
public function join($table, $cond, $type = '', $escape = TRUE)
And change $match[3] = ... to:
if ($escape === TRUE)
{
$match[3] = $this->_protect_identifiers($match[3]);
}
So, you can use join($table, $cond, $type = '', $escape = FALSE) to disable escaping.
In addition, setting _protect_identifires globally to FALSE is not in a correct direction.
the only option remains is using custom query():
$sql = "SELECT * FROM some_table WHERE id = ?"
$this->db->query($sql, array(3));
Try this
$this->db->join('post_likes', "post_likes.user_id=$the_userid AND
post_likes.post_id=post.id", 'left');
or
$this->db->join('post_likes', 'post_likes.user_id="'.$the_userid.'" AND
post_likes.post_id=post.id', 'left');
Update:
Define
$db['default']['_protect_identifiers']= FALSE;
in "application/config/database.php" at the end.
Simple solution would be to temporarily set the protect_identifiers off before join query, like so:
$this->db->_protect_identifiers = false;
After making join query you could set it back to true
Works for me in CodeIgniter version 2.1.2
try this one
$this->db->join('post_likes', 'post_likes.user_id="{$online_user}" AND post_likes.post_id=post.id', 'left');
please let me know if you face any problem.
Dont use $this->db->escape
$this->db->join('post_likes', 'post_likes.user_id="'.$online_user.'" AND post_likes.post_id=post.id', 'left');

writing the query in codeigniter giving a different result than required

I am working on codeigniter and I am writing the following query:
$this->db->select('*');
$this->db->from('tbl_profile');
$this->db->join('tbl_msg', 'tbl_msg.msg_sender_id = tbl_profile.profile_id');
$this->db->order_by("msg_id", "desc");
$query = $this->db->get('', $config['per_page'], $this->uri->segment(3));
$data['records'] = $query->result_array();
Correspondingly I am getting the following result:
SELECT (*) FROM tbl_profile
JOIN tbl_msg ON tbl_msg.msg_sender_id = tbl_profile.profile_id
Which is returninng a wrong result as I want the result corresponding to the following query:
select * from tbl_profile as A
join (select * from tbl_msg) as B on A.profile_id = B.msg_sender_id
Please help
First of all, you missing the order by clause, but I assum, you mean other differences.
If you want that, you can use this query, what will gave you back the exact code:
$this->db->select('*', FALSE);
$this->db->from('tbl_profile as A');
$this->db->join('( select * from tbl_msg ) as B', 'A.msg_sender_id = B.profile_id');
$this->db->order_by("msg_id", "desc");
$query = $this->db->get('', $config['per_page'], $this->uri->segment(3));
$data['records'] = $query->result_array();
From codeigniter user manual:
$this->db->select()
accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

Doctrine 2.0 native query without mapping

I am writing a tiny little migration script and i am only trying to update one attribute of one element.
The Result i need has no Representation in the local Environment, so what i would need is a very simple SQL (here it is Oracle) handler that i can iterate over and get an array returned.
Is that possible with doctrine?
i.e. i would want to do this:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->execute($query)->iterate();
foreach ($iterator as $array) {
// do something with an associative array
}
UPDATE / SOLUTION:
With the Hint from Corbin i came up with this Solution which works pretty fine:
$query = "SELECT t2.status FROM t2 LEFT JOIN t1 ON t1.id = t2.foreinkey";
$iterator = $connection->query($query);
while (is_object($iterator) AND ($array = $iterator->fetch()) !== FALSE) {
// do something with an associative array
}
https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#native-sql
If you want to do any mapping.
Another option would be to get the connection object from EntityManager::getConnection and operate on it.
It returns a Doctrine\DBAL\Connection which you should be able to work with. It has the typical fetchColumn fetchArray fetchAssoc so on.

Symfony Doctrine_Query DELETE with INNER JOIN

I am using symfony+doctrine, and I want to perform a delete query with a join. See below for my code I am currently using that works fine.
$sql = 'DELETE a
FROM a
INNER JOIN b ON a.b_id = b.id
WHERE b.c_id = :c_id';
$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$stmt = $pdo->prepare($sql);
$params = array('c_id' => $c_id);
$stmt->execute($params);
Anyone know how I can do this using:
Doctrine_Core::getTable('a')
Or
Doctrine_Query::create()->delete()->from('a')
I have had no luck with either.
I just don't really want to use raw SQL in my app.
Something like this should do it
Doctrine_Query::create()
->delete('a a')
->innerJoin('a.b b')
->where('b.c_id = ?', $c_id)
->execute()

Resources