Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8425439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:16:15+00:00 2026-06-10T04:16:15+00:00

I have a problem with Yii’s CDBCrtieria builder. I am trying to make a

  • 0

I have a problem with Yii’s CDBCrtieria builder. I am trying to make a rather complex query while using the escaping and safe functions provided by PDO.

Here is the query I am basically trying to build:

SELECT * FROM tbl_audit_log 
    WHERE (model_id = 1 AND model = "Title") OR 
          (model_id = 1 AND model = "Product") //etc

This is being built dynamically in PHP like:

$model_ids = array(array($model->id, 'Title'));
foreach($model->products as $id => $product){
    $model_ids[][] = $product->id;
    $model_ids[][] = "Product";
}

So I don’t know the values of the WHERE before I build the query. I must find an easy way to build:

    WHERE (model_id = 1 AND model = "Title") OR 
          (model_id = 1 AND model = "Product") //etc

Dynamically.

I have looked through the documentation but the closest thing I see is addCondition which would require complex coding to get working properly.

Does Yii provide any easy way of achieving this without having to deal with writing complex code to name my params etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:16:17+00:00Added an answer on June 10, 2026 at 4:16 am

    It is indeed a bit more complex, but here’s a working solution:

    $criteria = new CDbCriteria();
    $param_id = 0;
    // $model_ids is the one you built in your original code
    foreach( $model_ids as $id_pair ) {
        $criteria->addCondition( '( model_id = :id' . $param_id . ' AND model = :model' . $param_id . ' )', 'OR' );
        $criteria->params[ ':id' . $param_id ] = $id_pair[0];
        $criteria->params[ ':model' . $param_id ] = $id_pair[1];
        $param_id++;
    }
    

    This will generate custom identifiers for each of your parameters so they will all be validated. Then you can just use $criteria in your query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with my query on C, I’m using the oci8 driver. This
I'm using Yii's zii.widgets.jui.CJuiDatePicker and have the following problem: It works well when inserting
I have a problem with captcha in my YII project. I included a captcha
I am working on creating website with Yii framework and I have a problem
I have problem while loading data into html select when users press or click
I have problem with Uploadify: I log in the project using: FormsAuthentication.SetAuthCookie(myName, false); Then,
I have problem while sending messages from android to PC. Messages are send when
Yii beginner here. I am facing some problem with how to query the database
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have this small problem with Yii's static pages. I can't translate static pages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.