如何正确设置yii模型关系,正在使用';t';参考默认表是个好主意

如何正确设置yii模型关系,正在使用';t';参考默认表是个好主意,yii,Yii,在模型关系中使用“t”可能会导致错误 "Column not found: 1054 Unknown column 't.etc' in 'on clause'." 在CActiveRecord模型关系中使用“t”来引用当前表。 在使用、findAll、cactivedaptaprovider等时,我经常无意中使用它 有时它有效有时它并不取决于您执行的模型以及从何处执行。 我尝试使用tableAlias,但它不起作用。一定有个简单的方法 我怎样才能建立我的模型和它的关系,这样的关系是稳定的 而

在模型关系中使用“t”可能会导致错误

"Column not found: 1054 Unknown column 't.etc' in 'on clause'."
在CActiveRecord模型关系中使用“t”来引用当前表。 在使用、findAll、cactivedaptaprovider等时,我经常无意中使用它

有时它有效有时它并不取决于您执行的模型以及从何处执行。 我尝试使用tableAlias,但它不起作用。一定有个简单的方法

我怎样才能建立我的模型和它的关系,这样的关系是稳定的 而且总是有效

下面是两个类的例子来说明这个问题

class Order extends CActiveRecord
{
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'author'=>array(self::BELONGS_TO, 'User', 'user_id'),
            'shopproduct'=>array(self::BELONGS_TO, 'ShopProduct', 'product_id',
                'with'=>array(
                    'tagsrelations',
                ),
            ),
        );
    }
}


class ShopProduct extends CActiveRecord
{
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'author'=>array(self::BELONGS_TO, 'User', 'user_id'),
            'tagsrelations'=>array(self::HAS_MANY, 'TagsRelations','', 
                'on'=>' tagsrelations.tbl_uuid = t.uuid ',
                'with'=>'tag',
                'order'=>'tag.name ASC',
            ),
        );
    }
}


// works
$model=Order::model()->with('shopproduct')->findAll();

// doesn't work
// "Column not found: 1054 Unknown column 't.uuid' in 'on clause'. The SQL statement executed was"
$model=ShopProduct::model()->with('tagsrelations')->findAll();
也许有人能以一种理解的方式解释为什么这不起作用。 如何一劳永逸地解决这个问题。属于正常工作的关系。
什么是建立一个始终有效的多人关系的最佳方法

您需要的
t
仅用于加入还是用于某些特定目的?因为对于join来说,这不是正确的方式。应该这样做:

'tagsrelations'=>array(self::HAS_MANY, 'TagsRelations', array('tbl_uuid'=>'uuid')),

当然,如果需要的话,也可以添加条件。但是连接应该这样做。

谢谢。我根据您的新结构更改了型号。现在看来这是可行的。使用这样的数组()直接连接pk/fk等是一种好方法。似乎在Yii,许多关系仍然存在问题。我还是不能完全弄明白。但现在这是可行的。我不认为这是有问题的,你只需要知道如何定义它,但是你没有任何问题。你是对的。我发现了这个。我大部分时间都在使用即时加载。通过在控制器中使用loadModel。然后使用User::model()->with('profile,gallery,posts')->findAll();像那样。但是如果我删除一个关系(有很多),例如posts关系。然后在视图中自动使用延迟加载方法。就像视图中的$userModel->posts一样。然后我经常会犯一些奇怪的错误。我不知道如何使急切加载和懒惰加载都能很好地工作。