I have done a little dirty thing like this, without FK fields, for Yii 1.0:
class TableData extends CActiveRecord {
private static $tableName;
public function __construct($attributes=array(), $scenario='') {
if (self::$tableName === null) {
throw new CDbException(Yii::t('main', '"TableData" requires a "tableName" property.'));
}
parent::__construct($attributes, $scenario);
}
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return self::$tableName;
}
public static function setTableName($tableName) {
if (! in_array($tableName, self::$db->schema->tableNames)) {
throw new CDbException(Yii::t('main', 'Table "{table}" does not exist.', array('{table}'=>$tableName)));
}
self::$tableName = $tableName;
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
$rules = array();
$required = array();
foreach (self::$db->schema->getTable($this->tableName())->columns as $col) {
if (!$col->allowNull) {
$required[] = $col->name;
}
}
if (count($required) > 0) {
$rules[] = array(join(', ', $required), 'required');
}
return $rules;
}
/*
* @todo relations()
*/
}
and in the controller:
/**
* Defines table on which work
*/
private function setTable() {
$table = _request('table');
if ($table) {
TableData::setTableName($table);
return $table;
}
return null;
}
in the view, the main part is _form.php, needs also to be enhanced
this code loockup all tables on the DB that have a relationship with the fields of specific table, that automatically generate the array relational rules.
for
public function relations()
{
return array();
}
<?
/* coded by Max Rivera */
$con=mysql_connect('localhost','cpga_sis','******');
$db=mysql_select_db('cpga_sis',$con);
function ltable()
{
global $db;
$sql = mysql_query("SHOW TABLE STATUS");
while($linha = mysql_fetch_assoc($sql)):
$r[pkey($linha[Name])]=$linha[Name];
$q[$linha[Name]]=pkey($linha[Name]);
endwhile;
$x[0]=$r;
$x[1]=$q;
return $x;
}
function pkey($tabela)
{
global $db;
$query="DESC {$tabela}";
$results=mysql_query($query);
while ($row=mysql_fetch_array($results))
{
if ($row[Key]=="PRI")
{
return $row[Field];
}
}
return false;
}
function fk($tabela)
{
$chaves=ltable();
$pkey=pkey($tabela);
$query="DESC {$tabela}";
$results=mysql_query($query);
while ($row=mysql_fetch_array($results))
{
if(in_array($row[Field],$chaves[1]) AND $row[Field]!=$pkey)
{
$s=$chaves[0][$row[Field]];
$arr[$s]=$row[Field];
}
}
$r ="array(";
foreach($arr as $tabela=>$pk)
{
$r .= "'".$tabela."'=>array(self::BELONGS_TO, '".$tabela."','".$pk."'),";
}
$r .=")";
return $r;
}
$tabela='usuario';
print_r(fk($tabela));
?>
I have translated the (few) Strings used in the default crud Generator to german a while ago.
I think it makes sense to provide a set of default crud pages per language, rather than making
the crud generator i18n-capable, consider this:
crud model style=default lang=de js=active
or
crud model style=futuristic lang=gb js=inactive
to get the crud command some more abilities.
In addition to this it would be cool if the yii-crud would get more intelligent deploying database rows, for example an boolean or tinyint(1) field should be generated as a checkbox; foreign key relations should get be generated as an combobox, date and timestamp to a datepicker (when js is enabled), and so on…
I have builded all you are saying (90%) but for yii 1.0
now I am rewrinting all for yii 1.x but I have some dificult becouse the developers has changed the parsing method for generate crud (templates and php code)…