Yii简单关系问题
我对 Yii 框架和关系数据库完全陌生,但我需要创建一个小应用程序来控制合作伙伴和活动。合作伙伴(socios)可以有很多活动,活动可以有很多合作伙伴所以,这是我的数据库
CREATE TABLE `actividades` (
`id` int(11) NOT NULL,
`nombre` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `axs` (
`id_socio` int(11) NOT NULL,
`id_acti` int(11) NOT NULL,
KEY `id_socio` (`id_socio`),
KEY `id_acti` (`id_acti`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `socios` (
`id` int(11) NOT NULL,
`nombre` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ADD CONSTRAINT `id_socio` FOREIGN KEY (`id_socio`) REFERENCES `socios` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `id_acti` FOREIGN KEY (`id_acti`) REFERENCES `actividades` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE;
这是我的模型的关系
**Socios**
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'actividadesrel' => array(self::HAS_MANY, 'Actividades', 'id_socio'),
);
}
**Activades**
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'sociosrel' => array(self::HAS_MANY, 'Socios', 'id_socio'),
);
}
这是我的Socio的控制器
public function actionCreate()
{
$model=new Socios;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Socios']))
{
$model->attributes=$_POST['Socios'];
if($model->save()) {
foreach ($_POST['Socios']['actividadesrel'] as $actividadId) {
$socioActividad = new Axs;
$socioActividad->socio_id = $model->id;
$socioActividad->acti_Id = $actividadId;
if (!$socioActividad->save()) print_r($socioActividad->errors);
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
最后在我的社会创建形式
<div class="row">
<?php echo $form->labelEx($model,'Actividades del socio'); ?>
<?php echo $form->dropDownList($model, 'actividadesrel', CHtml::listData(
Actividades::model()->findAll(), 'id', 'nombre'), array('multiple'=>'multiple',
'size'=>5)
); ?>
<?php echo $form->error($model,'actividadesrel'); ?>
</div>
现在,每次我尝试创建一个新的合作伙伴(社会)我收到这条消息:
Please fix the following input errors:
ID cannot be blank.
这让我完全发疯了:P。我认为我的错误是对 Yii 和 ActiveRecord 以及其他与关系数据库相关的东西理解很差。
你能帮助我吗?
谢谢!!!!
I'm totally new to Yii Framework and Relational databases, but I need to create a small app to control partners and activities. Partners (socios) could have many activities and activities could have many partners So, Here is my database
CREATE TABLE `actividades` (
`id` int(11) NOT NULL,
`nombre` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `axs` (
`id_socio` int(11) NOT NULL,
`id_acti` int(11) NOT NULL,
KEY `id_socio` (`id_socio`),
KEY `id_acti` (`id_acti`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `socios` (
`id` int(11) NOT NULL,
`nombre` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ADD CONSTRAINT `id_socio` FOREIGN KEY (`id_socio`) REFERENCES `socios` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `id_acti` FOREIGN KEY (`id_acti`) REFERENCES `actividades` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE;
And this is my models's relations
**Socios**
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'actividadesrel' => array(self::HAS_MANY, 'Actividades', 'id_socio'),
);
}
**Activades**
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'sociosrel' => array(self::HAS_MANY, 'Socios', 'id_socio'),
);
}
And this is my Socio's controller
public function actionCreate()
{
$model=new Socios;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Socios']))
{
$model->attributes=$_POST['Socios'];
if($model->save()) {
foreach ($_POST['Socios']['actividadesrel'] as $actividadId) {
$socioActividad = new Axs;
$socioActividad->socio_id = $model->id;
$socioActividad->acti_Id = $actividadId;
if (!$socioActividad->save()) print_r($socioActividad->errors);
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
And finally in my socio create form
<div class="row">
<?php echo $form->labelEx($model,'Actividades del socio'); ?>
<?php echo $form->dropDownList($model, 'actividadesrel', CHtml::listData(
Actividades::model()->findAll(), 'id', 'nombre'), array('multiple'=>'multiple',
'size'=>5)
); ?>
<?php echo $form->error($model,'actividadesrel'); ?>
</div>
Now, everytime I try to create a new partner (socio) i get this message:
Please fix the following input errors:
ID cannot be blank.
This is driving me totally crazy :P. I'm assume my mistake is a very poor understanding about Yii and ActiveRecord and other shit related to relational databases.
Can you help me?
Thanks!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里发生了一些事情。
1) 此错误(
ID 不能为空。
)来自您的 Socio 模型。请执行以下两件事来解决此问题:id
主键设置为 auto_increment。2) 您正在设置两个“HAS_MANY”关系。我会设置一个“MANY_MANY”关系,如下所示:
Socio model:
Actividades model:
您可以在 终极指南。
我希望这可以帮助您走上正轨!
I think there are a few things going on here.
1) This error (
ID cannot be blank.
) is coming from your Socio model. Do two things to fix this:id
primary key is set to auto_increment, if you are not setting the ID in the Form.2) You are setting up two "HAS_MANY" relations. I would set up a "MANY_MANY" relation instead, like so:
Socio model:
Actividades model:
You can read more about relations in the Ultimate Guide.
I hope this helps get you on the right track!