Yii2 viaTable 多对多。表没有关系。急于加载
Yii2 viaTable many-to-many. Tables has no relation. Eagre loading
有 2 个 tables 多对多和另一个 Intermediary able
$this->createTable('tweet',[
'id' => Schema::TYPE_PK,
'text' => Schema::TYPE_STRING.' NOT NULL',
'date_written' => Schema::TYPE_STRING.' NOT NULL',
'date_imported' => Schema::TYPE_STRING.' NOT NULL',
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createTable('hashtag',[
'text' => $this->string(),
'PRIMARY KEY(text)'
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createTable('tweet_hashtag', [
'tweet_id' => $this->integer(),
'hashtag_text' => $this->string(),
'PRIMARY KEY(tweet_id, hashtag_text)'
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createIndex('idx-tweet_hashtag-tweet_id', 'tweet_hashtag', 'tweet_id');
$this->createIndex('idx-tweet_hashtag-hashtag_text', 'tweet_hashtag', 'hashtag_text');
$this->addForeignKey('fk-tweet_hashtag-tweet_id', 'tweet_hashtag', 'tweet_id', 'tweet', 'id', 'CASCADE');
$this->addForeignKey('fk-tweet_hashtag-hashtag_text', 'tweet_hashtag', 'hashtag_text', 'hashtag', 'text', 'CASCADE');
Table 推文
public function getTweetHashtags()
{
return $this->hasMany(TweetHashtag::className(), ['tweet_id' => 'id']);
}
public function getHashtagTexts()
{
return $this->hasMany(Hashtag::className(), ['text' => 'hashtag_text'])->viaTable('tweet_hashtag', ['tweet_id' => 'id']);
}
Table tweet_hashtag
public function getHashtagText()
{
return $this->hasOne(Hashtag::className(), ['text' => 'hashtag_text']);
}
public function getTweet()
{
return $this->hasOne(Tweet::className(), ['id' => 'tweet_id']);
}
Table 话题标签
public function getTweetHashtags()
{
return $this->hasMany(TweetHashtag::className(), ['hashtag_text' => 'text']);
}
public function getTweets()
{
return $this->hasMany(Tweet::className(), ['id' => 'tweet_id'])->viaTable('tweet_hashtag', ['hashtag_text' => 'text']);
}
当我尝试
Tweet::find()->with('hashtag')->where(['id' => $tweetID])->all()
有异常
Exception 'yii\base\InvalidParamException' with message 'app\models\Tweet has no relation named "hashtag".'
如何正确地进行预加载。要从话题标签 table 中获取 'text',通过推文 table.
中的 id
您应该使用名称或关系
Tweet::find()->with('tweetHashtags')->where(['id' => $tweetID])->all()
有 2 个 tables 多对多和另一个 Intermediary able
$this->createTable('tweet',[
'id' => Schema::TYPE_PK,
'text' => Schema::TYPE_STRING.' NOT NULL',
'date_written' => Schema::TYPE_STRING.' NOT NULL',
'date_imported' => Schema::TYPE_STRING.' NOT NULL',
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createTable('hashtag',[
'text' => $this->string(),
'PRIMARY KEY(text)'
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createTable('tweet_hashtag', [
'tweet_id' => $this->integer(),
'hashtag_text' => $this->string(),
'PRIMARY KEY(tweet_id, hashtag_text)'
],"ENGINE=InnoDB DEFAULT CHARSET=utf8");
$this->createIndex('idx-tweet_hashtag-tweet_id', 'tweet_hashtag', 'tweet_id');
$this->createIndex('idx-tweet_hashtag-hashtag_text', 'tweet_hashtag', 'hashtag_text');
$this->addForeignKey('fk-tweet_hashtag-tweet_id', 'tweet_hashtag', 'tweet_id', 'tweet', 'id', 'CASCADE');
$this->addForeignKey('fk-tweet_hashtag-hashtag_text', 'tweet_hashtag', 'hashtag_text', 'hashtag', 'text', 'CASCADE');
Table 推文
public function getTweetHashtags()
{
return $this->hasMany(TweetHashtag::className(), ['tweet_id' => 'id']);
}
public function getHashtagTexts()
{
return $this->hasMany(Hashtag::className(), ['text' => 'hashtag_text'])->viaTable('tweet_hashtag', ['tweet_id' => 'id']);
}
Table tweet_hashtag
public function getHashtagText()
{
return $this->hasOne(Hashtag::className(), ['text' => 'hashtag_text']);
}
public function getTweet()
{
return $this->hasOne(Tweet::className(), ['id' => 'tweet_id']);
}
Table 话题标签
public function getTweetHashtags()
{
return $this->hasMany(TweetHashtag::className(), ['hashtag_text' => 'text']);
}
public function getTweets()
{
return $this->hasMany(Tweet::className(), ['id' => 'tweet_id'])->viaTable('tweet_hashtag', ['hashtag_text' => 'text']);
}
当我尝试
Tweet::find()->with('hashtag')->where(['id' => $tweetID])->all()
有异常
Exception 'yii\base\InvalidParamException' with message 'app\models\Tweet has no relation named "hashtag".'
如何正确地进行预加载。要从话题标签 table 中获取 'text',通过推文 table.
中的 id您应该使用名称或关系
Tweet::find()->with('tweetHashtags')->where(['id' => $tweetID])->all()