Hitman
(Kirill2)
June 8, 2009, 5:16am
1
Want to add to application modules useful thing - to add to the database tables of the module at first start, if they do not exist there. Encountered problems when creating action install, and the corresponding function in the model, I get the error, the table that the module is not in the database. With that, I did not request it. What could be wrong?
as a part of the model, but I do not know how to solve it. ie this is not a bug in the code, but simply that the model probably is a table in a function tableName ();, and if it is demolished, the same table with the name of the model …
Dave
(Deventer)
June 8, 2009, 9:10am
2
An excerpt of code would be helpfull. Did you defined the table name in the model file?
Hitman
(Kirill2)
June 8, 2009, 9:43am
3
Сlass Advertisements_mdl_advertisement extends CActiveRecord {
public $type_name = '';
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* Получить имя таблицы.
*
* @param Set $spec:
* = '' - таблица комментариев страниц;
* = 'users' - таблица пользователей;
* = 'advertisements' - таблица страниц;
*
* @return String имя соответствующей таблицы.
*/
public function tableName($spec='') {
return 'advertisements';
}
public function relations()
{
return array(
'advertisement_type_id'=>array(self::BELONGS_TO, 'Advertisements_mdl_types', 'advertisement_type_id'),
);
}
function installModule()
{
//$dump = implode("", file('.'));
//$sql = explode(";", $dump);
//print_R($sql); die;
//$sqlDone = true;
//foreach ($sql as $s)
//{
$sql = "LOAD DATA CONCURRENT INFILE './protected/modules/advertisements/config/schema_advertisements.sql'";
$command=$connection->createCommand($sql);
$rowCount=$command->execute();
//}
print $rowCount . 'ADDED';
return true;
}
public function uninstallModule()
{
$sql = 'DROP TABLE `advertisements`, `advertisements_categories`, `advertisements_types`';
$command = $connection->createCommand($sql)->execute();
return $command;
}
beginning of the file the model here is, here proishodzhit add to the tables in the installModule function.
Sorry for the garbage in the code, so I do to debug it
olafure
(Olafure)
June 8, 2009, 9:51am
4
Which line is causing the error ?
Dave
(Deventer)
June 8, 2009, 12:23pm
5
I am not sure but I think you can't name a relation like the FK column.
public function relations()
{
return array(
'advertisement_type_id'=>array(self::BELONGS_TO, 'Advertisements_mdl_types', 'advertisement_type_id'),
);
}
should look like
public function relations()
{
return array(
'advertisementType'=>array(self::BELONGS_TO, 'Advertisements_mdl_types', 'advertisement_type_id'),
);
}
That is because the name of the relation retrieves an object of 'Adivertisments_mdl_types' and not the id.