سؤال

It seems that Active Record (Codeigniter) doesn't accept the WEEK parameter and I don't understand why?! Whern i remove '3', my query works properly!

$this->db->select('WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre')
                    ->from($this->table_name)
                    ->where(array(
                        'type_anomalie' => "Dérogation",
                        'YEAR(insere_le)' => $year
                    ))
                    ->group_by('WEEK(insere_le,3)')
                    ->get()
                    ->result_array();

This query, shows the following string when I execute it:

SELECT WEEK(insere_le, `3)` AS semaine, COUNT(*) AS nombre FROM (`aero_anomalie_montage`) WHERE `type_anomalie` = 'Dérogation' AND YEAR(insere_le) = '2014' GROUP BY WEEK(insere_le, `3)`

As you can see it adds an apostrophe before the number 3 and after the parenthesis )

هل كانت مفيدة؟

المحلول

It looks a little confused by your syntax. Why not put them in two different selects (if that's possible...)?

I don't know if this will help, but I found this, it will prevent it from adding the backticks altogether.

$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.

http://ellislab.com/codeigniter/user-guide/database/active_record.html

نصائح أخرى

hmm, if you look at active rec class (system/core/database/DB_active_rec.php) you will find this in function select() :

if (is_string($select))
{
    $select = explode(',', $select);
}

so what you're select is doing is actually exploding your string into an array like this:

array(
    WEEK(insere_le,
    3) AS semaine,
    COUNT(*) AS nombre
);

and then processing this.

This looks like an oversight from the developers of the active record class, and probably won't be solved by not escaping the values...

On the other hand, it actually checks its a string prior to the above.. so could try this:

$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))...

and the same with group_by(array('WEEK(insere_le,3)'))...

so end result would be:

$this->db->select(array('WEEK(insere_le,3) AS semaine', 'COUNT(*) AS nombre'))
                    ->from($this->table_name)
                    ->where(array(
                        'type_anomalie' => "Dérogation",
                        'YEAR(insere_le)' => $year
                    ))
                    ->group_by(array('WEEK(insere_le,3)'))
                    ->get()
                    ->result_array();

you tried this?:

$this->db->select("WEEK(insere_le,3) AS semaine, COUNT(*) AS nombre", FALSE);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top