CakePHP中的关联

时间:2014-04-29 04:56:24

标签: cakephp associations

我有2个db tables:用户&项目

Users表的字段是: ID,姓名,用户名,密码,电子邮件,地址,电话。

Projects表的字段是: id,name,description,user_id,deadline,budget。

我需要的是,我将有一个表单,其中将包含这两个表中的所有字段。并且,当我提交表单时,这些字段将保存在这两个表中。

例如,我的表单将是这样的:

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('username',array('type'=>'text','div'=>false));
    echo $this->Form->input('password',array('type'=>'password','div'=>'false));
    echo $this->Form->input('email',array('type'=>'email','div'=>false));
    echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
    echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('description',array('type'=>'text','div'=>false));
    echo $this->Form->input('deadline',array('type'=>'date','div'=>false));
    echo $this->Form->input('budget',array('type'=>'num','div'=>false));
    echo $this->Form->submit('Save');
    echo $this->Form->end();
?>

现在,我希望在提交表单时,字段将保存在相应的表中; Users表将收到&amp;保存其字段,Projects表将收到&amp;保存它的字段。 为此,我尝试了hasManyUsers之间的Projects关联。 Users表,表示Projects表中的每个用户都有User.php表中的许多项目。

class User extends AppModel{ public $hasMany=array( 'Project'=>array( 'className'=>'Project') ); } 中,我尝试了这个:

Users

我认为它有效,但它没有,只有Project表得到它的值,没有值到{{1}}表。这里有什么问题 ?我该怎么办?

感谢。

5 个答案:

答案 0 :(得分:1)

在您的视图中尝试此操作

echo $this->Form->create('User');
echo $this->Form->input('name',array('type'=>'text','div'=>false));
echo $this->Form->input('username',array('type'=>'text','div'=>false));
echo $this->Form->input('password',array('type'=>'password','div'=>false));
echo $this->Form->input('email',array('type'=>'email','div'=>false));
echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.0.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.0.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();

在你的控制器中你必须使用

$this->User->saveAssociated($data);

请参阅manual了解如何保存相关数据

答案 1 :(得分:1)

首先,将模型名称UserProject添加为表单字段的前缀,以便将它们分开:

查看

<?php
echo $this->Form->create('User');
echo $this->Form->input('User.name',array('type'=>'text','div'=>false));
echo $this->Form->input('User.username',array('type'=>'text','div'=>false));
echo $this->Form->input('User.password',array('type'=>'password','div'=>false));
echo $this->Form->input('User.email',array('type'=>'email','div'=>false));
echo $this->Form->input('User.address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('User.phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();

第二,使用分离将数据保存到不同的模型中。不要忘记将关联的列user_id添加到您的表projects

<强>控制器

class UserController extends AppController
{
    function create()
    {
        $this->loadModel('Project');

        if($this->request->is('post', 'put'))
        {
            $this->User->getDataSource()->begin($this); // Starts transaction
            if($this->User->save($this->request->data['User']))
            {
                // Success, save project now
                $this->request->data['Project']['user_id'] = $this->User->getLastInsertId();

                if($this->Project->save($this->request->data['Project']))
                {
                    $this->User->getDataSource()->commit($this); // Commit
                    // success
                } else {
                    $this->User->getDataSource()->rollback($this); // Rollback
                }
            } else {
                $this->User->getDataSource()->rollback($this); // Rollback
            }
        }
    }
}

我测试了这段代码→有效。

答案 2 :(得分:0)

您的问题对我来说不是很清楚,但我看到的是您需要使用关联来保存数据。

但是关联是两个Active Record模型之间的连接。创建关联以在两个模型的实例之间维护主键 - 外键信息。

您需要先保存USERS表数据并获取上次保存的主ID,并将其用作PROJECTS表的user_id,并保存创建PROJECT实例的PROJECT表信息。

答案 3 :(得分:0)

视图:

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('username',array('type'=>'text','div'=>false));
    echo $this->Form->input('password',array('type'=>'password','div'=>'false));
    echo $this->Form->input('email',array('type'=>'email','div'=>false));
    echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
    echo $this->Form->input('phone',array('type'=>'tel','div'=>false));


    // adding associate model name to every field..    
    echo $this->Form->input('Project.name',array('type'=>'text','div'=>false));
    echo $this->Form->input('Project.description',array('type'=>'text','div'=>false));
    echo $this->Form->input('Project.deadline',array('type'=>'date','div'=>false));
    echo $this->Form->input('Project.budget',array('type'=>'num','div'=>false));
    echo $this->Form->submit('Save');
    echo $this->Form->end();

注意:您可以停用所有&#39; div&#39;关于表单创建..查看文档以获取详细信息...

用户模型:

class User extends AppModel{
   public $hasMany=array('Project');
}

UsersController:

public function add(){
    if($this->request->is('post')){
       $this->User->create();
       if($this->User->save($this->request->data)){// if you association is correct, all associated data will save automatically. 
         $this->Session->setFlash('saved');
         $this->redirect('where you wants to redirect');
       }else{
         $this->Session->setFlash('Something went wrong. Try again');
       }

    }

}

答案 4 :(得分:0)

使用saveAll轻松实现

<!-- View -->
<?php
echo $this->Form->create('User');
echo $this->Form->input('name',array('type'=>'text','div'=>false));
echo $this->Form->input('username',array('type'=>'text','div'=>false));
echo $this->Form->input('password',array('type'=>'password','div'=>false));
echo $this->Form->input('email',array('type'=>'email','div'=>false));
echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.0.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.0.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();
?>

<!-- Controller -->
<?php
class UserController extends AppController
{
    function create()
    {
        if($this->request->is('post', 'put'))
        {
            if($this->User->saveAll($this->request->data))
            {
                // Success - set your flashMessage
            } else {
                // Error - set your flashMessage
            }
        }
    }
}
?>