Yii简单关系问题

发布于 2024-10-27 15:06:57 字数 2941 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯沦落人 2024-11-03 15:06:57

我认为这里发生了一些事情。

1) 此错误(ID 不能为空。)来自您的 Socio 模型。请执行以下两件事来解决此问题:

  1. 如果您未在表单中设置 ID,请确保将 Socio 表的 id 主键设置为 auto_increment。
  2. 然后检查 Socio 模型中的 Rules() 函数并确保 ID 不是必需的。在终极指南中阅读有关模型验证规则的更多信息

2) 您正在设置两个“HAS_MANY”关系。我会设置一个“MANY_MANY”关系,如下所示:

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

您可以在 终极指南

我希望这可以帮助您走上正轨!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文