开发者

Yii simple relational problem

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-&开发者_如何学编程gt;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!!!!


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:

  1. Make sure your Socio table's id primary key is set to auto_increment, if you are not setting the ID in the Form.
  2. Then check the rules() function in the Socio Model and make sure that ID is not required. Read more about Model validation rules here in the Ultimate Guide.

2) You are setting up two "HAS_MANY" relations. I would set up a "MANY_MANY" relation instead, like so:

Socio model:

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

Actividades model:

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

You can read more about relations in the Ultimate Guide.

I hope this helps get you on the right track!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜