Relational Active Record Problem

i have two class




<?php

class PublicInformation extends CActiveRecord{

	

	.....


	

	public function relations(){

		return array(		

			'Certificate' => array(self::HAS_MANY, 'Certificate', 'applicantId'),			

		);

	}

	

	

}

?>



And


<?php

class Certificate extends CActiveRecord{

	

..........

	

	public function relations(){

		return array(

			'PublicInformation' => array(self::BELONGS_TO, 'PublicInformation', 'applicantId'),

		);

	}

	

	

	

	

}

?>

i try to join these databases:




$applicantInfo = PublicInformation::model()->with('Certificate')->findByAttributes(array('applicantId'=>$applicantId));



but i got error




C:\WampServer\www\Keramos\yii\db\ar\CActiveFinder.php(706)


00694:     {

00695:         // determine the primary key value

00696:         if(is_string($this->_pkAlias))  // single key

00697:         {

00698:             if(isset($row[$this->_pkAlias]))

00699:                 $pk=$row[$this->_pkAlias];

00700:             else    // no matching related objects

00701:                 return null;

00702:         }

00703:         else // is_array, composite key

00704:         {

00705:             $pk=array();

00706: foreach($this->_pkAlias as $name=>$alias)

00707:             {

00708:                 if(isset($row[$alias]))

00709:                     $pk[$name]=$row[$alias];

00710:                 else    // no matching related objects

00711:                     return null;

00712:             }

00713:             $pk=serialize($pk);

00714:         }

00715: 

00716:         // retrieve or populate the record according to the primary key value

00717:         if(isset($this->records[$pk]))

00718:             $record=$this->records[$pk];







i traced the codes and i find that the $this->_pkAlias is null

where is the problem ?

Stack Trace

#0 C:\WampServer\www\Keramos\yii\db\ar\CActiveFinder.php(739): CJoinElement->populateRecord()

#1 C:\WampServer\www\Keramos\yii\db\ar\CActiveFinder.php(684): CJoinElement->populateRecord()

#2 C:\WampServer\www\Keramos\yii\db\ar\CActiveFinder.php(349): CJoinElement->runQuery()

#3 C:\WampServer\www\Keramos\yii\db\ar\CActiveFinder.php(72): CJoinElement->find()

#4 C:\WampServer\www\Keramos\yii\db\ar\CActiveRecord.php(1236): CActiveFinder->query()

#5 C:\WampServer\www\Keramos\yii\db\ar\CActiveRecord.php(1366): PublicInformation->query()

#6 C:\WampServer\www\Keramos\protected\controllers\StoreController.php(31): PublicInformation->findByAttributes()

#7 C:\WampServer\www\Keramos\yii\web\actions\CInlineAction.php(50): StoreController->actionView()

#8 C:\WampServer\www\Keramos\yii\web\CController.php(300): CInlineAction->run()

#9 C:\WampServer\www\Keramos\yii\web\filters\CFilterChain.php(133): StoreController->runAction()

#10 C:\WampServer\www\Keramos\yii\web\filters\CFilter.php(41): CFilterChain->run()

#11 C:\WampServer\www\Keramos\yii\web\CController.php(1049): CAccessControlFilter->filter()

#12 C:\WampServer\www\Keramos\yii\web\filters\CInlineFilter.php(59): StoreController->filterAccessControl()

#13 C:\WampServer\www\Keramos\yii\web\filters\CFilterChain.php(130): CInlineFilter->filter()

#14 C:\WampServer\www\Keramos\yii\web\CController.php(283): CFilterChain->run()

#15 C:\WampServer\www\Keramos\yii\web\CController.php(257): StoreController->runActionWithFilters()

#16 C:\WampServer\www\Keramos\yii\web\CWebApplication.php(324): StoreController->run()

#17 C:\WampServer\www\Keramos\yii\web\CWebApplication.php(121): CWebApplication->runController()

#18 C:\WampServer\www\Keramos\yii\base\CApplication.php(135): CWebApplication->processRequest()

#19 C:\WampServer\www\Keramos\index.php(10): CWebApplication->run()

Do you have primary keys in your db tables?

thanks derelict, solved.

now im trying to execute this query:




$criteria = new CDbCriteria();

			$criteria->with = array(

				 'Certificate',

				 'Dependent' ,

				 'Confirm' ,

				 'Education' ,

				 'Job' ,

				 'Language',

				 'Question',

				 'Relationship',

				 'Resume',

				 

				) ;			

			

$applicantInfo = PublicInformation::model()->findByPk($applicantId, $criteria);



and i get this error

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 41 bytes) in C:\WampServer\www\Keramos\yii\db\CDbCommand.php on line 372

how can i solve this problem ?

What is in your file CDbCommand.php on line 372? (I can’t identify it myself because it depends on Yii version)

you have to make:

  1. in database table PublicInformation

CONSTRAINT PK_PublicInformation_Certificate FOREIGN KEY (applicantId)

  1. in database table Certificate

CONSTRAINT FK_Certificate_PublicInformation FOREIGN KEY (applicantId)