Hi, I’m Having this problem that I couldn’t find a solution after the data save to my table I get this error message
“Object of class app\models\ChnMember could not be converted to string” for 3 days I couldn’t find how to solve it, the data are saved to my table but All i get is that error message:
my app\models\ChnMember:
class ChnMember extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'chn_member';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id', 'channel_id', 'chnmembertype_id'], 'required'],
[['user_id', 'channel_id'], 'integer'],
[['member_type', 'member_note'], 'string', 'max' => 100],
[['chnmembertype_id'], 'string', 'max' => 20],
[['user_id', 'channel_id', 'chnmembertype_id'], 'unique', 'targetAttribute' => ['user_id', 'channel_id', 'chnmembertype_id']],
[['channel_id'], 'exist', 'skipOnError' => true, 'targetClass' => Channel::className(), 'targetAttribute' => ['channel_id' => 'channel_id']],
[['chnmembertype_id'], 'exist', 'skipOnError' => true, 'targetClass' => ChnmemberType::className(), 'targetAttribute' => ['chnmembertype_id' => 'chnmembertype_id']],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'user_id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'member_type' => Yii::t('app', 'Member Type'),
'member_note' => Yii::t('app', 'Member Note'),
'user_id' => Yii::t('app', 'User ID'),
'channel_id' => Yii::t('app', 'Channel ID'),
'chnmembertype_id' => Yii::t('app', 'Chnmembertype ID'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChannel()
{
return $this->hasOne(Channel::className(), ['channel_id' => 'channel_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChnmembertype()
{
return $this->hasOne(ChnmemberType::className(), ['chnmembertype_id' => 'chnmembertype_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(User::className(), ['user_id' => 'user_id']);
}
/**
* @inheritdoc
* @return \app\queries\ChnMemberQuery the active query used by this AR class.
*/
public static function find()
{
return new \app\queries\ChnMemberQuery(get_called_class());
}
}
I created a file name "AxChnMemJoin" :
<?php
namespace app\accions\chnmem;
use Yii;
use app\models\ChnMember;
class AxChnMemJoin
{
public function ChannelMember($CHNID)
{
return ChnMember::findOne(['user_id' => Yii::$app->user->id, 'channel_id' => $CHNID]);
}
public function JoinChannel($CHNID)
{
$MCHNMEM = new ChnMember;
$MCHNMEM->user_id = Yii::$app->user->id;
$MCHNMEM->channel_id = $CHNID;
$MCHNMEM->chnmembertype_id = '100';
$MCHNMEM->member_type = 'Member';
$MCHNMEM->member_note = '100';
$MCHNMEM->save();
}
public function LeaveChannel($CHNID)
{
$this->ChannelMember($CHNID)->delete();
}
}
?>
Then I call this file from a widget "ChannelHeaderWidget"
<?php
namespace app\components\Channel\ViewContent;
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use app\models\Channel;
use app\accions\chnmem\AxChnMemJoin;
class ChannelHeaderWidget extends \yii\base\Widget {
public $chn_id;
public function run()
{
$modelHDR = Channel::findOne($this->chn_id); $acc_ChnMem = new AxChnMemJoin;
$isMember = $acc_ChnMem->ChannelMember($modelHDR->channel_id);
// JOIN CHANNEL
if (Yii::$app->request->post('value') == 'JoinChannel')
{
$acc_ChnMem->JoinChannel($modelHDR->channel_id);
}
// LEAVE CHANNEL
if (Yii::$app->request->post('value') == 'LeaveChannel')
{
$acc_ChnMem->LeaveChannel($modelHDR->channel_id);
}
return $this->render('/chn/chn-header', [
'modelHDR' => $modelHDR,
'isMember' => $isMember,
]);
}
}
?>
and my view "/chn/chn-header"
<?php
use yii\helpers\Html;
use yii\helpers\Url; // URL HOME
/* @var $this yii\web\View */
/* @var $modelHDR app\models\Channel */
$this->title = $modelHDR->channel_title;
?>
<header class="top channel-header" style="background: url('<?=Url::home().$modelHDR->channel_bgimage?>') no-repeat;">
<div class="title-chn row">
<div class="col-xs-4">
<img class="img-circle" src="<?=Url::home().$modelHDR->user->user_thavatar?>" height="70" width="70">
</div>
<div class="col-xs-16">
<h2 class="title"><?= $modelHDR->channel_title ?></h2>
<h3 class="title hidden-xs"><?= $modelHDR->channel_subject ?></h3>
</div>
<div class="col-xs-4 text-right" style="padding:15px">
<?php if(Yii::$app->user->id){ ?>
<?php if($modelHDR->user_id == Yii::$app->user->id){ ?>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#ModalEdit">Edit</button>
<?php } ?>
<?php if($modelHDR->user_id !== Yii::$app->user->id){ ?>
<?= $this->render('/chn-mem/Join-Leave', [
'modelHDR' => $modelHDR,
'isMember' => $isMember,
])
?>
<?php } ?>
<?php } ?>
</div>
</div>
</header>