0

I'm trying to get the time range on my yii app.

This is my model :

public function timeRange($attribute, $params) {

        $criteria = new CDbCriteria;

        $criteria->addCondition(
                "WHERE ('jam_keluar' > '{$this->jam_masuk}' OR 'jam_masuk' < '{$this->jam_keluar}')".
                "OR ('jam_masuk' < '{$this->jam_keluar}' AND 'jam_keluar' > '{$this->jam_keluar}')"
            );

        $record = self::model()->exists($criteria);
        if(!empty($record)) {
            $this->addError('jd_id', 'Item already exists within range.');
            return false;
        }

    }

But i'm getting the following error :

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE ('jam_keluar' > 03:00:00' OR 'jam_masuk' < '12:00:00')OR ('jam_masuk' < '1' at line 1. The SQL statement executed was: SELECT 1 FROM `a_jadwal` `t` WHERE WHERE ('jam_keluar' > 03:00:00' OR 'jam_masuk' < '12:00:00')OR ('jam_masuk' < '12:00:00' AND 'jam_keluar' > '12:00:00') LIMIT 1 

Anyone?

3 Answers 3

3

You need to remove 'WHERE' from within your addCondition because addCondition will handle that for you. Also escape all your values with quotes but do not put quotes around your column names.

You should use CDbCriteria::compare or at least use addCondition with params for this to avoid SQL injection:

// Use the solution with CDbCriteria::addCondition
$criteria->addCondition(
    "WHERE (jam_keluar > :jam_masuk OR jam_masuk < :jam_keluar)".
    "OR (jam_masuk < :jam_keluar AND jam_keluar > :jam_keluar)",
    array(
        ':jam_masuk' => $this->jam_masuk,
        ':jam_keluar' => $this->jam_keluar
    )
);

// Or use the solution with CDbCriteria::compare
$criteria->compare('jam_keluar', '>'.$this->jam_masuk, false);
$criteria->compare('jam_masuk', '<'.$this->jam_keluar, false, 'OR');
$criteria->compare('jam_masuk', '<'.$this->jam_keluar, false, 'OR');
$criteria->compare('jam_keluar', '>'.$this->jam_keluar, false);
Sign up to request clarification or add additional context in comments.

Comments

0

There's a single quote missing.

$criteria->addCondition(
            "WHERE ('jam_keluar' > '{$this->jam_masuk}' OR 'jam_masuk' < '{$this->jam_keluar}')".
            "OR ('jam_masuk' < '{$this->jam_keluar}' AND 'jam_keluar' > '{$this->jam_keluar}')"
        );

Here:

"WHERE ('jam_keluar' > {$this->jam_masuk}' ...

Comments

0

public function timeRange($attribute, $params) {

$criteria = new CDbCriteria;

$criteria->addCondition(
        "WHERE ('jam_keluar' > '{$this->jam_masuk}' OR 'jam_masuk' < '{$this->jam_keluar}')".
        "OR ('jam_masuk' < '{$this->jam_keluar}' AND 'jam_keluar' > '{$this->jam_keluar}')"
    );

$record = self::model()->exists($criteria);
if(!empty($record)) {
    $this->addError('jd_id', 'Item already exists within range.');
    return false;
}
else {
    return true;
}

}

you have missed ' inside query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.