Yii2 - 无法解析 hasMany() 的结果
Yii2 - Unable to parse result of hasMany()
我卡在了一些非常基本的事情上,无法继续前进。我有这样的表 interpret
和 interpret_member
(这里只是重要数据):
$this->createTable('interpret', [
'name' => $this->string(255)->notNull(),
'PRIMARY KEY (name)'
]);
$this->createTable('interpret_member', [
'name' => $this->string(255)->notNull(),
'interpret' => $this->string(255)->notNull(),
'PRIMARY KEY (name, interpret)'
]);
$this->createIndex(
'idx-interpret_member',
'interpret_member',
'interpret'
);
$this->addForeignKey('fk-interpret_member', 'interpret_member', 'interpret', 'interpret', 'name', 'CASCADE', 'CASCADE');
然后我有gii生成的两个表的模型,所以我在Interpret model
中也有方法getInterpretMembers
。首先,我尝试根据 this manual, which gives me an error Trying to get property 'name' of non-object
. So I searched here and found thread. Following that I changed my foreach ($members as $member)
to foreach ($members->InterpretMember as $member)
. That gave me error Getting unknown property: yii\db\ActiveQuery::InterpretMember
. The last hope was 线程解析其输出,但因错误 Trying to get property 'name' of non-object
而结束。上次尝试的代码是:
public function getInterpretMembers()
{
$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name']);
foreach ($members as $member) {
$member = ArrayHelper::toArray($member, [
'app\models\user\InterpretMember' => [
'name',
'interpret',
'position'
]
]);
echo "Member = {$member->name}<br/>";
}
die();
}
我在所有尝试中都使用了这段代码,它只是根据提到的线程和手册进行了修改。
问题很简单
$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name']);
hasMany函数returns一个ActiveQuery实例
而实际上不是来自数据库的数据,
所以为了得到你需要的数据
从 ActiveQuery 实例
调用 one() 或 all() 函数
$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name'])->all();
恭喜,现在你将拥有数据
您误解了关系和 ActiveRecord
在 Yii2 中的工作方式。
您的模型应该是什么样子。
解释模型
class Interpret extends \yii\db\ActiveRecord
{
/**
* Method that defines the relation. It should
* return instance of \yii\db\ActiveQuery
*/
public function getInterpretMembers()
{
return $this->hasMany(InterpretMember::class, ['interpret' => 'name']);
}
//... other methods
}
解释成员模型
class InterpretMember extends \yii\db\ActiveRecord
{
public function getInterpret()
{
return $this->hasOne(Interpret::class, ['name' => 'interpret'])
}
}
这将在您的 Interpret
class 中创建 属性 interpretMembers
并在您的 InterpretMember
[=] 中创建 属性 interpret
33=].
要在您的控制器中使用它,您可以执行以下操作:
class InterpretController extends \yii\web\Controller
{
public function actionView()
{
//find interpret with name InterpretName
$interpret = Interpret::findOne('InterpretName');
//output all members
foreach ($interpret->interpretMembers as $member) {
echo "Member = {$member->name}<br/>";
}
die();
}
}
或者,如果您想在 Interpret class 中使用成员:
class Interpret extends \yii\db\ActiveRecord
{
/**
* Method that defines the relation. It should
* return instance of \yii\db\ActiveQuery
*/
public function getInterpretMembers()
{
return $this->hasMany(InterpretMember::class, ['interpret' => 'name']);
}
//... other methods
public function doSomethingWithMembers()
{
foreach ($this->interpretMembers as $member) {
echo "Member = {$member->name}<br/>";
}
die();
}
}
我卡在了一些非常基本的事情上,无法继续前进。我有这样的表 interpret
和 interpret_member
(这里只是重要数据):
$this->createTable('interpret', [
'name' => $this->string(255)->notNull(),
'PRIMARY KEY (name)'
]);
$this->createTable('interpret_member', [
'name' => $this->string(255)->notNull(),
'interpret' => $this->string(255)->notNull(),
'PRIMARY KEY (name, interpret)'
]);
$this->createIndex(
'idx-interpret_member',
'interpret_member',
'interpret'
);
$this->addForeignKey('fk-interpret_member', 'interpret_member', 'interpret', 'interpret', 'name', 'CASCADE', 'CASCADE');
然后我有gii生成的两个表的模型,所以我在Interpret model
中也有方法getInterpretMembers
。首先,我尝试根据 this manual, which gives me an error Trying to get property 'name' of non-object
. So I searched here and found foreach ($members as $member)
to foreach ($members->InterpretMember as $member)
. That gave me error Getting unknown property: yii\db\ActiveQuery::InterpretMember
. The last hope was Trying to get property 'name' of non-object
而结束。上次尝试的代码是:
public function getInterpretMembers()
{
$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name']);
foreach ($members as $member) {
$member = ArrayHelper::toArray($member, [
'app\models\user\InterpretMember' => [
'name',
'interpret',
'position'
]
]);
echo "Member = {$member->name}<br/>";
}
die();
}
我在所有尝试中都使用了这段代码,它只是根据提到的线程和手册进行了修改。
问题很简单
$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name']);
hasMany函数returns一个ActiveQuery实例 而实际上不是来自数据库的数据, 所以为了得到你需要的数据 从 ActiveQuery 实例
调用 one() 或 all() 函数$members = $this->hasMany(InterpretMember::className(), ['interpret' => 'name'])->all();
恭喜,现在你将拥有数据
您误解了关系和 ActiveRecord
在 Yii2 中的工作方式。
您的模型应该是什么样子。
解释模型
class Interpret extends \yii\db\ActiveRecord
{
/**
* Method that defines the relation. It should
* return instance of \yii\db\ActiveQuery
*/
public function getInterpretMembers()
{
return $this->hasMany(InterpretMember::class, ['interpret' => 'name']);
}
//... other methods
}
解释成员模型
class InterpretMember extends \yii\db\ActiveRecord
{
public function getInterpret()
{
return $this->hasOne(Interpret::class, ['name' => 'interpret'])
}
}
这将在您的 Interpret
class 中创建 属性 interpretMembers
并在您的 InterpretMember
[=] 中创建 属性 interpret
33=].
要在您的控制器中使用它,您可以执行以下操作:
class InterpretController extends \yii\web\Controller
{
public function actionView()
{
//find interpret with name InterpretName
$interpret = Interpret::findOne('InterpretName');
//output all members
foreach ($interpret->interpretMembers as $member) {
echo "Member = {$member->name}<br/>";
}
die();
}
}
或者,如果您想在 Interpret class 中使用成员:
class Interpret extends \yii\db\ActiveRecord
{
/**
* Method that defines the relation. It should
* return instance of \yii\db\ActiveQuery
*/
public function getInterpretMembers()
{
return $this->hasMany(InterpretMember::class, ['interpret' => 'name']);
}
//... other methods
public function doSomethingWithMembers()
{
foreach ($this->interpretMembers as $member) {
echo "Member = {$member->name}<br/>";
}
die();
}
}