قبل از اینکه راه حل مشکلتون رو بگم، یک مسئله رو درمورد قیدهایی که ساختین یادآوری کنم:
بهتره از CASCADE در اینجا برای عمل Delete استفاده نکنید (پارامتر اول از دو پارامتر آخری) چون اگه فرضاً رکورد همسر حذف بشه، باعث میشه کاربر مربوطه هم حذف بشه. بهتره بجاش از SET NULL استفاده کنید یعنی اگه یکی همسرش از سیستم حذف شد (مثلاً فوت کرد)، برگرده به جامعه مجردها !
ضمناً بهتره مدلها رو توی کنترلر فراخوانی کنید یا ترجیحاً از relationها استفاده کنید:
فایل Migration :
<?php
class m140411_160139_create_tbl_user extends CDbMigration
{
public function up()
{
$this->createTable('tbl_user',
array (
'id'=>'pk',
'name'=>'string',
'family'=>'string',
'shsh'=>'string',
'tt'=>'datetime',
'vazeiyat'=>'int(2)',
'sex'=>'int(2)',
'hamsar_id'=>'int(11)',
'father_id'=>'int(11)',
'mother_id'=>'int(11)',
), 'ENGINE=InnoDB'
);
$this->addForeignKey('hamsar', 'tbl_user', 'hamsar_id', 'tbl_user', 'id', 'SET NULL', 'CASCADE');
$this->addForeignKey('father', 'tbl_user', 'father_id', 'tbl_user', 'id', 'SET NULL', 'CASCADE');
$this->addForeignKey('mother', 'tbl_user', 'mother_id', 'tbl_user', 'id', 'SET NULL', 'CASCADE');
}
public function down()
{
$this->dropForeignKey('hamsar', 'tbl_user');
$this->dropForeignKey('father', 'tbl_user');
$this->dropForeignKey('mother', 'tbl_user');
$this->dropTable('tbl_user');
}
}
متد relations در فایل مدل User :
public function relations() {
return array(
'hamsar' => array(self::HAS_ONE, 'User', 'hamsar_id'),
'father' => array(self::BELONGS_TO, 'User', 'father_id'),
'mother' => array(self::BELONGS_TO, 'User', 'mother_id'),
'childrenOfMale' => array(self::HAS_MANY, 'User', 'father_id'),
'childrenOfFemale' => array(self::HAS_MANY, 'User', 'father_id'),
);
}
فایل view :
<?php if ($user->vazeiyat) : ?>
<div class="bb">
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $user->id . '.png'; ?>" />
<?php if ($user->hamsar) : ?>
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $user->hamsar->id . '.png'; ?>" />
<?php endif ; ?>
</div>
<?php foreach ($children as $child) : ?>
<div class="bb chi">
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $child->id . '.png'; ?>" />
<?php if ($child->hamsar) : ?>
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $child->hamsar->id . '.png'; ?>" />
<?php endif; ?>
<?php $grandChildren = ($child->sex ? $child->childrenOfMale : $child->childrenOfFemale);
<?php foreach ($grandChildren as $grandChild) : ?>
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $grandChild->id . '.png'; ?>" />
<?php if ($grandChild->hamsar) : ?>
<img src="<?php echo Yii::app()->homeUrl . '/images/' . $grandChild->hamsar->id . '.png'; ?>" />
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
public function actionIndex($uid) {
$user = User::model()->findByPk($uid);
if(!$user) {
throw new CHttpException(404, 'User not found.');
}
$this->render('index', array(
'user' => $user,
'children' => ($user->sex ? $user->childrenOfMale : $user->childrenOfFemale);
));
}
نحوه صدا زدن ویو در کنترلر:
public function actionIndex($uid) {
$user = User::model()->findByPk($uid);
if(!$user) {
throw new CHttpException(404, 'User not found.');
}
$this->render('index', array(
'user' => $user,
'children' => ($user->sex ? $user->childrenOfMale : $user->childrenOfFemale);
));
}