2011-03-25 39 views
1

我完全新的Yii框架和关系型数据库,但我需要创建一个小的应用程序来控制合作伙伴和活动。合作伙伴(socios)可以有许多活动和活动可以有许多合作伙伴所以,这里是我的数据库Yii的简单的关系问题

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'), 
    ); 
    } 

这是我的社会的控制器

  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以及与关系数据库相关的其他垃圾的理解很差。

你能帮我吗?

谢谢!

回答

3

我觉得有几件事情怎么回事。

1)此错误(ID cannot be blank.)从你的社会模型的到来。做两两件事来解决这个问题:

  1. 确保您社会经济表的id主键设置为AUTO_INCREMENT,如果你不设置Form中的ID。
  2. 然后检查Socio模型中的rules()函数,并确保不需要ID。在Ultimate Guide中阅读更多关于模型验证规则的信息。

2)要设置两个 “HAS_MANY” 关系。我会成立一个 “MANY_MANY” 的关系,而不是像这样:

社会经济模式:

public function relations() 
{ 
    return array(
     'actividades'=>array(self::MANY_MANY, 'Actividades', 
      'axs(id_socio, id_acti)'), 
    ); 
} 

ACTIVIDADES型号:

public function relations() 
{ 
    return array(
     'socios'=>array(self::MANY_MANY, 'Socios', 
      'axs(id_acti, id_socio)'), 
    ); 
} 

你可以阅读更多有关Ultimate Guide关系。

我希望这有助于让你在正确的轨道上!

+0

我想知道在这MANY_MANY关系,如果我们插入一个新的“ACTIVIDADES”,那么如何还插入一个进入连接表,在这种情况下,“AXS”。 – 2011-10-30 03:35:19

+0

有一些方便的扩展,使这更容易(http://www.yiiframework.com/search/?q=many_many&type=extension)。基本上,在你的afterSave()方法中,你只需要做一些数据库插入。你可以直接用SQL来做这件事,或者为你的关系表创建一个AR模型,并保存它(如下所示:http://www.yiiframework.com/forum/index.php?/topic/8170-best-method - 用于节能一对多,多对多的记录) – thaddeusmt 2011-10-30 17:12:48