Yii Framework Forum: FK-less relations - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

FK-less relations Rate Topic: -----

#1 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 November 2008 - 10:16 PM

Is there a way to allow AR to work with MySQL MyISAM, that is without FK constraints? According to the documentation this is possible with SQLite.

Quote

Tip: For SQLite database, we may create tables that define the foreign key constraints such as the example below. However, these constraints are NOT enforced by the SQLite database itself.


/Tommy

0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 November 2008 - 10:20 PM

This is not supported. The Guide about SQLite says you need to define the FK constraints, but they are not enforced by SQLite.
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 27 November 2008 - 04:16 AM

I believe an option for locking down the model to a development config would solve my problem since I have InnoDB installed. Current web hotel of choice don't support InnoDB.

0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 27 November 2008 - 04:43 PM

Here's my suggested workaround (one line replaced in CMysqlSchema.php):


require_once(dirname(__FILE__).'/protected/dbfix/CMysqlSchema.php');


protected function findConstraints($table)
{
//$row=$this->getDbConnection()->createCommand('SHOW CREATE TABLE '.$table->rawName)->queryRow();

$row=array(file_get_contents(dirname(__FILE__).'/'.str_replace('`','',$table->rawName).'.sql', FILE_TEXT));

$matches=array();
$regexp='/FOREIGN KEYs+(([^)]+))s+REFERENCESs+([^(^s]+)s*(([^)]+))/mi';
foreach($row as $sql)
{
if(preg_match_all($regexp,$sql,$matches,PREG_SET_ORDER))
break;
}


The files are standard phpMyAdmin table dumps. Had to use file_get_contents() instead of file(), obviously because SHOW CREATE TABLE returns multiple FK declarations on the same line (causing exit from the foreach loop).

/Tommy

0

#5 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 27 November 2008 - 05:37 PM

I am afraid this workaround is not appropriate to be used in the framework. It is inefficient (file operation) and requires specific file locations.

If there is a bug with the SHOW CREATE TABLE approach, could you please report and we will see how to solve it.
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 27 November 2008 - 07:41 PM

I completely agree. Have to admit I hadn't yet thought about performance. I can imagine the files should be preloaded instead.

Also, I posted this just in case somebody else has the same situation (uses relations, want to take advance of AR, but myISAM-only hosting available).

Possibly referential checks has to be added to the CUD actions, thats what I'm going to have a look at next, now that I know I won't end up with an Yii exception due to stripped away FK declarations on deploying the database.

/Tommy

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users