Yii Active Record - 关系表上的WHERE

时间:2014-02-02 15:49:13

标签: php mysql activerecord yii

我的mysql数据库中有以下表格:

category (id,name, url)
recipe_categories (category_id, recipe_id)
recipes (id, name, description)

我想要实现的SQL语句如下:

SELECT * FROM recipes r 
INNER JOIN recipe_categories cr ON cr.recipe_id = r.id
INNER JOIN category c ON c.id = cr.category_id
WHERE c.url = "google.de"

这两个模型:

<?php
class RecipeCategoryModel extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'recipe_categories';
    }


    public function relations()
    {
        return array(
            'recipes'   => array(self::MANY_MANY ,      'RecipeModel',      'recipe_has_categories(recipe_category_id,recipe_id)'),
        );
    }

    public function getActive() {
        return RecipeCategoryModel::model()->with(array(
            'recipes'=>array(
                'joinType'=>'INNER JOIN',
            ),
        ))->findAll();
    }
}

?>


<?php
class RecipeModel extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'recipes';
    }

    public function relations()
    {
        return array(
            'categories'    => array(self::MANY_MANY ,      'RecipeCategoryModel',      'recipe_has_categories(recipe_id,recipe_category_id)'),
        );
    }

    public function getMainImage() {

    }

}

?>

问题是WHERE-clausel。我如何用ActiveRecord(with(),CDbCriteria,...)实现这一点?

1 个答案:

答案 0 :(得分:1)

在考虑这个问题的答案时,我不得不清理你的db表和模型名称,因为提供的代码在测试时会导致一些错误。但是我已经能够生成你想要的SQL语句 - 但是在使用Yii的ActiveRecord时它会使用它自己的语法和表别名。

我的结果是:

SELECT `t`.`id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`description` AS `t0_c2`, `categories`.`id` AS `t1_c0`, `categories`.`name` AS `t1_c1`, `categories`.`url` AS `t1_c2`
FROM `recipe` `t` 
INNER JOIN `recipe2category` `categories_categories` ON (`t`.`id`=`categories_categories`.`recipe_id`) 
INNER JOIN `category` `categories` ON (`categories`.`id`=`categories_categories`.`category_id`) 
WHERE (categories.url = 'google.de')

要在Yii CActiveRecord调用中获取WHERE语句,您需要在CDbCriteria或FindAll()参数中添加“条件”或“比较”。请参阅http://www.yiiframework.com/doc/api/1.1/CDbCriteria#condition-detailhttp://www.yiiframework.com/doc/api/1.1/CDbCriteria#addCondition-detail

例如:

$Criteria = new CDbCriteria;
$Criteria->with = array(
'categories' => array(
    'joinType' => 'INNER JOIN'
    ),
);
$Criteria->condition = 'categories.url = :url';
$Criteria->params = array(
    ':url' => 'google.de',
);

$Recipes = Recipe::model()->findAll($Criteria);

但是要使代码工作,你需要我清理过的数据库表和模型:

TABLE recipe - id, name, description
TABLE recipe2category - category_id, recipe_id
TABLE category - id, name, url

Model Recipe.php:

<?php
class Recipe extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'recipe';
    }

    public function relations()
    {
        return array(
            'categories'    => array(self::MANY_MANY , 'RecipeCategory', 'recipe2category(recipe_id,category_id)'),
        );
    }
}

?>

Model RecipeCategory.php:

<?php
class RecipeCategory extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'category';
    }

    public function relations()
    {
        return array(
        'recipes'   => array(self::MANY_MANY , 'Recipe', 'recipe2categoy(category_id,recipe_id)'),
        );
    }
}

?>

希望这很有用。